Cross Platform RS485 Communication using USB2SERIAL (USB to RS485 Converter) in C

Pinterest icon

This is a short tutorial on how to use the USB2SERIAL (USB to RS232/RS485) converter to implement a PC controlled RS485 (TIA/EIA-485) network.In this article you will learn to communicate with a microcontroller unit from an x86 PC running either Windows or Linux  through a twisted pair RS 485 network.

The tutorial teaches you to develop Cross platform PC side software that can communicate with a RS485 network using USB2SERIAL in C (Win32 API/Linux API).

Image showing a microcontroller (MSP430 ) interfaced with PC running Window or Linux using RS485 network

SourceCodes 

link to xanthium enterprises repo on Github containing code for RS485 communication using CSharp  

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

If you are new to Github Check this article to download code .

Introduction to RS485 

Sometimes while developing your projects ,you will come across a scenario where you have to transmit some data over long distances like controlling a robot over 20 or 30 meters or You have to interface your PC with a remote sensor monitoring a wind mill or has to control several sensors in a electrically noisy environment like a factory with several high power electrical devices or you want to control an under water robot diving several meters down.In all these case you can't use your standard RS232 serial or USB as their range is limited to few meters.

In these cases its better to use RS485 ( EIA-485​) protocol which has a long range (about 1.2KM) and built in noise immunity.The protocol can operate at speeds ranging from 35Mbit/s​ (upto 10 meters) to 100Kbit/s (upto1.2KM).RS485 is used widely in factory automation(PLC control,SCADA),Home automation,lighting control systems,Pan Tilt Control in CCTV system and also for controlling Model Railway sets.It is also used to implement the physical layer in many industrial automation protocols like Modbus and Profibus.

One problem with PC's is that it lacks any RS485 ports,but that can be easily solved by using USB to Serial Converters like USB2SERIAL.USB2SERIAL can be used as a USB to RS485 converter as explained in this tutorial,USB to RS232 level Converter,USB to Serial (TTL) Converters with selectable 3.3V and 5V signal Levels and as a Development board for the FT232 chip using the D2XX library

photo of USB2SERIAL USB to RS485 converter

Hardware used

USB2SERIAL uses a FT232 based USB to Serial converter chip to convert the USB sata into serial stream (TTL level 3.3V/ 5V) which is then converted to either RS232 signal levels or RS485 Signal levels depending upon the jumper settings (CONN1).The ~RTS and ~DTR signals are used to put the MAX485 chip ( RS 485 part ) in either Receive Mode or Transmit Mode.Please note that the ~RTS and ~DTR signals of FT232 are both active low .Below figure shows the block diagram of RS485 part of the USB2SERIAL board (RS232 section is omitted for simplicity) . 

block diagram of USB2SERIAL (usb to RS485) converter highlighting the RS485 mode

The figure below shows the jumper settings of USB2SERIAL V2.0 board for RS485 mode.

 jumper settings of USB2SERIAL V2.0 board for RS485 mode

 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). 

FT232 MAX485 connections for USB to RS485 conversion in USB2SERIAL

USB2SERIAL also has an option to control the ~RE and DE pin of MAX485 using CBUS1 and CBUS2 pins of FT232 which require the use of D2XX library.In this article we will  use ~RTS and ~DTR pins to control the MAX485 chip

Please note that active low signals are prefixed with a tilde character ~  instead of an over line in the text.

Software Development

Developing software to communicate over an RS485 line using USB2SERIAL is quite simple,RS485 programming is basically Serial programming + MAX485 direction(Transmit/Receive) control.Since most of the programming parts are basically serial programming which have already been explained at this website,So I am not going to repeat it instead i will provide links to the relevant sections.The below list shows the supported platforms and languages used.

