Skip to content

Commit

Permalink
fix: modbus_rtu_slave
Browse files Browse the repository at this point in the history
  • Loading branch information
rise0chen committed Nov 15, 2024
1 parent 74ce4b6 commit c9dac88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
2 changes: 2 additions & 0 deletions modbus_rtu_slave/bin/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 150
merge_derives = false
37 changes: 19 additions & 18 deletions modbus_rtu_slave/bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)));
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)))
Expand All @@ -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);

Expand Down
44 changes: 28 additions & 16 deletions modbus_rtu_slave/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
从站ID<br>
<input type="text" id="salve_id">
<br>
超时时间(ms)<br>
<input type="text" id="timeout">
手臂环境<br>
<select id="simu">
<option value="false" selected>真机环境</option>
<option value="true">仿真环境</option>
</select>
<br>
</form>
<button onclick="save()">保存参数</button>
Expand Down Expand Up @@ -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",
Expand All @@ -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));
}
Expand All @@ -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(`<option value="${indexs[i]}" ${selected}>${indexs[i]}</option>`);
Expand All @@ -117,17 +124,22 @@
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";
}
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";
}
}
}
Expand Down

0 comments on commit c9dac88

Please sign in to comment.