1 个不稳定版本
0.1.0 | 2023年6月27日 |
---|
#1007 in 硬件支持
19KB
412 代码行
USB 电子秤
使用 Rust 与 USB 电子秤交互!使用 hidapi
crate 与 USB 设备交互。
入门
添加到您的 Cargo.toml
# Without Serde
usb_scale = { version = "0.1.0", features = [] }
# With Serde
usb_scale = { version = "0.1.0", features = ["serde"] }
连接到电子秤
fn connect_to_scale() {
use usb_scale::{Scale, vendors::FAIRBANKS_SCB_900_SCALE};
match Scale::connect(FAIRBANKS_SCB_900_SCALE) {
Ok(scale) => println!("{:?}", scale),
Err(err) => println!("Error: {:?}", err),
}
}
获取读数
fn get_a_reading() {
use usb_scale::{Scale, ScaleReading, error::ScaleResult, vendors::FAIRBANKS_SCB_900_SCALE};
let scale = Scale::connect(FAIRBANKS_SCB_900_SCALE).unwrap();
let reading: ScaleResult<ScaleReading> = scale.read();
println!("{:?}", reading);
}
使用自定义厂商信息(您通常可以在电子秤的说明书中找到此信息)
fn use_custom_vendor_info() {
use usb_scale::{Scale, vendors::VendorInfo};
const FAIRBANKS_VENDOR_ID: u16 = 0x0B67;
const FAIRBANKS_SCB_900_PRODUCT_ID: u16 = 0x555E;
let fairbanks_scb_900 = VendorInfo::new(FAIRBANKS_VENDOR_ID, FAIRBANKS_SCB_900_PRODUCT_ID);
let scale = Scale::connect(fairbanks_scb_900).unwrap();
}
更复杂的示例(在 examples/src/main.rs
中找到)
use usb_scale::{
Scale,
ScaleReading,
ScaleStatus,
error::{ScaleError, ScaleResult},
weight::{Weight, WeightUnit},
vendors::FAIRBANKS_SCB_900_SCALE
};
use std::thread;
use std::time::Duration;
fn main() {
let mut scale_connection = Scale::connect(FAIRBANKS_SCB_900_SCALE);
loop {
scale_connection = match scale_connection {
Ok(scale) => {
match scale.read() {
Ok(reading) => {
println!("Reading: {}", reading);
// Status: Is the scale Stable, In Motion, Requires Taring, etc
println!("Scale Status: {}", reading.status);
println!("Report ID: {}", reading.report_id);
println!("Data Scaling: {}", reading.data_scaling);
println!("Scale Unit: {:?}", reading.weight.and_then(|w| Some(w.unit)));
let scale_weight_text = match reading.weight {
Some(weight) => weight.to_string(),
None => "No Reading".to_string(),
};
println!("Scale Weight: {}", scale_weight_text);
Ok(scale)
},
Err(err) => {
println!("Could not read scale! {}", err);
println!("Attempting to reconnect...");
scale.reconnect()
}
}
},
Err(err) => {
println!("Could not connect to scale! {}", err);
println!("Attempting to reconnect...");
Scale::connect(FAIRBANKS_SCB_900_SCALE)
}
};
println!("");
thread::sleep(Duration::from_millis(200));
}
}
依赖项
~0.4–11MB
~71K SLoC