diff --git a/Cargo.lock b/Cargo.lock index ba6077f..8aa6a23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -352,6 +352,26 @@ version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" +[[package]] +name = "libudev" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b324152da65df7bb95acfcaab55e3097ceaab02fb19b228a9eb74d55f135e0" +dependencies = [ + "libc", + "libudev-sys", +] + +[[package]] +name = "libudev-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324" +dependencies = [ + "libc", + "pkg-config", +] + [[package]] name = "lock_api" version = "0.4.7" @@ -444,6 +464,7 @@ dependencies = [ "rumqttc", "serde", "serde_json", + "serialport", "tokio", "tokio-modbus", "tokio-serial", @@ -542,6 +563,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" + [[package]] name = "pollster" version = "0.2.5" @@ -723,9 +750,11 @@ dependencies = [ "IOKit-sys", "bitflags", "cfg-if", + "libudev", "mach 0.3.2", "nix 0.24.1", "regex", + "serde", "winapi", ] diff --git a/Cargo.toml b/Cargo.toml index 2a06ad4..b5449be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ clap = { version = "3.2.12", features = ["derive", "env"] } rumqttc = { version = "0.13.0", features = ["url"], git = "https://github.com/bytebeamio/rumqtt" } serde = { version = "1.0.139", features = ["serde_derive"] } serde_json = "1.0.82" +serialport = { version = "4.2.0", features = ["serde"] } tokio = { version = "1.20.0", features = ["rt", "rt-multi-thread"] } tokio-modbus = "0.5.3" tokio-serial = "5.4.3" diff --git a/src/main.rs b/src/main.rs index 462f12f..ea427c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,10 +42,18 @@ enum ModbusProto { // tty: std::path::PathBuf, tty: String, baud_rate: u32, - // data_bits: tokio_serial::DataBits, // TODO: allow this to be represented as a number instead of string - // stop_bits: tokio_serial::StopBits, // TODO: allow this to be represented as a number instead of string - // flow_control: tokio_se&rial::FlowControl, - // parity: tokio_serial::Parity, + + #[serde(default = "default_modbus_data_bits")] + data_bits: tokio_serial::DataBits, // TODO: allow this to be represented as a number instead of string + + #[serde(default = "default_modbus_stop_bits")] + stop_bits: tokio_serial::StopBits, // TODO: allow this to be represented as a number instead of string + + #[serde(default = "default_modbus_flow_control")] + flow_control: tokio_serial::FlowControl, + + #[serde(default = "default_modbus_parity")] + parity: tokio_serial::Parity, }, } @@ -53,6 +61,22 @@ fn default_modbus_port() -> u16 { 502 } +fn default_modbus_data_bits() -> tokio_serial::DataBits { + tokio_serial::DataBits::Eight +} + +fn default_modbus_stop_bits() -> tokio_serial::StopBits { + tokio_serial::StopBits::One +} + +fn default_modbus_flow_control() -> tokio_serial::FlowControl { + tokio_serial::FlowControl::None +} + +fn default_modbus_parity() -> tokio_serial::Parity { + tokio_serial::Parity::None +} + #[derive(Serialize, Deserialize)] struct Range { address: u16, @@ -337,8 +361,19 @@ async fn handle_connect( let socket_addr = format!("{}:{}", host, port).parse().unwrap(); tcp::connect_slave(socket_addr, slave).await.unwrap() } - ModbusProto::Rtu { ref tty, baud_rate } => { - let builder = tokio_serial::new(tty, baud_rate); + ModbusProto::Rtu { + ref tty, + baud_rate, + data_bits, + stop_bits, + flow_control, + parity, + } => { + let builder = tokio_serial::new(tty, baud_rate) + .data_bits(data_bits) + .flow_control(flow_control) + .parity(parity) + .stop_bits(stop_bits); let port = tokio_serial::SerialStream::open(&builder).unwrap(); rtu::connect_slave(port, slave).await.unwrap() }