#include <SoftwareSerial.h>
SoftwareSerial rfid(2, 3);
unsigned char buffer[64]; // buffer array for data recieve over serial port
int count=0; // counter for buffer array
void setup()
{
rfid.begin(9600); // the rfid baud rate
Serial.begin(9600); // the Serial port of Arduino baud rate.
}
void loop()
{
if (rfid.available()) // if date is comming from softwareserial port ==> data is comming from rfid shield
{
while(rfid.available()) // reading data into char array
{
buffer[count++]=rfid.read(); // writing data into array
if(count == 64)break;
}
Serial.write(buffer,count); // if no data transmission ends, write buffer to hardware serial port
clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array
count = 0; // set counter of while loop to zero
}
if (Serial.available()) // if data is available on hardwareserial port ==> data is comming from PC or notebook
rfid.write(Serial.read()); // write it to the rfid shield
}
void clearBufferArray() // function to clear buffer array
{
for (int i=0; i<count;i++)
{ buffer[i]=NULL;} // clear all index of array with command NULL
}
if (rfid.available()) // if date is comming from softwareserial port ==> data is comming from rfid shield
{
while(rfid.available()) // reading data into char array
{
buffer[count++]=rfid.read(); // writing data into array
if(count == 64)break;
}
Serial.write(buffer,count); // if no data transmission ends, write buffer to hardware serial port
clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array
count = 0; // set counter of while loop to zero
}
You won't necessarily get all the bytes quickly, so you will leave the loop prematurely. Read this:
My example above of RFID reader shows a better way of reading the data.
Well it is your problem.
The buffer only fills up at the data rate of the input but you empty it very quickly. So you start reading before all the data is in and finish reading too soon.
Well it is your problem.
The buffer only fills up at the data rate of the input but you empty it very quickly. So you start reading before all the data is in and finish reading too soon.
My example above of RFID reader shows a better way of reading the data.
I get your example and change for my example...:
#include <SoftwareSerial.h>
SoftwareSerial rfid(2, 3);
const unsigned int MAX_INPUT = 128;
int count=0; // counter for buffer array
void setup()
{
rfid.begin(9600); // the rfid baud rate
Serial.begin(9600); // the Serial port of Arduino baud rate.
}
void process_data (char * data)
{
// for now just display it
Serial.println("FOUND");
Serial.println (data);
} // end of process_data
void loop()
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
if (rfid.available () > 0)
{
char inByte = rfid.read ();
Serial.print(inByte);
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
}
}
}
You haven't. You've been told that your code for reading the RFID tag is not robust. You insist that the problem must come after that. You are not accepting the fact that you need to fix all upstream errors before trying to deal with downstream errors.