RS485 Programming using Python and USB to RS485 Converter

cross-platform RS485 programming on windows and linux using Python

In this tutorial we will learn to communicate with a microcontroller like 8051 or MSP430 from an x86 PC (Linux/Windows) using RS485 Protocol.Here the software running on PC will send commands to a microcontroller board through twisted pair RS485 cable and control the peripherals connected to the microcontroller. 

The control software running on the PC is written in Python and uses the PySerial module for RS485 communication.A USB to RS485 converter is used to convert the data send by the PC to RS485 compatible signals.On the receiver side a MSP430 Launchpad is interfaced with a MAX485 chip for decoding the signals.

If you are new to PySerial Module ,You can find a short tutorial on using PySerial here


link to xanthium enterprises repo on Github containing code for serial communication between PC and Micro Controller using Visual  

All the Python source files used in this tutorial can be downloaded from our GitHub Page.

Sourcecodes are available for both Python 2.7.x version and Python 3.x.x version.The codes are available for both Windows and Linux Versions .

Circuit Diagram

Here we are going to interface a microcontroller board with an x86 PC running Windows or Linux Operating System through a RS485 link.On the microcontroller side we are using MSP430 Launchpad connected to MAX485 RS485 decoder chip.On the PC side we are using USB2SERIAL USB to RS485 converter which will convert the data send from the PC to RS485 compatable signal levels.The figure below shows the connections between MSP430 and PC.

interfacing micro controller with windows/Linux PC through RS485 protocol using USB2SERIAL USB to RS485 converter

MSP430 Launchpad interfaced with PC using RS485 protocol

Hardware Used

programming USB to RS 485 converter using Python and PySerial Buy USB to RS485 converter online from India 

On the PC side we are using USB2SERIAL V2.0 USB to RS485 converter for converting the USB signals to RS485 differential signals.

USB2SERIAL can also convert from USB to Serial,USB to RS232 and USB to Parallal (Using D2XX Library).

You can buy USB2SERIAL (USB to RS485 Converter) online from here

In USB2SERIAL, USB to RS485 converter a FT232RL chip is interfaced with a MAX485 chip to convert the USB signals to RS485 balanced line signals.The figure below shows the RS485 section of the USB2SERIAL (USB to RS485 converter).

RS485 section of USB2SERIAL USB to RS485 converter

Moving the CONN1 jumpers (Red Squares) towards the RS485 side will connect the TXD of FT232 to DI pin of MAX485 (Red Line )and the RXD of FT232 to RO pin of MAX485 (Green Line).Data send from the PC is given to DI pin of MAX485 and then transmitted over the RS485 link.The data received by MAX485 through the RS485 link is sent back to the PC using the RXD pin of FT232.

The ~DTR and ~RTS pins of FT232 (both active low) are connected to the DE and ~RE (active low) pins of MAX485 through the CONN2 jumpers. The ~DTR pin of the FT232 is used to put MAX485 in transmit mode while ~RTS pin is used to put the MAX485 chip in Receive mode.

When you are using USB2SERIAL USB to RS485 converter,the board should be put in RS485 mode by selecting the jumpers as shown in the below figure.

setting the RS485 mode in USB2SERIAL V2.0 USB to RS485 Converter

After the jumper settings for RS485 mode are configured,

TXD output (TTL) of FT232 is connected to DI pin of MAX485.(through CONN1 Jumper)
RXD input (TTL) of FT232 is connected to RO pin of MAX485.(through CONN1 Jumper)

~RTS (active low) pin of FT232 is connected to ~RE (active low) pin of MAX485 (through CONN2 Jumper).
~DTR (active low) 
pin of FT232 is connected to  DE pin of  MAX485 (through CONN2 Jumper).

Code Explanation

RS485 programming is quite similar to serial programming here we will be using the RTS and DTR pins of FT232 to control the RO and DI pins of MAX485 chip.

Basic steps for programming the RS485 port are outlined below .

1) Identify the COM port number of USB2SERIAL
2) Open the Serial Port
3) Set the parameters like Baudrate,Start/Stop bits
4) Put the USB2SERIAL board in Transmit or Receive Mode
5) Send Data or wait for data reception 
6) Close the Serial Port

First step in Programming your RS485 port is to identify the port number corresponding to your USB2SERIAL board.In case you are wondering why we are talking about serial port numbers when we are discussing RS485 communication,this is because USB2SERIAL appears a virtual serial port to the Operating System(Windows /Linux ).Data is first sent to the virtual serial port and then into the MAX485 RS485 converter.

On Windows this can be found out by checking the Device manager.On Linux you can check the /dev directory.I have already written a detailed article on finding out serial port numbers on Linux here.

The steps 2 and 3 opening the serial port and configuring the parameters have already being explained in the serial programming article using pyserial and python.


Transmitting a Byte using USB to RS485 converter

First thing to do is to put MAX485 in the USB2SERIAL board in transmit mode.MAX485 can be put in transmit mode by holding the DE pin high and ~RE pin low.As i have already mentioned ~RTS is connected to ~RE pin of MAX485 and ~DTR pin is connected to the DE.This can be done by the following lines of code (Python3.0) .

#Controlling DTR and RTS pins to put USB2SERIAL in transmit mode

COM_Port.setDTR(0) #DTR=0,~DTR=1 so DE = 1,Transmit mode enabled
COM_Port.setRTS(0) #RTS=0,~RTS=1 (In FT232 RTS and DTR pins are inverted)

print('\n    DTR = 0,~DTR = 1 so DE = 1,Transmit mode enabled')
print('    RTS = 0,~RTS = 1')
# Write character 'A' to serial port                            
data = bytearray(b'A')                # Convert Character to byte array 
NoOfBytes  = COM_Port.write(data)     # Write data to serial port

print('\n   ',NoOfBytes,' bytes written')
print('\n    A written to',COM_PortName)
COM_Port.close()                       # Close the Serial port

The full code in Python 3.x and Python 2.7.x can be downloaded from our software repository. 

Screenshot of the program running on Linux is shown below.

RS485 programming using python,pyserial and usb to rs485 converter

Receiving a Byte using USB to RS485 converter

To Receive a string,we will put USB2SERIAL in receive mode and wait till the microcontroller on the other end transmits a string of characters.MAX485 can be put in Receive mode by holding the ~RE pin LOW and DE pin low.(~RTS is connected to ~RE pin of MAX485 and ~DTR pin is connected to the DE).This can be done by the following lines of code (Python3.0) .

#Controlling DTR and RTS pins to put USB2SERIAL in Receive mode

COM_Port.setRTS(1) #RTS=1,~RTS=0 so ~RE=0,Receive mode enabled for MAX485
COM_Port.setDTR(1) #DTR=1,~DTR=0 so  DE=0,(In FT232 RTS and DTR pins are inverted)
                   #~RE and DE LED's on USB2SERIAL board will be off

print('\n    DTR = 1,~DTR = 0 so  DE = 0')
print('    RTS = 1,~RTS = 0 so ~RE = 0,Receive mode enabled for MAX485')

print('\n    Waiting for data.....\n')

RxedData = COM_Port.readline() 
print('   ',RxedData, '\n')

COM_Port.close()                       # Close the Serial port

The full code in Python 3.x and Python 2.7.x can be downloaded from our software repository. 


Screenshot of the Program running on Windows

RS485 programming on Windows using Python and USB to RS485 converter

Screenshots of the program running on Linux 

USB to RS485 converter receiving data on Linux using Python and Pyserial