RS232 Shield send command and receive data
![userHead](https://dfimg.dfrobot.com/dfPortrait2.png)
I have the Rs232 shield 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 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.
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 is as follows:
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);
}
}
Hi!
Is the device unstable? It is recommended to try changing the baud rate.
![userHeadPic](https://dfimg.dfrobot.com/dfPortrait2.png)