Read RS232 data on arduino

I am using an radar which is based on rs232 output data. I use MAX232 to TTL convertor with arduino. Radar givs data in 14 byte in hex i want to read it on serial monitor. what can i do ,Below i use sofwareserial.h library to read data but failed

#include <SoftwareSerial.h>
const int RX_PIN = 2; // Replace with your actual RX pin
const int TX_PIN = 3; // Replace with your actual TX pin
SoftwareSerial mySerial(RX_PIN, TX_PIN);

void setup() {
 Serial.begin(115200);      // Serial monitor for debugging
 mySerial.begin(115200);    // SoftwareSerial for connecting to the MAX232
}

void loop() {
 // Check for and print the raw data from the radar
 printRawRadarData();
}

void printRawRadarData() {
 // Check if data is available from the MAX232
 while (mySerial.available()) {
   char receivedChar = mySerial.read();
   Serial.print(receivedChar,HEX);
 }
}

Your topic has been moved to the Programming category of the forum. Please take care when deciding where to start new topics

this won't even compile if you don't define RX_PIN

please post your circuit and provide information like which arduino you are using etc

Does your radar use 115200 bauds?

How does it fail?

This is far to high for SoftwareSerial; 19200 is safe, 38400 might still work.

Which board are you using?

If your radar serial output is really at 115200 baud, then you need to be looking at an Arduino board with more than 1 hardware serial port. Maybe a MEGA2560 based board instead.

1 Like

oh sorry i am also using rx pin , tx and rx is 2,3 i have also tried 0,1_10,11 but did not get any response .yes radar gives data on 115200 baud rate

#include <SoftwareSerial.h>

const int RX_PIN = 2; // Replace with your actual RX pin
const int TX_PIN = 3; // Replace with your actual TX pin
SoftwareSerial mySerial(RX_PIN, TX_PIN);

void setup() {
  Serial.begin(115200);      // Serial monitor for debugging
  mySerial.begin(115200);    // SoftwareSerial for connecting to the MAX232

  // Send the "read radar parameter" instruction
  sendReadRadarParameterInstruction();
}

void loop() {
  // Check for and print the raw data from the radar
  printRawRadarData();
}

void sendReadRadarParameterInstruction() {
  // Instruction data packet based on Table 3
  byte instructionPacket[] = {
    0xAA, 0xAA,  // Frame header
    0x20, 0x00,  // Frame type (low byte first)
    0x71,        // Instruction of read parameter
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // Reserve
    0x55, 0x55   // Frame tail
  };

  // Send the instruction packet to the MAX232
  mySerial.write(instructionPacket, sizeof(instructionPacket));
}

void printRawRadarData() {
  // Check if data is available from the MAX232
  while (mySerial.available()) {
    char receivedChar = mySerial.read();
    Serial.print(receivedChar);
  }
}

yes i have , you mean this code will also run on arduino mega ?

#include <SoftwareSerial.h>

const int RX_PIN = 19; // Replace with your actual RX pin
const int TX_PIN = 18; // Replace with your actual TX pin
SoftwareSerial mySerial(RX_PIN, TX_PIN);

void setup() {
  Serial1.begin(115200);      // Serial monitor for debugging
  mySerial.begin(115200);    // SoftwareSerial for connecting to the MAX232

  // Send the "read radar parameter" instruction
  sendReadRadarParameterInstruction();
}

void loop() {
  // Check for and print the raw data from the radar
  printRawRadarData();
}

void sendReadRadarParameterInstruction() {
  // Instruction data packet based on Table 3
  byte instructionPacket[] = {
    0xAA, 0xAA,  // Frame header
    0x20, 0x00,  // Frame type (low byte first)
    0x71,        // Instruction of read parameter
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // Reserve
    0x55, 0x55   // Frame tail
  };

  // Send the instruction packet to the MAX232
  mySerial.write(instructionPacket, sizeof(instructionPacket));
}

