RS485
The article provides an example of RS485 serial data transmission using AccessPort software and an ESP32 IoT Programmable Controller, demonstrating how to send and receive data with a USB/RS485/TTL protocol converter.
8.3 RS485 Communication
Example: RS485 Serial Data Transmission
In this example, we use AccessPort software as the debugging tool for sending and receiving serial data.
After uploading the code to a development board, use any USB/RS485/TTL protocol converter to send data to the board.
Hardware Requirements:
- ESP32 IoT Programmable Controller (SKU: DFR0886)×1
- USB/RS485/TTL Protocol Converter (SKU: TEL0070)×1
Example Code:
The program transmits the data received from the USB serial port via the RS485 interface and simultaneously receives RS485 data and sends it out via the USB serial port.
#include <ArduinoRS485.h>
RS485Class RS485;
void setup() {
Serial.begin(9600);
RS485.begin(9600); // Initialize RS485 with a baud rate of 9600
}
bool flag = false;
void loop() {
// If there is data available on the serial port
while (Serial.available()) {
if (!flag) flag = true; // Set the flag to true, indicating the start of transmission
if (flag) RS485.beginTransmission(); // Start RS485 transmission
RS485.write((char)Serial.read()); // Read data from the serial port and send it via RS485
}
// If transmission has started, end RS485 transmission
if (flag) RS485.endTransmission();
flag = false; // Reset the flag
// If there is data available on RS485
while (RS485.available()) {
Serial.write(RS485.read()); // Read data from RS485 and send it to the serial port
}
}
After uploading the program, assume the USB serial port of the development board is COM7.
Connect a USB/RS485/TTL protocol converter to the RS485 interface on the development board, and the converter's USB is connected to the computer. This will trigger the appearance of a USB serial port, COM8.
When the string "Data sent by motherboard" is entered into the development board's USB serial port (COM7), the RS485 converter at COM8 receives the transmitted string.
Similarly, when the string "Data sent by RS485 device" is entered into the RS485 converter's COM8, the development board's USB serial port (COM7) will receive the transmitted string.

Was this article helpful?
