Hi,
I am trying to read data from RFID reader and send data using Bluetooth. When I am trying to do it separately, everything works fine, but when I am trying to combine 2 ports, I have problems. I tried to read in multiple forums for a while and it didn't help much. Here is my code:
#include <SoftwareSerial.h>
unsigned char TAG_1[38]={10,85,51,48,48,48,69,50,48,48,48,48,49,68,56,54,48,50,48,49,50,48,50,49,57,48,53,53,49,48,57,53,67,69,13,10,10};
unsigned char TAG_2[38]={10,85,51,48,48,48,51,48,48,56,51,51,66,50,68,68,68,57,48,49,52,48,48,48,48,48,48,48,48,48,51,57,66,66,13,10,10};
SoftwareSerial RFIDSerial (8,9);
SoftwareSerial blueTooth (2,3);
unsigned char incomingByte;
void sendIdentifyCmd ()
{
RFIDSerial.write (0x0A);
RFIDSerial.write (0x55);
RFIDSerial.write (0x0D);
}
void copy_str(unsigned char *str_src, unsigned char *str_dest, int len){
for(int i=0; i<len; i++){
str_dest[i]=str_src[i];
}
}
bool str_cmp(unsigned char *str1, unsigned char *str2, int len=37){
for(int i=0; i<len; i++){
if(str1[i]!=str2[i]){
return false;
}
}
return true;
}
void setup ()
{
Serial.begin (9600);
RFIDSerial.begin (38400);
blueTooth.begin(38400);
Serial.println ("begin initial Serial!\n");
// blueTooth.println("lets bt\n");
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
void loop ()
{
int i=0;
unsigned char str[700]="";
RFIDSerial.listen();
if (RFIDSerial.isListening()) {
Serial.println("RFIDSerial is listening!"); //i am and getting this message
}else{
Serial.println("RFIDSerial is not listening!");
}
sendIdentifyCmd ();
delay (2);
while(RFIDSerial.available () > 0) //this loop is never reached because "he is available" is not printed
{
Serial.println("he is available");
incomingByte=RFIDSerial.read();
str[i]=incomingByte;
Serial.print (str[i]);
Serial.print (' ');
i++;
}
unsigned char *temp_str=(unsigned char*)malloc((i+1)*sizeof(unsigned char));
copy_str(str, temp_str, i);
blueTooth.listen();
blueTooth.println("hi");//i am and getting this message
if(str_cmp(TAG_1, temp_str)){
Serial.println();
Serial.print("Hello Tag1\n");
blueTooth.print("Hello Tag1 from bt");
}
if(str_cmp(TAG_2,temp_str)){
Serial.println();
Serial.print("Hello Tag2\n");
blueTooth.print("Hello Tag2 from bt\n");
}
Serial.println();
free(temp_str);
delay (1000);
}
I put the method listen() in order to use different ports separately. As you can see, when I am trying to listen to RFID, I am getting the message that RFID is listening, and I even send the command to the reader to read tags. I can see on my module a blue led blinking which means that the reader is reading, but I am not able to read the data that comes from the reader. At the same time, I am sending data via the Bluetooth port when I am listening to it.
Now I am wondering who is the listen() method listens to.
So the questions are:
- Why RFIDSerial is not available and how to make it available?
- who is the listen() method listens to?
Thanks in advance!