void printRawRadarData() {
  // Check if data is available from the MAX232
  while (mySerial.available()) {
    char receivedChar = mySerial.read();
    Serial1.print(receivedChar);
  }
}

i am using this code.
and output should be like :
AA AA 0C 07 00 00 00 00 00 03 8E 00 55 55

The MEGA2560 has 4 hardware serial ports that would be capable of handling 115200 baud. You would not need to use a software serial port.

You will not get a software serial port to work correctly at 115200 baud unless you have one of the very high speed boards - maybe one of the Teensy variants.

If you must use the Arduino you have (is it an UNO?), then you need to configure your radar sensor for a much lower baud rate - I wouldn't try anything above 19200 baud. If that is not an option, then you need to look for a board with multiple hardware serial ports.

1 Like

For the Mega2560 try this code
Make sure pin 19 is connected to MAX232 Rout and 18 to the MAX232 Tin

// For Mega2560

// RX_PIN = 19
// TX_PIN = 18

// Connect pin 19 to MAX232 Rout
// Connect pin 18 to MAX232 Tin


void setup() {
  Serial.begin(115200);      // Serial monitor for debugging
  Serial1.begin(115200);    //  Serial 1 for connecting to the MAX232

  // Send the "read radar parameter" instruction
  sendReadRadarParameterInstruction();
}

void loop() {
  // Check for and print the raw data from the radar
  delay(600);
  printRawRadarData();
}

void sendReadRadarParameterInstruction() {
  // Instruction data packet based on Table 3
  byte instructionPacket[] = {
    0xAA, 0xAA,  // Frame header
    0x20, 0x00,  // Frame type (low byte first)
    0x71,        // Instruction of read parameter
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // Reserve
    0x55, 0x55   // Frame tail
  };

  // Send the instruction packet to the MAX232
  Serial1.write(instructionPacket, sizeof(instructionPacket));
}

void printRawRadarData() {
  // Check if data is available from the MAX232
  while (Serial1.available()) {
    char receivedChar = Serial1.read();
    Serial.print(receivedChar);
    Serial.print(" ");
  }
}
1 Like

thank you! but it didn`t work, is any other solution.

What DID it do? Did you change anything, or use it exactly as posted?

1 Like

That is not printing the HEX value of receivedChar.

1 Like

You are right. Please show @piyoosh the correct print statement

1 Like

Are you using the MAX232 IC or some RS232 module?
We need to make sure the RX, TX pins are connected the right way.

1 Like

YES i am using max232 module ( RS232 to TTL Serial Interface Module) and all connection is ok according to code . and also i have use Serial.print(receivedChar,HEX) for hex value.

Just to double check
Connect pin 19 to MAX232 Rout
Connect pin 18 to MAX232 Tin

also I made a mistake in the code
In the void printRawRadarData() function
change
Serial.print(receivedChar);
to
Serial.print(receivedChar, HEX);

1 Like

A very common mistake is having the Tx and Rx reversed on the RS-232 side of the wiring.

1 Like

1. Are you using Arduino UNO or Arduino MEGA?
2. Are you using this (Fig12) RS232/TTL Coneverter Module?
TTLRS232ConverterModule
Figure-1:

3. Do you have the following TTL/USB Converter Module (Fig-2)? If yes, then you can use the hardware UART Port (UART) of UNO to receive data from radar at Bd = 115200 bits/sec. The modules of Fig-2 would be used to transfer message from UNO to Serial Monitor at Bd = 9600 using software UART Port (SUART). If you don't have this module, then use Arduino MEGA's one of hese three UART Ports: Serial1, Serial2, Serial3.
image
Figure-2:

1 Like

1)I am using arduino mega 2560 but i have aslo checked on arduino uno.
2) yes i am using this convertor.
3) when i will use this TTL/USB convertor , have to use 9600 baud rate or 115200?
4) i am also using arduino mega with above code with serial 1 but it`s not responding