From c9dac8863d4c738dbfd5647dc016ec48d92b2a9c Mon Sep 17 00:00:00 2001 From: rise0chen Date: Fri, 15 Nov 2024 14:12:57 +0800 Subject: [PATCH] fix: modbus_rtu_slave --- modbus_rtu_slave/bin/rustfmt.toml | 2 ++ modbus_rtu_slave/bin/src/main.rs | 37 +++++++++++++------------- modbus_rtu_slave/web/index.html | 44 ++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 34 deletions(-) create mode 100644 modbus_rtu_slave/bin/rustfmt.toml diff --git a/modbus_rtu_slave/bin/rustfmt.toml b/modbus_rtu_slave/bin/rustfmt.toml new file mode 100644 index 0000000..d0fecdb --- /dev/null +++ b/modbus_rtu_slave/bin/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 150 +merge_derives = false diff --git a/modbus_rtu_slave/bin/src/main.rs b/modbus_rtu_slave/bin/src/main.rs index df28f4d..af1e988 100644 --- a/modbus_rtu_slave/bin/src/main.rs +++ b/modbus_rtu_slave/bin/src/main.rs @@ -1,4 +1,5 @@ use std::future::{self, Future}; +use std::net::SocketAddrV4; use std::pin::Pin; use std::sync::Arc; use tokio::sync::Mutex; @@ -18,7 +19,7 @@ impl tokio_modbus::server::Service for Service { fn call(&self, req: Self::Request) -> Self::Future { let SlaveRequest { slave, request } = req; - if slave != self.slave.into() { + if slave != self.slave.0 { return Box::pin(future::ready(Ok(None))); } @@ -93,10 +94,7 @@ impl tokio_modbus::server::Service for Service { let fut = async move { let mut ctx = ctx.lock().await; ctx.write_multiple_registers(addr, &datas).await.unwrap()?; - Ok(Some(Response::WriteMultipleRegisters( - addr, - datas.len() as u16, - ))) + Ok(Some(Response::WriteMultipleRegisters(addr, datas.len() as u16))) }; Box::pin(fut) } @@ -114,19 +112,12 @@ impl tokio_modbus::server::Service for Service { let ctx = self.ctx.clone(); let fut = async move { let mut ctx = ctx.lock().await; - let rsp = ctx - .read_write_multiple_registers(rr, cnt, wr, &datas) - .await - .unwrap()?; + let rsp = ctx.read_write_multiple_registers(rr, cnt, wr, &datas).await.unwrap()?; Ok(Some(Response::ReadWriteMultipleRegisters(rsp))) }; Box::pin(fut) } - Request::ReportServerId => Box::pin(future::ready(Ok(Some(Response::ReportServerId( - self.slave.into(), - true, - Vec::new(), - ))))), + Request::ReportServerId => Box::pin(future::ready(Ok(Some(Response::ReportServerId(self.slave.into(), true, Vec::new()))))), Request::Custom(code, bytes) => { println!("unknown mb({}): {:02X?}", code, bytes); Box::pin(future::ready(Err(ExceptionCode::IllegalFunction))) @@ -137,16 +128,26 @@ impl tokio_modbus::server::Service for Service { #[tokio::main] async fn main() { - let socket_addr = "127.0.0.1:3051".parse().unwrap(); - let client = tcp::connect(socket_addr).await.unwrap(); + let lebai = lebai_sdk::connect("127.0.0.1".into(), true).await.unwrap(); + let mb_serial = lebai.get_item("plugin_modbus_rtu_slave_serial".into()).await.unwrap().value; + let mb_baud_rate = lebai.get_item("plugin_modbus_rtu_slave_baud_rate".into()).await.unwrap().value; + let mb_slave_id = lebai.get_item("plugin_modbus_rtu_slave_salve_id".into()).await.unwrap().value; + let mb_simu = lebai.get_item("plugin_modbus_rtu_slave_simu".into()).await.unwrap().value; - let slave = Slave(12); + let socket_addr = if mb_simu == "true" { + SocketAddrV4::new([127,0,0,1].into(), 3050) + } else { + SocketAddrV4::new([127,0,0,1].into(), 3051) + }; + let client = tcp::connect(socket_addr.into()).await.unwrap(); + let slave = Slave(mb_slave_id.parse().unwrap_or(30)); let service = Service { slave, ctx: Arc::new(Mutex::new(client)), }; - let server_builder = tokio_serial::new("/dev/pts/6", 19200); + let serial = format!("/dev/ttyS{}",if mb_serial.is_empty(){"1"}else{&mb_serial}); + let server_builder = tokio_serial::new(serial, mb_baud_rate.parse().unwrap_or(115200)); let server_serial = tokio_serial::SerialStream::open(&server_builder).unwrap(); let server = Server::new(server_serial); diff --git a/modbus_rtu_slave/web/index.html b/modbus_rtu_slave/web/index.html index e90793b..fc1c5be 100644 --- a/modbus_rtu_slave/web/index.html +++ b/modbus_rtu_slave/web/index.html @@ -17,8 +17,11 @@ 从站ID

- 超时时间(ms)
- + 手臂环境
+
@@ -53,18 +56,26 @@ }; ws.send(JSON.stringify(rpc_data)); rpc_data.params[0] = { - "key": "plugin_modbus_rtu_slave_timeout", - "value": document.getElementById("timeout").value + "key": "plugin_modbus_rtu_slave_simu", + "value": document.getElementById("simu").value + }; + ws.send(JSON.stringify(rpc_data)); + + rpc_data.method = "restart_plugin_daemon"; + rpc_data.params[0] = { + "name": "modbus_rtu_slave", }; ws.send(JSON.stringify(rpc_data)); } function get() { - rpc_data.method = "get_item"; + rpc_data.method = "get_box_devices"; rpc_data.id = 40; rpc_data.params[0] = { - "key": "plugin_serial_devices", + "prefix": "ttyS", }; ws.send(JSON.stringify(rpc_data)); + + rpc_data.method = "get_item"; rpc_data.id = 41; rpc_data.params[0] = { "key": "plugin_modbus_rtu_slave_serial", @@ -80,9 +91,9 @@ "key": "plugin_modbus_rtu_slave_salve_id", }; ws.send(JSON.stringify(rpc_data)); - rpc_data.id = 55; + rpc_data.id = 61; rpc_data.params[0] = { - "key": "plugin_modbus_rtu_slave_timeout", + "key": "plugin_modbus_rtu_slave_simu", }; ws.send(JSON.stringify(rpc_data)); } @@ -100,13 +111,9 @@ console.log(msg.error); } else { var id = msg.id; - var value = msg.result.value; - if (value === undefined) { - return; - } if (id == 40) { let options = []; - let indexs = JSON.parse(value); + let indexs = msg.result.devices; for (const i in indexs) { let selected = indexs[i] == document.getElementById("serial").value ? "selected" : ""; options.push(``); @@ -117,8 +124,13 @@ document.getElementById("serial").innerHTML = options; } } + + var value = msg.result.value; + if (value === undefined) { + return; + } if (id == 41) { - document.getElementById("serial").value = value || "0"; + document.getElementById("serial").value = value || "1"; } if (id == 42) { document.getElementById("baud_rate").value = value || "115200"; @@ -126,8 +138,8 @@ if (id == 51) { document.getElementById("salve_id").value = value || "30"; } - if (id == 55) { - document.getElementById("timeout").value = value || "800"; + if (id == 61) { + document.getElementById("simu").value = value || "false"; } } }