Hey guys,
First of all, thanking Robin for his tutorial on serial basics here.
http://forum.arduino.cc/index.php?topic=396450
Robin's code showed me how to read the Serial more efficiently. I had to go a little more further to use his code for reading multiple serials at the same time (with a common function with start and end markers).
Problem is that, after a couple of serial reads, the output seems to be mixing up. Means the new out put comes with some mixed up some parts of previous buffer. The variables used is that of local scope, so shouldn't be problem right? To my knowledge, the variables declared inside the function will be cleared after the execution is complete.
I'm using the CRC32 library from here, GitHub - bakercp/CRC32: An Arduino library for calculating a CRC32 checksum.
to ensure what I send and receive are same.
So the serial data will have
a start character '<'
and an end character '>'
and crc code seprated with '|'
Data is json. So it will look like the below (actual valid serial data)
<6260500|{"H":{"O":0,"M":3,"K":"","N":0}}>
and another one
<2198828917|{"H":{"O":0,"M":3,"K":"BkJ7jZv0b","N":2133462144}}>
Guys, i'm still a noob with all these, and I'm not at all sure if what I'm doing is all good or correct. Kindly take a look at my code below and let me know your comments.
#include <CRC32.h>
#define MAX_PACKET_SIZE 600
#define CHECKSUM_CHAR_SIZE 15
#define START_MARKER '<'
#define END_MARKER '>'
struct SystemData
{
boolean enableDebugMode = false;
boolean enableMegaTestMode = false;
boolean iAmTheServer = false;
char payload[MAX_PACKET_SIZE];
String apiKey;
uint32_t deviceKey;
uint32_t serverId;
};
struct Serial0Data
{
boolean newData = false;
unsigned long lastPing = 0;
};
struct Serial1Data
{
boolean newData = false;
unsigned long lastPing = 0;
};
Serial0Data serial0Data;
Serial1Data serial1Data;
SystemData systemData;
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
Serial2.begin(115200);
Serial.println("<Arduino is ready>");
}
void loop()
{
checkAndParseSerials(Serial2, systemData.payload, serial0Data.newData, serial0Data.lastPing);
if (serial0Data.newData)
{
serial0Data.newData = false;
Serial.print("From Serial0: ");
Serial.println(systemData.payload);
}
checkAndParseSerials(Serial1, systemData.payload, serial1Data.newData, serial1Data.lastPing);
if (serial1Data.newData)
{
serial1Data.newData = false;
Serial.print("From Serial1: ");
Serial.println(systemData.payload);
}
}
void checkAndParseSerials(Stream &port, char * payload, boolean &newData, unsigned long &lastPing)
{
char receivedChars[MAX_PACKET_SIZE + CHECKSUM_CHAR_SIZE];
static boolean recvInProgress = false;
static int ndx = 0;
char rc;
while (port.available() > 0 && newData == false)
{
rc = port.read();
if (recvInProgress == true)
{
if (rc != END_MARKER)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= MAX_PACKET_SIZE + CHECKSUM_CHAR_SIZE)
{
ndx = (MAX_PACKET_SIZE + CHECKSUM_CHAR_SIZE) - 1;
}
}
else
{
receivedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
lastPing = millis();
if (verifiedSerialData(receivedChars, payload))
{
newData = true;
}
}
}
else if (rc == START_MARKER)
{
ndx = 0;
//memset(receivedChars, 0, MAX_PACKET_SIZE);
recvInProgress = true;
}
}
}
bool verifiedSerialData(char * receivedChars, char * payload)
{
char checksum[CHECKSUM_CHAR_SIZE];
char * strtokIndx;
//memset(checksum, 0, CHECKSUM_CHAR_SIZE);
memset(payload, 0, MAX_PACKET_SIZE - CHECKSUM_CHAR_SIZE);
if (strlen(receivedChars))
{
if (strstr(receivedChars, "|"))
{
strtokIndx = strtok(receivedChars,"|");
strcpy(checksum, strtokIndx);
strtokIndx = strtok(NULL, "|");
strcpy(payload, strtokIndx);
size_t numBytes = String(payload).length() - 1;
CRC32 crc;
crc.update(payload, numBytes);
if ((String(crc.finalize()) == String(checksum)) && (checksum))
{
return true;
}
else
{
Serial.println("CRC code test FAILED!");
return false;
}
crc.reset();
}
}
return false;
}
I should be doing something very silly here. Maybe clear the input buffer of serials after read? I dont know. Tried to troubleshoot for long, but I dont know the way out of this problem.
@Robin if you can also take a look on this, would be very helpful since its mainly an edited version of your code.
Thanks in advance.