Cross Platform RS485 Communication using C#

cross platform RS485 communication tutorial  for Linux and Windows Systems running on dot net and mono framework

In this tutorial we will learn to build and program a RS485 communication network between a x86 PC and a microcontroller board .The PC side software is written in CSharp (C#) and is targeted to run on both Linux and Windows operating systems.

On the Windows Platform the software runs on .NET Framework and on the Linux platform it runs on the open source equivalent called Mono Framework.

The software can be used as a starting point for controlling PLC's from your PC over RS485 link or developing SCADA software.

This tutorial requires the reader to be familiar with programming the serial port on Windows or Linux Systems using C# programming Language.It is recommended to read Serial Port Programming on Windows using C# (dotnet) or Serial Port Programming on Linux using C# (mono).


Circuit Diagram
Hardware Required
RS485 Section(USB2SERIAL)

RS485 Transmission
RS485 Transmission Code

RS485 Reception
RS485 Reception Code

Running the Code


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

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


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

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

In case you are using MSP430 Motor control Booster Pack (above image) ,you can use the built in MAX485 communication module for RS485 communication as shown in the figure below.The below image shows how to connect the booster pack with USB2SERIAL.

interfacing MSP430 Motor control Booster Pack with Windows/Linux PC using RS485 protocol


Most PC's do not have built in RS485 communication ports so for making your PC communicate with a RS485 network you will need a USB to RS485 converter.

In this tutorial ,I am using a FT232 based USB to RS485/RS232/Serial converter called USB2SERIAL.

USB2SERIAL uses FT232 and MAX 485 chip to convert the USB protocol from your PC to RS485 signal levels.The Board can also be used for communicating with Legacy RS232 devices.

USB to RS485 converter available online on ebay

USB2SERIAL is available online for Purchase.The usermanual can be found here.

USB2SERIAL RS485 Section

Programming the RS485 port of USB2SERIAL is similar to programming your Serial port.In RS485 programming the steps are similar to serial programming,here we will be using the RTS and DTR pins to control the direction of Data transmission (Transmit /Receive).

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

interfacing a x86 PC with RS485 network using USB to RS485 converter

RS485 communication code in C#

Now let's write the PC side code to communicate with the RS485 network.Programming the RS485 port is basically Serial Programming + RTS/DTR pin control,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.

Since Opening the Serial port and setting up the Baud rate,number of start/stop bits have already been explained in the serial programming using C# tutorial,I am not going to repeat it here.You can check it out here (Windows) and here (Linux).  

Transmitting a Byte through RS485 Link

The ~RTS and ~DTR pins of FT232 chip in USB2SERIAL are connected to the RE and DE pins of MAX485 chip through CONN2 ,these two pins (~RTS and ~DTR of FT232) are used to control the direction of transmission in the RS485 link.Please note that 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.

Now one thing to remember is that RTS and DTR signals coming from the FT232 chip are active low .

When you SET the RTS pin high using C# API ,~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 COMPort.DtrEnable = false;  so that ~DTR pin of FT232 is high enabling the DE pin of MAX485 (transmit mode). 
CLEAR the RTS pin
using COMPort.RtsEnable = false;,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.

 4 LED indicators of USB2SERIAL to show the status of MAX485 during USB to RS485 conversion

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. 

RS485 Transmission Code

Now here is a skeleton code for transmitting a byte (character 'A')  through the RS485 network .

using System;
using System.IO.Ports;

namespace USB2SERIAL_RS485_Write
 class RS485Write
   static void Main(string[] args)
     SerialPort COMPort = new SerialPort();//Create a SerialPort Object

     COMPort.PortName = "COM24";      // Assign the COM port number
     COMPort.BaudRate = 9600;         // Set Baud rate = 9600
     COMPort.DataBits = 8;            // Number of data bits = 8
     COMPort.Parity   = Parity.None;  // No parity
     COMPort.StopBits = StopBits.One; // One stop bit
     COMPort.Open();                  // Open the serial port
     //Put USB2SERIAL in Transmit Mode 
     COMPort.DtrEnable = false;       // Since DTR = 0.~DTR = 1 So  DE = 1 
     COMPort.RtsEnable = false;       // Since RTS = 0,~RTS = 1 So ~RE = 1

     COMPort.Write("A");              // Write  "A" to opened serial port

     COMPort.Close();                 // Close the Serial port

Please provide the name of the serial port corresponding to your system,

On Windows,   (here COM24 is port number in my system) 

COMPort.PortName = "COM24";      // Assign the COM port number

On Linux,   (here ttyUSB0 is port number in my system) 

COMPort.PortName = "/dev/ttyUSB0"; // Assign the COM port number

You can compile the code using Visual Studio,SharpDevelop or Mono Compiler (Linux).Please note that on Linux ,you should run the program as root user.

Receiving a string through RS485 Link

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.We will then use the ReadLine() Function to Read data and Display it on the console .

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

RS485 Reception Code

Now here is a skeleton code for receiving a string of characters through the RS485 network .The string "Hello from MSP430 \n" is send by the microcontroller connected to the RS485 network. 

using System;
using System.IO.Ports;//namespace containing SerialPort class

namespace USB2SERIAL_RS485_Read
  class RS485_Read
   static void Main(string[] args)
     string RxedData;// Variable for Holding received data
     SerialPort COMPort = new SerialPort();//Create a SerialPort Object 

     COMPort.PortName = "COM24";       // Assign the COM port number
     COMPort.BaudRate = 9600;          // Set Baud rate = 9600
     COMPort.DataBits = 8;             // Number of data bits = 8
     COMPort.Parity   = Parity.None;   // No parity
     COMPort.StopBits = StopBits.One;  // One stop bit

     COMPort.Open();                   // Open the serial port
     //Put USB2SERIAL in Receive Mode       
     COMPort.RtsEnable = true;         // Since RTS = 1, ~RTS = 0 So ~RE = 0 
     COMPort.DtrEnable = true;         // Since DTR = 1. ~DTR = 0 So  DE = 0 
     RxedData = COMPort.ReadLine();       // Wait for data reception
     Console.WriteLine("\n\t \" {0} \" ", RxedData)

Running the Code 

On Windows,You can compile the .cs files using either Microsoft Visual Studio or Sharpdevelop IDE .For performing RS485 communication between PC and Microcontroller make the circuit connection as shown in the above figure.If you are using RobotShield Booster Pack ,there is no need to build the MAX485 interfacing circuit as it is built in the Booster Pack itself. 

For Sending data from PC to Microcontroller through RS485 link,Download the MSP430 reception program onto the MSP430 microcontroller and then  compile and run RS485_Write_Win32.cs file using either Visual studio or Sharpdevelop IDE.The screenshots are given below.

 Cross platform RS485 communication between PC and Microcontroller using C sharp and Dot net framework

For Receiving data send from Microcontroller to the PC through RS485 link,Download the MSP430 transmission program onto the MSP430 microcontroller and then compile and run RS485_Read_Win32.cs file using either Visual studio or Sharpdevelop IDE.Reset the microcontroller so that it sends the string "Hello from MSP430 \n" to the PC .The screenshots are given below.

 Cross platform RS485 communication between PC and Microcontroller using C sharp and Dot net framework

On Linux,The .cs files can be compiled using Mono C Sharp compiler (mcs) and run using the Mono Runtime.Please note that the program requires root privileges to run.For more details on compiling C# files on Linux you can check this article

You can compile the c# files using the mcs compiler as shown below.

compiling RS485 communication program on Linux

If the program compiles correctly it will produce an exe file of the same name.You can then run the exe file using mono runtime as shown below.

running the  RS485 communication program on Linux

The below two screenshots shows the program reading and writing to RS485 port on a Linux System.

Cross platform RS485 communication between PC and Microcontroller on Linux

RS485 programming on a Linux system



Nice Tutorial

This is great websites, thanks for the tutorial