Especially, Arduino does NOT use UART pins to communicate with RS422/RS485 expansion board. Therefore, users can use Arduino UART pins for other purposes.
Moreover, A single Arduino Uno/Mega can communicate with multiple RS422/RS485 expansion boards (up to 14) without using Arduino UART pins.
Library and examples for the RS422/RS485 expansion board are part of of PhpocExpansion library for Arduino. The library reference is available here
The examples for the RS422/RS485 expansion board includes two examples: one for exchanging data between Arduino and Serial device via RS-422, the other one is for exchanging data between Arduino and Serial device via RS-485.
This tutorial explains how Arduino exchanges data with Serial Device via RS-422 using RS422/RS485 expansion board.
Application
Testing
RS422/RS485 Expansion Board is designed to let Arduino exchange data with serial device via RS-422 or RS-485. For testing, we can use PC or laptop as a serial device. Although most of PC and laptop does not have RS422/RS485 COM port, we still can do the test by using USB-to-RS422 converter cable (or USB-RS232 cable + RS232-to-RS422 converter).
Hardware Required
- Arduino Uno or Mega
- USB Cable for Arduino.
- PHPoC Shield R2 or PHPoC WiFi Shield R2.
- RS422/RS485 Expansion Board - PES-2607.
- USB-to-RS422 converter cable (or USB-RS232 cable + RS232-to-RS422 converter)
Note that: in the test, we use two cables: one is USB cable that connects PC to Arduino, the other cable is RS232-to-USB cable that connects PC to RS422/RS485 expansion board for testing. These cables connect to PC with different COM ports.
You can check these COM Port by accessing "Device Manager" on your PC. It looks like below image.
Note that the COM port number is depending on each PC.
Wiring
- Stack PHPoC Shield or PHPoC WiFi Shield on Arduino.
- Stack RS422/RS485 expansion board PES-2607 on PHPoC Shield or PHPoC WiFi Shield.
- Connect Arduino to PC via usb cable.
- Connect USB-to-RS422 converter to expansion board and your PC.

Install Arduino IDE
If you have not install Arduino IDE yet, please download and install Arduino IDE .
Install Library
- Run Arduino IDE.
- Navigate to Sketch > Include Library > Manage Libraries
- Search "Phpoc Expansion" on search bar of the Library Manager.
- Select the PhpocExpansion library and press the [Install] button.
- Restart Arduino IDE for the next step.
Source Code
- Run Arduino IDE.
- Open "ExpansionSerialRS422" example.
- Compile the example code and uploads to Arduino by clicking "Upload" button on Arduino IDE.
- Run a Serial software such as TeraTerm, connect to COM port corresponding to RS422-to-USB cable.
- Config serial parameters on TeraTerm
- Open "Serial Monitor" tool on Arduino IDE.
- Send some data from TeraTerm to RS422/RS485 Expansion Board by typing some characters in TeraTerm.
- We can see that, data is shown on both TeraTerm and Serial Monitor.
Note that: By default, TeraTerm does NOT display the data that it sends, it only displays data that it receives from serial.
Therefore, data that is displayed in screen is echo data from Arduino
Code Explanation
This code does:
- Reads data from RS422/RS485 expansion boards.
- Sends back the received data to RS422/RS485 expansion boards and prints the received to Serial Monitor.
At the beginning
- We need to create a ExpansionSerial object:
Code:ExpansionSerial rs422(expansionId);
You can change it and map it to the below to know the value.
- We need to create a buffer to keep the received data:
Code:byte rwbuf[BUFFER_SIZE];
Note that: RS422/RS485 expansion board is one of the expansion boards. List of expansion for Arduino is available here
In setup() function
- Phpoc.begin() and Expansion.begin() functions must be used to initial PHPoC [WiFi] Shield and expansion board, repectively.
- Serial.println(in0.getName()) function is used optionally to get name of expansion board and print it to serial.
- rs422.begin(F("115200N81N")) function is used to set baud-rate, parity, data bit, stop bit and flow control to 115200, none, 8 bit, 1 bit and no flow control, respectively.
In loop() function
There are some functions are used.
- rs422.available(): get the size of received data in the receiving buffer of RS422/RS485 expansion board.
- rs422.availableForWrite(): get the size of free space in the sending buffer of RS422/RS485 expansion board.
- rs422.readBytes(rwbuf, rwlen): receive data from the receiving buffer of RS422/RS485 expansion board and save it to the application buffer.
- rs422.write(rwbuf, rwlen): send data from the application buffer to the sending buffer of RS422/RS485 expansion board.
Data is read from the receiving buffer of RS422/RS485 expansion board and save to application buffer, and then write to the sending buffer of RS422/RS485 expansion board.
Firstly, we get the size of received data in receiving buffer of RS422/RS485 expansion board.
Code:
// get the size of the received data in the receiving buffer of RS422/RS485 expansion board int rxlen = rs422.available();
If data is available in the receiving buffer of RS422/RS485 expansion board, get the size of free space in the sending buffer of RS422/RS485 expansion board.
Code:
// read and write length int rwlen; // get the size of free space in the sending buffer of RS422/RS485 expansion board int txfree = rs422.availableForWrite();
If we read an amount of data that exceed capacity of application buffer, application buffer will be overflowed and data is lost.
Therefore, before reading data, we need to make sure that the the reading data does not exceed the application buffer capacity.
Code:
// check to make sure that read data does not exceed size of application buffer rwlen = min(rxlen, BUFFER_SIZE);
All data that stored in application buffer will be written to the sending buffer of of RS422/RS485 expansion board. If we write an amount of data that exceed the free space of the sending buffer of RS422/RS485 expansion board, the sending buffer of RS422/RS485 expansion board will be overflowed and data is lost. Therefore, before reading data and store data to application buffer, we need to make sure that the the reading data does not exceed the free space of the sending buffer of RS422/RS485 expansion board.
Code:
// check to make sure that write data does not exceed free space in the sending buffer of RS422/RS485 expansion board rwlen = min(rwlen, txfree);
Reading data from the receiving buffer of RS422/RS485 expansion board and save to application buffer.
Code:
// receive data from the receiving buffer of RS422/RS485 expansion board and save it to the application buffer rwlen = rs422.readBytes(rwbuf, rwlen);
Sending data from the application buffer to the sending buffer of RS422/RS485 expansion board
Code:
// send data from the application buffer to the sending buffer of RS422/RS485 expansion board rs422.write(rwbuf, rwlen);
Printing data to Serial Monitor of Arduino IDE
Code:
// print data to serial monitor of Arduino IDE Serial.write(rwbuf, rwlen);
See Also
- Arduino - Relay Expansion Board - PES-2601
- Arduino - Digital Input Expansion Board - PES-2602
- Arduino - DC Motor Controller - PES-2604
- Arduino - Stepper Motor Controller - PES-2605
- Arduino - RS-232 Expansion Board - PES-2606
- Arduino - RS-422 Expansion Board - PES-2607
- Arduino - RS-485 Expansion Board - PES-2607
References