9 years, 6 months ago.

RS485 communication

Hi, I want to connect my external device with rs485 interface to mbed microcontroller, can any one help me out how can I connect them ?? I know the standard serial ports can be used to perform the required task, but I need some suggestions that how rs485 is different from the other serial communication, and some sample piece of code as well.

thanks in advance

1 Answer

9 years, 6 months ago.

Rs485 specifies the physical connection (voltages etc.). It is not a protocol. RS485 is typically used for half duplex serial data. Half duplex means that it can both send and receive but not at the same time. This means that you have to switch the direction of the transceiver when you change direction. A typical transceiver like the MAX485 has of course the balanced input/output to connect to the RS485 bus, Rx and Tx connections which go to the serial connections of the MBed and 2 control lines for enabling the receiver and the transmitter, these controls have opposite polarity so you can connect then together to a single output with which you control the direction. High is transmit, low is receive. As you are working half duplex you will typically be using a master slave system in which the master sends a request while the slaves listen, after the request the master switch to receive mode and the addressed slave switches to transmit mode and sends a reply. After the reply or a timeout all devices go back to listening again. So your send routine should set the transceiver to transmit mode, send the request or reply, wait for the request or reply to complete and switch back to listening mode. The tricky part is the waiting for the end of the request/reply, you can calculate the duration of a message and set a timer or use a flag in the UART. The normal serial routine returns to the caller after the character has been placed in the fifo, the character has not been send yet! switching direction at this point will truncate the character. A better way is to use the transmit register empty flag in the UART status register. This flag is set once the character has been completely shifted out. The MBed serial library unfortunately does not make this flag available so you have to access that register directly. Using it in an interrupt is complicated so it is better to use busy waiting.