Hello,
I am trying to use an RS232 converter with my Mega2560 to read data that is being outputted by a weight scale device. The scale uses RFID tags and outputs 4 lines two of which contain the tag number and an assigned ID, the first two lines arent important.
Ive been trying to implement this using the sample code which reads serial data byte by byte until a newline is detected.
The sample code i modified is a much more efficient way of reading Serial data instead of using that awful Serial.readString function.
Ive been able to capture the RS232 output from the the weight scale using a USB to RS232 converter.
the data looks like this:
FR"EIDVID"
?
969 000000680825
FR"EIDVID"
?
969 000000680825
FR"EIDVID"
?
969 000000680825
FR"EIDVID"
?
969 000000680825
The data always starts with the FR"EIDVID" indicator and then a question mark in line 2.
Line 3 can sometimes be blank depending if the RFID tag has an alias assigned, if its blank its blank and if it contains a value it contains a value, it really makes no difference if its blank.
Its line 4 i want to capture, it will always be a numeric value.
So ive written some code with some very helpful advice i got in an earlier post.
The problem is that i dont have immediate access to the hardware and i would like to try and tie down the programming side as much as possible. I will test and if it fails I will try and figure out and rectify the issue.
The code should read data until a newline is received and then the string is terminated.
Then it performs the check, line 1 is checked to see if its FR"EIDVID", a variable lineChecker is set to 1.
line 2 is check to see if its equal to a ?, if so line checker is 2
line 3 is checked to see if it contains a hypen as the Alias is usually something like 33-33 or 1-102.
line 4 is checked to see if its all numbers.
At present this code works if i simulate it from the Arduino terminal but i was looking for some advice on successfully detecting the last two lines reliably.
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n'; //read until newline
char rc;
while (Serial2.available() > 0 && newData == false) {
rc = Serial2.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
if (strcmp(receivedChars, "FR\"EIDVID\"") == 0) { //check for line 1
lineChecker = 1;
}
else if (strcmp(receivedChars, "?") == 0) { //check for line 2
lineChecker = 2;
}
else if (line3Checker() != false) { //check if line 3 contains a hyphen
lineChecker = 3;
earTag = receivedChars;
}
else if (check_string() == true) {
if (eidLookup == true) {
eidTag = receivedChars;
detectedCow = true;
duplicateChecker();
}
}
else {
lineChecker = 0;
}
lineChecker = 0;
lastTag=earTag;
newData = false;
}
}
boolean line3Checker()
{
char * pch;
pch = strchr(receivedChars, '-');
while (pch != NULL)
{
return true;
}
return false;
}
boolean check_string() {
int counter = 0;
char* testChars = receivedChars;
int string_len = strlen(testChars);
for (int i = 0; i < strlen(testChars); i++) { //add a 0 in blank space
if (testChars[i] == ' ')
{
testChars[i] = '0';
}
}
for (int i = 0; i < strlen(testChars); i++) {
if (isdigit(testChars[i])) {
counter++;
}
}
if ((counter == strlen(testChars) && (strlen(testChars) != 0)))
{
lineChecker = 4;
eidLookup = true;
int len = strlen(receivedChars);
return true;
}
else {
eidLookup = false;
return false;
}
}