Make other Rtu options configurable
parent
fa867cbfe5
commit
2a7387eb76
|
@ -352,6 +352,26 @@ version = "0.2.126"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
|
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]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
version = "0.4.7"
|
version = "0.4.7"
|
||||||
|
@ -444,6 +464,7 @@ dependencies = [
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"serialport",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-modbus",
|
"tokio-modbus",
|
||||||
"tokio-serial",
|
"tokio-serial",
|
||||||
|
@ -542,6 +563,12 @@ version = "0.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pkg-config"
|
||||||
|
version = "0.3.25"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pollster"
|
name = "pollster"
|
||||||
version = "0.2.5"
|
version = "0.2.5"
|
||||||
|
@ -723,9 +750,11 @@ dependencies = [
|
||||||
"IOKit-sys",
|
"IOKit-sys",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
|
"libudev",
|
||||||
"mach 0.3.2",
|
"mach 0.3.2",
|
||||||
"nix 0.24.1",
|
"nix 0.24.1",
|
||||||
"regex",
|
"regex",
|
||||||
|
"serde",
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -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" }
|
rumqttc = { version = "0.13.0", features = ["url"], git = "https://github.com/bytebeamio/rumqtt" }
|
||||||
serde = { version = "1.0.139", features = ["serde_derive"] }
|
serde = { version = "1.0.139", features = ["serde_derive"] }
|
||||||
serde_json = "1.0.82"
|
serde_json = "1.0.82"
|
||||||
|
serialport = { version = "4.2.0", features = ["serde"] }
|
||||||
tokio = { version = "1.20.0", features = ["rt", "rt-multi-thread"] }
|
tokio = { version = "1.20.0", features = ["rt", "rt-multi-thread"] }
|
||||||
tokio-modbus = "0.5.3"
|
tokio-modbus = "0.5.3"
|
||||||
tokio-serial = "5.4.3"
|
tokio-serial = "5.4.3"
|
||||||
|
|
47
src/main.rs
47
src/main.rs
|
@ -42,10 +42,18 @@ enum ModbusProto {
|
||||||
// tty: std::path::PathBuf,
|
// tty: std::path::PathBuf,
|
||||||
tty: String,
|
tty: String,
|
||||||
baud_rate: u32,
|
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
|
#[serde(default = "default_modbus_data_bits")]
|
||||||
// flow_control: tokio_se&rial::FlowControl,
|
data_bits: tokio_serial::DataBits, // TODO: allow this to be represented as a number instead of string
|
||||||
// parity: tokio_serial::Parity,
|
|
||||||
|
#[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
|
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)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct Range {
|
struct Range {
|
||||||
address: u16,
|
address: u16,
|
||||||
|
@ -337,8 +361,19 @@ async fn handle_connect(
|
||||||
let socket_addr = format!("{}:{}", host, port).parse().unwrap();
|
let socket_addr = format!("{}:{}", host, port).parse().unwrap();
|
||||||
tcp::connect_slave(socket_addr, slave).await.unwrap()
|
tcp::connect_slave(socket_addr, slave).await.unwrap()
|
||||||
}
|
}
|
||||||
ModbusProto::Rtu { ref tty, baud_rate } => {
|
ModbusProto::Rtu {
|
||||||
let builder = tokio_serial::new(tty, baud_rate);
|
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();
|
let port = tokio_serial::SerialStream::open(&builder).unwrap();
|
||||||
rtu::connect_slave(port, slave).await.unwrap()
|
rtu::connect_slave(port, slave).await.unwrap()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue