Great, you managed the 'hello world' example.I've managed to send and retrieve some simple data between the master and slave.
When the Pi4 (master) sends out a query, it needs to wait for response. For first tests, make this wait time sufficient long.
- transfer time of serial data on the wire
- time to switch the RS485 receiver from send to receive
- grant some 'wakeup- and think time' for the clients
- and the transfer time of serial data on the wire for the response
- and some extra safety margin
- prepare for timeout checks, clients may not respond.
The pico (slave) need to poll for incoming messages. All the time. When coding in micropython it is not a simple task to handle other jobs like reading a touch panel 'in parallel'.
- threading is still not perfect.
- asyncio could help to implement multiple tasks.
- a while loop in micropython could first look for incoming uart data, and if none are available to other jobs. There is a receive buffer in the uart class which lowers timing requirements. Do not use uart.readline(). What you need to implement is a robust message detection in the uart byte stream coming in. When using modbus libraries this is handled there, otherwise do this yourself.
Code:
# pseudocode for a repeating background task each 200 mst0 = time.ticks_ms()while True: if uart.any(): c = uart.read(1) - collect all chars until a message is complete. - could be the client starts to listen in the middle of an incoming query (after a reset of an coding error). - if message is complete, handle the query and send response else: t1 = time.ticks_ms() if time.ticks_diff(t1, t0) > 200: t0 = t1 - do other jobs, but do not spend too much time here - store result to be available when a query arrives
As with RS485 you already have GND, A,B wires, you could also provide an extra VCC line to the clients (but be aware of shorts, EMF, line losses so a careful design is needed).
Statistics: Posted by ghp — Fri Nov 15, 2024 9:01 am