Hi! I have problems with reading RFID tags.
The code I'm using is this:
int dato=1, inByte;
void setup() {
// initialize both serial ports:
Serial.begin(115200);
Serial1.begin(57600);
Serial.println("read = 1");
}
void loop() {
if (Serial.available()) {
dato = Serial.read();
}
switch(dato){
case '1':
dato=0;
Serial.print("pin E= ");
Serial1.write(0x04);
Serial1.write(0x00);
Serial1.write(0x01);
Serial1.write(??); // What are the data belonging to these fields?
Serial1.write(??); // What are the data belonging to these fields?
Serial.println(" espera");
break;
}
}
void serialEvent1() {
if (Serial1.available()) {
inByte = Serial1.read();
Serial.print(inByte,HEX);
Serial.print(" ");
}
}
and returns: 5 0 0 FE 87 73
According to the manual, the problem is in the CRC, how can I fix it?
How to calculate the parameters for the command "MSB-CRC16" and "LSB-CRC16"?
You have more than that. You have not explained what Serial and Serial1 are connected to.
If Serial is connected to the PC, and you are sending data to the Arduino from the Serial Monitor, to tell the Arduino to deal with Serial1, you then WRITE to the Serial1 instance, where, presumably, the RFID reader is. What does that have to do with reading RFID tags?
So, you need to clarify what is connected to what.
Hi PaulS, thanks for your interest.
1 - The reader is connected to a arduino mega 2560 on port serial1 (pins 18 y 19).
2 - Through the serial port of the Arduino, RFID module prints the response in the computer.
3 - The RFID module returns a problem with the values ??of "CRC-16LSB" and "CRC-16MSB"
4 - I would like to know how I can calculate or know the "CRC-16LSB" values ??and "CRC-16MSB"?
Thank you for your help.
Attachment the datasheet and the manufacturer's link
No, it doesn't. The RFID module can NOT print data to the computer. It can make data available to the Arduino which can print it to the serial port that the PC is on the other end of.
What are you writing to the RFID reader? Why are you writing to the RFID reader?
The module is connected as you say to Arduino and Arduino screen printed on the PC by serial port, as you can see in the code.
When sending commands that are in the manual towards RFID module, it responds the values ??shown in the image, as the manual says there is an error in the CRC values??.
My question is: How can I calculate values ??for CRC-16, which must go in the last 2 bytes of the command of RFID?
Hola paulS
El modulo esta conectado como usted dice a Arduino, y Arduino imprime en la pantalla del PC por puerto serial, como se puede ver en el código.
Al enviar los comandos que están en el manual hacia el modulo RFID, este responde los valores que se muestran en la imagen, según el manual, dice que hay un error en los valores de CRC.
Mi pregunta es: ¿Cómo puedo calcular los valores de CRC-16, que deben ir en los últimos 2bytes del comando del RFID?
int dato=1, inByte;
void setup() {
// initialize both serial ports:
Serial.begin(115200);
Serial1.begin(57600);
Serial.println("read = 1");
}
void loop() {
if (Serial.available()) {
dato = Serial.read();
}
switch(dato){
case '1':
dato=0;
Serial.print("pin E= ");
Serial1.write(0x04);
Serial1.write(0x00);
Serial1.write(0x01);
Serial1.write(??); // What are the data belonging to these fields?
Serial1.write(??); // What are the data belonging to these fields?
Serial.println(" espera");
break;
}
}
void serialEvent1() {
if (Serial1.available()) {
inByte = Serial1.read();
Serial.print(inByte,HEX);
Serial.print(" ");
}
}
unsigned char rfid[4] = {
5, 0, 0, 0xFE
};
unsigned char test[3] = {
4, 0, 1
};
void setup()
{
uint16_t crc_out;
Serial.begin(9600);
// while(!Serial);
// delay(1000);
// See: http://forum.arduino.cc/index.php?topic=235918.0
// and see the user's manual in reply #4 for the code to
// calculate the CRC on page 6
// pass in the address of the bytes to be processed for a CRC
// and the the number of bytes. In this case I used the four
// bytes shown in the example in the thread at the URL above
// and there are 4 bytes. The code prints the correct CRC (7387)
crc_out = rfid_crc16(rfid,4);
// NOTE that this prints the result in the order MSByte LSByte
// but you must send them in the order LSByte and then MSByte
// This prints the CRC as 0x7387 but you would
// send it as 0x87 0x73
Serial.print(crc_out,HEX); //(expecting = 0x7387)
Serial.println(" rfid");
Serial.println("");
// generates 0x4BDB
crc_out = rfid_crc16(test,3);
Serial.print(crc_out,HEX);
Serial.println(" test");
}
void loop()
{
}
// This is exactly the code from page 6
#define PRESET_VALUE 0xFFFF
#define POLYNOMIAL 0x8408
unsigned int rfid_crc16(unsigned char const *pucY, unsigned char ucX)
{
unsigned char ucI,ucJ;
unsigned short int uiCrcValue = PRESET_VALUE;
for(ucI = 0; ucI < ucX; ucI++) {
uiCrcValue = uiCrcValue ^ *(pucY + ucI);
for(ucJ = 0; ucJ < 8; ucJ++) {
if(uiCrcValue & 0x0001) {
uiCrcValue = (uiCrcValue >> 1) ^ POLYNOMIAL;
}
else {
uiCrcValue = (uiCrcValue >> 1);
}
}
}
return uiCrcValue;
}
The first test uses the data from the example that you have circled in RFID.jpg which you attached to your first post. The data are the four bytes 5, 0, 0, 0xFE and the example shows the CRC as 87 73. The program prints 7387 becuase it prints the MSB (73) first whereas you must send the LSByte first to the RFID device.
The second test uses the data from the code in your first post: 4, 0, 1 and the result shows that the CRC is 0x4BDB which you would have to send as 0xDB and then 0x4B.
The CRC in your code is correct but I can't find any documentation of the 0x35 command. The manual in message #4 does not have a 0x35 command and it also doesn't have pages 35-36. Which manual has those pages?
The CRC is correct and the message looks valid, so at worst the reader should have returned an error message. The garbled response you're getting suggests that the reader is not wired correctly.