For developing the software,choose the platform (Windows\Linux\Mac) on which you are going to run your code and select the language (C ,C#,Python) for developing the software.Please make sure that the language you selected is supported  by the Platform(OS) using the list given above.If you want to use the same code base for multiple operating systems go for Python.If you don't want to release the source code of your application,its better to go for C or C#.

For eg: lets say you want to develop the software in Python on a Linux/Windows system,first thing to do is to read the serial programming article in Python then come here and go through  this article.This way its much easier for you to understand,same goes through for all other languages and platforms.You can check other serial programming articles here.

Programming the RS485 link on x86 PC

As i have already mentioned ,programming a RS485 link to communicate with an embedded system like a MSP430 microcontroller board is similar to programming a serial port  to do the same but with an added task of configuring the MAX485 in transmit or receive mode .Most of the intricacies of programming thye serial port on Window and Linux using C,C Sharp and Python are already covered in various articles.In this article I will only tell about configuring the RS485 part along with links to the relevant parts of the serial programming articles.

Transmiting a Byte through RS485 Link 
In this section we will learn to transmit a byte through the RS485 network using USB2SERIAL.The Program running on the PC side will transmit a byte (more specifically ascii 'A') to a MSP430 microcontroller board through a RS485 link.The microcontroller board receives the character and lights up an LED indicating that a byte has been received.

The  diagram for connecting the USB2SERIAL with the MSP430 Launchpad board are shown below.

connecting the USB2SERIAL with the MSP430 Launchpad board for RS485 communication

There is a detailed explanation of interfacing MAX485 RS485 tranciever chip with MSP430 Launchpad (MSP430G2553) .Please note that you can use any microcontroller like 8051, LPC2148 (ARM7,NXP) or ATmega8535 (Atmel) instead of MSP430.If you are new to MSP430,you can check out a brief tutorial about using MSP430 Launchpad.There is  also a detailed explanation about configuring the MSP430G2553 UART.

Transmitting a byte through RS485 Link (PC side) involves the following steps

1) Identifying the serial port name and number on your chosen platform(Windows/Linux)
2) Opening a connection between the chosen serial port and your program.
3) Configuring the baud rate,number of start/stop bits,number of data bits ,parity bits etc of the serial port.
4) Controlling the RTS and DTR lines to put USB2SERIAL in transmit mode.
5) Write a byte to the serial port.
6) Close the connection to the serial port

​Identifying the serial port 
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.So the first step involves identifying the correct serial port and serial port number for communicating with USB2SERIAL(USB to RS485 converter).Serial ports are designated different name and numbers indifferent operating systems like  Windows and Linux.You can check out the below links to correctly identify the serial port names in your OS.

Finding out COM port Numbers in Windows
Finding out Serial port name and number in Linux  

 

Opening a connection to Serial port.
Next step involves opening a connection to the serial port on your OS so that the program you are developing can talk to the port.The process of opening a connection differs widely across different operating systems,for example Windows uses CreateFile() function to open the port and uses Handles to communicate with it after that while Linux uses open() system call to open the port and used File descriptors(fd) to communicate with it.

Opening a connection to Serial port using Win32 API
Opening a connection to Serial port  in Linux

 

Configuring the Serial Port Settings 
In this step we will configure the basic settings like baud rate,number of start/stop bits,number of data bits ,parity bits of the opened serial port.

In windows,If you are using Win32 API ,the serial port is configured by filling out a structure named as DCB.
In Linux the settings are configured by filling out the termios structure.

 Configuring the Serial Port Settings using Win32 API
 Configuring the Serial Port Settings in Linux.

Controlling the RTS and DTR lines
In this section we will use the DTR and RTS lines(of FT232 ic in the USB2SERIAL) to control the Receive and transmit modes of MAX485(RS485 ic) ic.MAX 485 is a half duplex RS485 transceiver which means that it can only transmit or receive at a time,not both at same time. If you check the block diagram of USB2SERIAL you can see that ~RTS (active low) and ~DTR (active low) pins of FT232 are used to put the MAX485 chip in Transmit or receive mode.You can check out the below links to learn ,how to control the RTS and DTR lines on Windows and Linux OS's.

Controlling RTS and DTR pins using Win32 API
Controlling RTS and DTR pins in Linux

Now one thing to remenber is that RTS and DTR signals coming from the FT232 chip are active low .When you SET the RTS pin high using C ,~RTS line from FT232 will become LOW (same for ~DTR).When you CLEAR the RTS pins using C API,~RTS line will become high.

 

Putting MAX485 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.

To put the chip in transmit mode,

CLEAR the DTR pin using any one of the Programming API's(like EscapeCommFunction() in Win32 or ComPort.setDTR(0) in PySerial) so that ~DTR pin of FT232 is high enabling the DE pin of MAX485 (transmit mode). 
CLEAR the RTS pin,So that ~RTS pin of FT232 is high disabling the ~RE pin of MAX485 (receiver mode is disabled).

Now in USB2SERIAL board there are 4 LED lights near the DB9 Male connector for indicating the status of DI,DE,~RE (active Low) and RO pins of MAX485.

Photo of 4 LED indicators of USB2SERIAL to show the status of MAX485

When you connect the USB2SERIAL board to your PC (jumpers are set in RS485 mode) , All the LED's except RO will be ON.

When a connection to the serial port is opened DE and ~RE LED's will go OFF while the other two(RO and DI) will light up.Now when you CLEAR the DTR pin you can see the DE LED lighting up and on clearing the RTS pin you can see the ~RE LED lighting up.This makes it quite easy to check whether your RTS and DTR control code is working properly.

Please note that if you run the codes provided along with this tutorial,you won't be able to see lights changing as it happens quite fast.One way to check this is to put breakpoints in the code. 

