Arduino to RS232 interface issue

I am trying to interface an arduino mega via a level converter to a RS232 device. To activate the RS232 device, I found I have to send string of hex codes. When I send the codes direct from the PC to the RS232 device, I see data being read back. But when I send the codes from the arduino/level converter, the RS232 device sends nothing to the arduino serial monitor.

Codes sent to RS232 device:
0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x01,0x00

I expect the RS232 device to respond:
55 AA 01 00 00 00 25 00 01 00 01 00 5E 00 5E 00 00 00 00 00 00 00 22 00 22 00 22 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 49

The arduino program I used:

#include <SoftwareSerial.h>

int i = 0;
int x = 1 ; 
byte GetData[] = {0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x01,0x00};

void setup() {
 Serial.begin(9600);
}
   
void loop()
{

 if(x>0){
   for (int i=0; i<sizeof(GetData); i++){
     Serial.write(GetData[i]);
   }
        x=x-1;
     }
 if (Serial.available()) {
   Serial.println("data");
   while(Serial.available()>0) {
     byte inByte = Serial.read();
     //Serial.print(inByte);
     delay(10);
   }}
}

The arduino serial monitor is blank.

Please help. Is there some bugs in my arduino program, or I have wrong configuration.

Thank you.

Maybe you're squirting out the message too soon.

(uncompiled, untested)

#include <SoftwareSerial.h>

byte GetData[] = {0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x01,0x00};

void setup() {
 Serial.begin(9600);

  delay (1000);  // could be longer, could be shorter.

 for (int i=0; i<sizeof(GetData); i++)
 {
   Serial.write(GetData[i]);
 }
}
   
void loop()
{
  if (Serial.available()) 
  {
    Serial.println("data");
    while(Serial.available()>0) {
      byte inByte = Serial.read();
     //Serial.print(inByte);
      delay(10);
    }
  }
}

I suggest you use the second example in Serial Input Basics to receive the data from your RS232 device.

...R

So, you are using softserial to communicate with the device, and built in serial to print out to the serial monitor?

I didnt see the software serial being used in the code.

Thank you very AWOL, Robin2 and mart256 for the prompt response.

AWOL, I tried different delays but it still did not work.

Robin2, I read the Serial Input basics, and followed the example. It did not help.

mart256, I used softserial to communicate and build in serial to print out to serial monitor. What did I missed?

Pls give me a helping hand. Thank you.

eujin888:
Thank you very AWOL, Robin2 and mart256 for the prompt response.

AWOL, I tried different delays but it still did not work.

Robin2, I read the Serial Input basics, and followed the example. It did not help.

mart256, I used softserial to communicate and build in serial to print out to serial monitor. What did I missed?

Pls give me a helping hand. Thank you.

Do you understand the code you posted? Or just copypasted it? Check the void setup, there is only one serial channel being initialized. There should be two channñels.

mart256, I am newbie. In a previous code void setup, I used and intialized 2 channels. That did not work either.

Previous code viod setup:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // RX, TX

byte GetData[] = {0x55,0xAA,0x01,0x00,0x00,0x00,0x00,0x01,0x00};

int i = 0;
int x = 1 ; 
void setup() {

Serial.begin(9600);
mySerial.begin(9600);

}

void loop()
{

//char buffer[4];

if(i>30000) {
// Serial.println("Sending...");
if(x>0){
  for (int i=0; i<sizeof(GetData); i++){
  
   //sprintf(buffer,"%00c",GetData[i]);
    
    Serial.write(GetData[i]);
    mySerial.write(GetData[i]);
   
     
    }
  
}
  //Serial.println("");
   x= x  - 1 ; 
   i=0;
}
if (mySerial.available()) {
  Serial.println("data");
  int inByte = mySerial.read();
  Serial.print(inByte);
}
if (Serial.available()) {
 
  int inByte = Serial.read();
  mySerial.write(inByte);
  Serial.write(inByte);
  
}
i++;
}

I appreciate any help to make my project work. Thank you.

That code makes more sense, but now the error could be also in the schematic. How are you shifting levels to rs232 from arduino?

Why did you chose pins 0 and 1 for soft serial? Arent those the hardware serial pins?

Show us a schematic of connections.

eujin888:
Robin2, I read the Serial Input basics, and followed the example. It did not help.

You need to tell me more if you want me to help.

Post the code that you used and tell me exactly what it did, and what you wanted it to do.

...R

msrt256,

I use a ttl to rs232 converter between arduino and rs232 device(an e-smart MPPT). No particular reason for pins 0 and 1 for soft serial. I thought any pin can be used as long as I define them. Using pins 10 and 11 also did not work either.

I attach a photo of the set-up.

Arduino Mega pin 10 to TTL/RS232 converter TXD; pin11 to TTL/RS232 converter RXD.
TTL/RS232 connector output to e-smart MPPT RS232 port.

Kindly advise/instruct. Thank you.