Hello everyone, I'm new in building Arduino codes and I have a problem with data read.
I use Arduino Mega 2560 and RFID module from Elechouse (http://www.elechouse.com/elechouse/images/product/13.56MHZ_RFID_Module/13.56MHZ_RFID_Manual.pdf
I used the code sample from Elechouse:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //pin2 Rx, pin3 Tx
int CMD[64]; // Here the Max command length is set as 64 bytes. For example, Command “AB 02 01” is 3 bytes
int comlen =0;
int out_flag =0;
void setup()
{
Serial.begin(9600);
mySerial.listen();
Serial.println("Serial number will be displayed here if a card is detected by the module:\n");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
delay(10);
mySerial.write(0x02); //Send the command to read RFID tag, please refer to the manual for more detail.
}
void loop() // run over and over
{
while (Serial.available())
{
int a = SerialReadHexDigit();
//Serial.println(a);
if(a>=0){
CMD[comlen] = a;
comlen++;
}
delay(10);
}
for(int i=0; i<comlen; i+=2){
int c = mySerial.write( CMD[i]*16 + CMD[i+1]); //Convert Hex Characters in to Hex number, and send it to RFID.
}
comlen =0;
while (mySerial.available()) {
byte C = mySerial.read();
if (C<10) Serial.print("0");
Serial.print(C ,HEX); //Display in HEX
Serial.print(" ");
out_flag =1;
}
if (out_flag >0) {
Serial.println();
out_flag = 0;
}
}
/*************************************************************************************
* The following function is to receive data and judge if it is HEX character. Hex characters
* include 1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,A,B,C,D,E,F
* Any other characters sent with the command will be ignored.
**************************************************************************************/
int SerialReadHexDigit()
{
byte c = (byte) Serial.read();
if (c >= '0' && c <= '9') {
return c - '0';
}
else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
else {
return -1; // getting here is bad: it means the character was invalid
}
}
And only appears "Serial number will be displayed here if a card is detected by the module". Nothing more.
I read some topics that SoftwareSerial doesn't work very well on Mega 2560. So I am trying to change to Hardware Serial 1 (RX1: PIN19, TX1: PIN18) and only write Serial number will be displayed here if a card is detected by the module again.
My code now is this:
int CMD[64]; // Here the Max command length is set as 64 bytes. For example, Command “AB 02 01” is 3 bytes
int comlen =0;
int out_flag =0;
void setup()
{
Serial.begin(9600);
Serial.println("Serial number will be displayed here if a card is detected by the module:\n");
Serial1.begin(9600);
delay(10);
Serial1.write(0x02); //Send the command to read RFID tag, please refer to the manual for more detail.
}
void loop() // run over and over
{
while (Serial.available())
{
int a = SerialReadHexDigit();
//Serial.println(a);
if(a>=0){
CMD[comlen] = a;
comlen++;
}
delay(10);
}
for(int i=0; i<comlen; i+=2){
int c = Serial1.write( CMD[i]*16 + CMD[i+1]); //Convert Hex Characters in to Hex number, and send it to RFID.
}
comlen =0;
while (Serial1.available()) {
byte C = Serial1.read();
if (C<10) Serial.print("0");
Serial.print(C ,HEX); //Display in HEX
Serial.print(" ");
out_flag =1;
}
if (out_flag >0) {
Serial.println();
out_flag = 0;
}
}
/*************************************************************************************
* The following function is to receive data and judge if it is HEX character. Hex characters
* include 1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,A,B,C,D,E,F
* Any other characters sent with the command will be ignored.
**************************************************************************************/
int SerialReadHexDigit()
{
byte c = (byte) Serial.read();
if (c >= '0' && c <= '9') {
return c - '0';
}
else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
else {
return -1; // getting here is bad: it means the character was invalid
}
}
It is expected to appear something like 8C 9A F5 10
Please, give me some orientation.