Transmitting a Byte through RS485 network
After the MAX485 in the USB2SERIAL is put in transmit mode by making both the ~RTS and ~DTR pins high as described above .Now to send the data through the RS485 cable we will write a byte into the virtual serial port of USB2SERIAL using anyone of the API's provided like WriteFile() for Windows or write() for Linux. Depending on the language and code you can use anyone of write functions provided below.

Writing a Byte using Win32 API in C (Windows)
Writing a Byte using Linux System Calls in C (Linux)

Closing the Serial port
After the port is used for transmitiing or receiving a byte it should always be closed.If you do not close a serial port,the port will not available to other programs which may misbehave or crash .Closing the serial port is already covered so no links are provided.

 

Receiving a Byte through the RS485 link

In this section we will learn to receive a byte send by the microcontroller board ,in this case MSP430 launchpad(MSP430G2553).you can check the connections here.You can use the same program for transmitting and receiving,her i am writing a separate program so that you can understand the concept easily.The program is similar to the one explained above instead of the transmitting part, we are going to wait till a character is received.

Receiving a byte through RS485 Link (PC side) involves the following steps

1) Identifying the serial port name and number on your chosen platform(Windows/Linux)
2) Opening a connection between the chosen serial port and your program.
3) Configuring the baud rate,number of start/stop bits,number of data bits ,parity bits etc of the serial port.
4) Controlling the RTS and DTR lines to put USB2SERIAL in Receive mode.
5) Wait till a byte is received by the PC serial port.
6) Close the connection to the serial port

 

Putting MAX485 in Receive Mode

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).

To put the chip in receive mode,

SET the RTS pin (using either EscapeCommFunction() in Win32 or ComPort.setRTS(1) in PySerial) ,So that ~RTS pin of FT232 is HIGH enabling the ~RE pin of MAX485 (receiver mode is enabled). The reason we are setting RTS pin is because RTS pin inverted internally in FT232 chip.

SET the DTR Pin so that ~DTR pin is LOW ,which will disable DE and inhibit transmit mode of MAX485.

THE ~RE and DE LED's on THE USB2SERIAL board will be off after putting the board in receive mode.

 

Reading a Byte from RS485 network
Configuring your program(PC side) to wait for a data byte varies across platforms and languages .

In the case of Windows,
If you are using C,you have to configure and register an Event with Windows so that OS will notify you when a data is available in the PC serial buffer.After the notification you can read the data using ReadFile() function.Please dont forget to set the timeouts otherwise data may be corrupted.

In Linux it is achieved by using read() system call.You can check out the below links for reading a byte from your serial port buffer.

Reading data from Serial port using Win32 API
Reading data from Serial port Linux System calls

Screenshots 

Windows(using Win32 API)(C language)

The RS485 reception program waiting for data to be send from the microcontroller board.

The RS485 reception program waiting for data to be send from the microcontroller board

After the microcontroller sends the string "Hello from MSP430".It is displayed on the console.

Win32 c program running on windows displaying the data received through RS485 link

On Linux using native API's (termios/ioctl())

On Linux ,user programs are not allowed to access the serial port so you must run your programs as root using sudo command.

The below image shows RS485 program running on a PC(Linux Mint /Ubuntu)  sending data to the Microcontroller board.First line in the below image shows compiling the program using gcc.Second line uses sudo to run the program as root.

The image below shows the data being received by the PC on a Linux system.

image  shows the data being received by the PC on a Linux system through RS485

Using Python 

The zip files contains the RS485 communication program in both Python 2.7.x and Python 3.x.x variations.Some of the Linux distributions are still shipping with Python 2.7.x while in windows it is recommended to use Python 3.0.Please note that Python 2.7.x and Python 3.x.x are not compatible.In Linux Please run the python program as root using sudo.

The below figure shows an RS485 reception program (in Python 3.4) executing on Windows 7.

program written in python3.x receiving data through a RS485 communication channel

The below figure shows data received by RS485 communication program written in Python 2.7.x executing on an Ubuntu Linux PC.

data received by RS485 communication program written in Python 2.7.x executing on an Ubuntu Linux PC

Source Codes for RS485 Communication

The source codes for RS485 communication on windows and Linux can be downloaded below.The Microcontroller code (C file + Workspace) is also bundled with each download.Microcontroller code is written in embedded C for MSP430G2553 and compiled using IAR EW.Support for Python 2.7.x as well as Python 3.x is provided.

RS485 Communication using Win32 API (C language) (Windows)
 

RS485 Communication using C on Linux (Linux 3.x.x)(Ubuntu/Mint Linux)

RS485 Communication using Python (Windows / Linux)

USB2SERIAL can be purchased here.