please, I need your help: I would like to use three ARDUINO NANO to create a small RS485 network, with one MASTER and two SLAVES in HALF DUPLEX.
As an interface each NANO will have a MAX485, connected to pins D0 and D1 for RX TX, and to D4 as RS485 Data direction (TE transmission Enable). The question is: how can I read the UART register to know when the transmission of last bit is over, and immediately switch the RS485 to receive? Thank you very much.

Please correct the topic name (RT to RS) .

Basically you can use


which holds up the processor until the last byte has been moved from the buffer into the FIFO.
Of course once the last byte has moved to the FIFO, you should still wait the time it takes to transmit 2 more bytes, There is 1 still in the output register, and 1 still in the FIFO. Depending on the serial format you can calculate how long it will take. Standard 8N1 consists of 10 bits. 1 start, 8 data & 1 stop bit. Depending on the baud rate it is being sent at. Say for 9600bps, it comes down to about 1ms per byte. Best practice in that case would be to wait for 3ms, so it would turn out to be something like

digitalWrite(TE_PIN, LOW);

You can do the whole thing 'non-blocking' as well, by reading the size of the output buffer (check hardwareSerial.h for how the flush() function is implemented), and doing the wait for the final 2 bytes, using millis() or micros(), rather than using delay().
You will also have to make sure that any slave does not start responding to soon, and that the Master waits for a specified time in which it waits for a response. Slave should only be able to send data upon request, and only within the period of time that the Master is waiting for the response.
You want to make sure that there is not 2 MAX485's in drive mode at the same time.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.