Comments (6)
Pseudocode:
let mut direction_pin = gpio.get(DIRECTION_PIN)?.into_output();
let mut modbus_request = ModbusRequest::new(SLAVE_ADDRESS, ModbusProto::Rtu);
let mut request_buffer = Vec::new();
let mut response_buffer = Vec::new();
let mut uart_buffer = [0u8; 128];
// example: get registers
modbus_request.generate_get_holdings(address, num_registers, &mut request_buffer)?;
direction_pin.set_high();
uart.write(&request)?;
// sleep (~ 1ms) to let device see that the pin is high and read data
direction_pin.set_low();
// sleep (~ 10ms) to let the device process request and send response
let read = uart.read(&mut uart_buffer)?;
modbus_request.parse_u16(&uart_buffer[..read], &mut response_buffer)?;
// use data in `response_buffer`
Of course, be sure to .clear()
the Vec
s and .fill(0)
the UART buffer before or after use.
from tokio-modbus.
@amreo, full disclouse, I ended up hacking this demo in Python. But I assume the uart instance is e.g. an instance of SerialPort from https://crates.io/crates/serial.
Does that help?
from tokio-modbus.
For anyone who finds themselves in a similar situation: I used rmodbus instead which is a bit more verbose, but lets me change pin voltage between writing and receiving over UART.
from tokio-modbus.
@ViliamVadocz, I'd also be interested in this. Could you by any chance share some (pseudo) code on how you used rmodbus to accomplish this?
from tokio-modbus.
Fantastic, thank you!!
from tokio-modbus.
Pseudocode:
let mut direction_pin = gpio.get(DIRECTION_PIN)?.into_output(); let mut modbus_request = ModbusRequest::new(SLAVE_ADDRESS, ModbusProto::Rtu); let mut request_buffer = Vec::new(); let mut response_buffer = Vec::new(); let mut uart_buffer = [0u8; 128]; // example: get registers modbus_request.generate_get_holdings(address, num_registers, &mut request_buffer)?; direction_pin.set_high(); uart.write(&request)?; // sleep (~ 1ms) to let device see that the pin is high and read data direction_pin.set_low(); // sleep (~ 10ms) to let the device process request and send response let read = uart.read(&mut uart_buffer)?; modbus_request.parse_u16(&uart_buffer[..read], &mut response_buffer)?; // use data in `response_buffer`Of course, be sure to
.clear()
theVec
s and.fill(0)
the UART buffer before or after use.
What is uart in this pseudocode?
from tokio-modbus.
Related Issues (20)
- "Device or resource busy" in RTU server example HOT 3
- Generalize impl around `Service` usage HOT 5
- Tokio module 'runtime' is private HOT 12
- Asyncronous responses / Multiplexing HOT 2
- How to convert tokio_modbus::Request to bytes? HOT 5
- TCP server example - shared data hashmap HOT 5
- TCP Sync Client HOT 6
- write_multiple_registers taking array as a param HOT 3
- Can not disconnect slave, need help HOT 3
- Although disconnect the context, the SerialStream reopen error HOT 1
- Patterns/best practice for reconnect-after-failure? HOT 1
- How to turn 'tokio_serial::SerialStream' into global static to save it for reuse
- is_connected()? HOT 1
- Dev Dependencies: Upgrade rustls-pemfile and tokio-rustls HOT 4
- Return Modbus exception codes for client and server.
- Use `async-trait` in `Service` trait ? HOT 10
- tokio-modbus 0.12 entering unreachable code in any response error HOT 7
- Dev Dependencies: Upgrade rustls
- How to get hexadecimal modbus rtu message data HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tokio-modbus.