@John Ryan: I would be happy to work together to solve this!
I've reworked my previous code based on the all suggestions. Still no luck
This is it:
#include <SoftwareSerial.h>
#define rxPin 7
#define txPin 2
#define TAG_LEN 12
// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
int ledPin = 13;
int serIn;
byte code[TAG_LEN]; // var that will hold the bytes-in read from the serialBuffer
byte bytes_read = 0;
char target_tag[] = {0x00, 0x1b, 0x4e, 0xc1, 0xd0, 0x14, 0xe0, 0xd5, 0x7b };
void setup() {
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(ledPin, OUTPUT);
mySerial.begin(19200); // set the data rate for the SoftwareSerial port
Serial.begin(19200); // opens serial port, sets data rate to 19200 bps
}
void loop () {
mySerial.print(0xfa, BYTE); //request Tag code
// only if there are bytes in the serial buffer execute the following code
if(serialAvailable()) {
//keep reading from serial untill there are bytes in the serial buffer
while (serialAvailable() && bytes_read < TAG_LEN){ //read Serial
code[bytes_read] = serialRead(); //insert the byte you just read into the array at the specified index
bytes_read++; //update the new index
}
//feedback that something was received
Serial.println ("Processing Tag");
}
//do somenthing else perhaps wait for other data.
Serial.println ("NO TAG ");
//print out later in the loop the sentence only if it has actually been collected;
if( bytes_read >= TAG_LEN) {
Serial.print("Tag ID= ");
//loop through all bytes in the array and print them out
for(bytes_read=0; bytes_read < TAG_LEN; bytes_read++) {
Serial.print((code[bytes_read]), HEX); //print out the byte at the specified index
//serInString[serOutIndx] = ""; //optional: flush out the content
}
if (memcmp(code, target_tag, TAG_LEN) > 0) { //if received tag code == to listed tag code
digitalWrite(ledPin, HIGH); //do something (blink a led)
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
//reset all the functions to be able to fill the string back with content
bytes_read = 0;
}
//slows down the visualization in the terminal
Serial.flush(); //flush serial buffer
Serial.println();
delay(200);
}
I really can't think of anything else to do. Any help is appreciated.
Regards, Loïc