Hello guys,
Ive got an animal weigh scale that outputs in RS232 format so i have an RS232 to TTL converter attached to my mega2560.
the data from the weigh scale device always comes in 4 lines so i set up a bit of code in my arduino with some help from the forums here to read and identify each line. I have attached the image of the serial output from the scale captured by a RS232 to USB cable to this device.
The weigh scale scans a cows ear tag via RFID and posts out the Tags ID number.
the problem is that my Arduino will only read the first piece of data, e.g. the animals ID number and tag number.
I have a 16x2 I2C LCD which displays the tag number for verification.
the issue seems to be that i can read the first tag but it ignores the rest until i reset.
The USB to RS232 cable attached to my laptop will continuously capture data so it must be my code.
I am using the weigh scales output to also measure water consumption of each animal so it needs to keep reading animals RFID tags.
I then store the water consumption, time and date (via RTC) and tag number on an SD card and post the data to a server using the Wiznet W5500 Ethernet module.
Ive attached the code here but i am not sure what is up with it. Any help would be great.
my code uses the following functions to read the data from the weight scale. there is a flag called "linechecker" which doesnt do anything which you can ignore.
so the Serial2 is read when data gets to it until it gets a new line, it then processes the data to see which line of data was received and if its valid. it SHOULD then display this on the LCD. At present it only does this once when connected to the weight reader.
here is the code which reads the serial data and then processes it.
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
// char endMarker = '\r';
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) { //LINE 1 SHOULD ALWAYS BE FR"EIDVID"
lineChecker = 1;
}
else if (strcmp(receivedChars, "?") == 0) { //LINE 2 ALWAYS IS JUST A ?
lineChecker = 2;
}
else if (line3Checker() != false) {
lineChecker = 3;
earTag = receivedChars;
// sameCow = true;
}
else if (check_string() == true) {
if (eidLookup == true) {
eidTag = receivedChars;
detectedCow = true;
duplicateChecker();
}
}
else {
lineChecker = 0;
}
lineChecker = 0;
lastTag=earTag;
newData = false;
}
}
boolean line3Checker() //CHECK LINE 3 FOR - E.G. A VALID TAG NUMBER ETC 200-11
{
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 WHERE THERE IS A 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;
//EIDLCD=1;
lcd.setCursor(0,1);
lcd.print("EID:");
int len = strlen(receivedChars);
lcd.print(&receivedChars[len-8]); //SHOW THE LAST 8 CHARS OF THE TAG NUMBER
lcd.setCursor(12,1);
lcd.print(" ");
return true;
}
else {
eidLookup = false;
return false;
}
}
in my main loop there is a piece of code that reads the Serial2, its to flush the serial buffer.
I mainly put this in to have the Serial buffer read and emptied while the start button wasnt pressed. i have a start stop button on the device, when its at stop it should ignore the Serial data but it reads into the buffer anyway. so i used read the buffer.
while (Serial2.available() > 0) {
char x = Serial2.read();
}
However, the above code runs in the loop always, i wonder if its causing an issue.
also here is my main loop.
void loop() {
////////////////////DAYLIGHT SAVINGS TIME CODE //////////////////////////
DateTime now = rtc.now();
if (now.dayOfTheWeek() == 0 && now.month() == 3 && now.day() >= 8 && now.day() <= 16 && now.hour() == 2 && now.minute() == 0 && now.second() == 0 && DST == 0)
{
rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour()+1, now.minute(), now.second()));
DST = 1;
EEPROM.put(0, DST);
}
else if(now.dayOfTheWeek() == 0 && now.month() == 11 && now.day() >= 1 && now.day() <= 8 && now.hour() == 2 && now.minute() == 0 && now.second() == 0 && DST == 1)
{
rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour()-1, now.minute(), now.second()));
DST = 0;
EEPROM.put(0, DST);
}
////////////////////////////////////////////////////////////
currentMillis = millis();
int reading = digitalRead(startButton); //START BUTTON WILL SET A FLAG TO READ DATA OR IGNORE DATA
// Serial.println(reading);
if (startStop == 1)
{
recvWithEndMarker(); //CHECK SERIAL DATA
showNewData();
redBlink(); //SHOW A BLINKING LED AND SHOW THE WORD 'REC' ON THE LCD
readyMsg=1;
}
else {
if(readyMsg==1){
lcd.setCursor(0,0);
lcd.print("STANDBY");
}
while (Serial2.available() > 0) {
char x = Serial2.read();
}
digitalWrite(redLED, HIGH);
}
if (reading == LOW && previous == HIGH && millis() - time > debounce) {
if (state == LOW)
state = HIGH;
else
state = LOW;
time = millis();
}
startStop = state;
if ((enteredLoop == 1) && (startStop == 1)) {
if (currentMillis - previousMillis >= 60000) {
saveData();
}
else if ((currentMillis - previousMillis <= 60000) && (detectedCow == true))
{
if (started == 0)
{
getTime();
}
checkLevel();
}
}
previous = reading;
lastButtonState = startStop;
}
My issue is that the Serial only reads and displays the data on the LCD ONCE on reset but not again.
DOLGAN_TEST_CODE.ino (15.9 KB)


