RS232 Shield send command and receive data back

I am using the Rs232 shield from DFROBOT and I'm trying to get a small packet of data from a solar inverter using and Arduino MEGA. I have this working if I use a MAX RS232 on TX1 and RX1, however the DFROBOT Shield is using TX0 and RX0, which is the same as the serial output window that I'm using to see the results.

The process is as follows:

Send a command to the inverter through the serial port
Receive the information back from the serial port

I am getting one good-ish response which can be seen at 11:15:37 below, and the rest is NAKss. I've tried adding various delays since the baud rate is pretty slow, but that didn't seem to solve the problem.

The response I'm after is shown in the code as char myByteArray[]

SERIAL WINDOW OUTPUT

11:15:32.876 -> <File: serial-test-2>
11:15:32.983 -> QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ QPIGS⸮⸮ NAKss
11:15:37.279 -> QPIGS⸮⸮ 000.0 00.0 230.1 50.0 0000 0000 000 462 56.50 003 100 0035 00.(NAKss
11:15:39.653 -> QPIGS⸮⸮ NAKss
11:15:41.723 -> QPIGS⸮⸮ NAKss
11:15:43.757 -> QPIGS⸮⸮ NAKss
11:15:45.800 -> QPIGS⸮⸮ NAKss
11:15:47.817 -> QPIGS⸮⸮ NAKss
11:15:49.873 -> QPIGS⸮⸮ NAKss

My code:

const byte numChars = 108;
char receivedChars[numChars];
String outPut;
byte message[] = {0x51, 0x50, 0x49, 0x47, 0x53, 0xb7, 0xa9, 0x0d};
char myByteArray[] = "(000.0 00.0 240.3 50.0 3183 3183 063 393 53.40 004 062 0041 0004 277.7 00.00 00000 00010010 00 00 03562 010u)";

boolean newData = false;
boolean msgSent = false;
void setup() {
   Serial.begin(2400);
   //Serial1.begin(2400);
   Serial.println("<Arduino is ready>");
   Serial.println("<File: serial-test-2>");
}
void loop() {
   recvWithStartEndMarkers();
   showNewData();
   
}
void recvWithStartEndMarkers() {
   static boolean recvInProgress = false;
   static byte ndx = 0;  
   char startMarker = '(';
   char endMarker = '\r';
   char rc;
   Serial.write(message, sizeof(message));
   delay(5);
   while (Serial.available() > 0 && newData == false) {
     
       rc = Serial.read();
       delay(5);
       //String myString = String(myByteArray);
       if (recvInProgress == true) {
           if (rc != endMarker) {
               receivedChars[ndx] = rc;
               ndx++;
               if (ndx >= numChars) {
                   ndx = numChars - 1;
               }
           }
           else {
               receivedChars[ndx] = '\0'; // terminate the string
               recvInProgress = false;
               ndx = 0;
               newData = true;
               
           }
       }
       else if (rc == startMarker) {
           recvInProgress = true;
       }
   }
}
void showNewData() {
   if (newData == true) {
       //Serial.print("This just in ... ");
       Serial.println(receivedChars);
       newData = false;
       delay(2000);
   }
}

link, please!
C

As a newbie I'll go easy on you. Please read

and help us help you.
Thanks
C

1 Like

I think you're going to confuse the solar inverter if you're sending it commands and stuff intended for the serial monitor. The fact that it was working on serial1 reinforces that view I believe.

1 Like

@wildbill is right, I asked for a link in order to see if the device might have an option to select other serial, or if it might be easily hacked, because using serial Monitor on serial0 is always such a useful tool that you really should keep your other device on serial1.
C

1 Like

Here's the product:

Ok cool, that's what I wondered. I like the shield because it's nice and neat and doesn't require any flying wires or soldering! But I can go back to a separate MAX3232 board and wire it up if that's the best option.

That's what I'd do. The other possibility is to send your debugging output out of serial one. You would need a way to receive and display it - another Arduino perhaps.

1 Like

That's what I thought, it's really optimized for Uno, you can't easily redirect for Serial1 on a Mega.
Too bad. I guess you could fly the four leads of interest, 5V, GND, TX1, RX1, but you're right, DIY is probably easier as long as you're comfortable going that way.
C

1 Like

Yes, I had considered using a dot matrix display that I have, or indeed sending to another Arduino... but I think going back to what I know works is going to be the best step forward!
Thanks for your help!

Might as well flag this topic as solved, and give @wildbill credit.
C

1 Like

Er... I'm struggling to find the 'close' button... I've checked the 'Solution' box... is that done?

yep, that's all it takes!
HAND
(Have A Nice Day!)
C

I just thought of a potential hack: if I cut the RX and TX pins off the shield and then re-wire them to the RX1 and TX1 headers on the MEGA board I should be able to use Serial1 instead.

I did say... :grinning:
C

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.