Example Code for Arduino-RS232 Communication Test

Last revision 2025/12/08

This article explains how to conduct a communication test between Arduino microcontrollers and RS232 devices using an RS232 shield and a USB-RS232 converter. It includes detailed hardware and software preparation, wiring diagrams, and a sample code for toggling an LED based on received serial data.

Hardware Preparation

Software Preparation

Wiring Diagram

Connection Diagram

Step Sequence Connection Operation
1 Plug the RS232 Shield into the Arduino microcontroller
2 Connect the DB9 male connector of the USB-RS232 converter to the DB9 female connector of the RS232 Shield

Sample Code

# This sample codes is for testing the RS232 shiled.
# Editor : YouYou
# Date   : 2013.9.25
# Ver    : 0.1
# Product: RS232 shield
# SKU    : DFR0259
*/
int led = 13;    //define the LED pin 
void setup()
{
  Serial.begin(9600);    //init serial
  pinMode(led,OUTPUT);
}
void loop()
{
  int temp;    //serial data temporary cache 
  if(Serial.available())    //if serial receives data
  {
    temp=Serial.read();    //store the received data temporarily
     if(temp=='V'){
      digitalWrite(led,1-digitalRead(led));    //change the LED statu if receiving the char "V".
    Serial.println("OK");    //reply OK to show that the char "V" has been received and the LED statu also has been changed 
     }
  }
}

Result

Open the Serial debugger, find the COM related with USB-RS232, send a char "V", then the LED statu will be changed and it will reply "OK".

Result

Was this article helpful?

TOP