开云体育

Locked Re: RFID Connectivity #rfid


 

Steve,?

OK, last message about this! LOL! I am getting ahead of myself and making silly mistakes.

This is the sketch you should try. Ignore the last one. Create the RFID connection in JMRI first. This sketch is for the 7-byte tags.?this sketch sends the RFID data to JMRI over the serial connection in a format that JMRI can process. The line "Serial.println(rfidString);" sends the RFID data as a string over the serial port. As long as JMRI is listening on the same serial port and is configured to expect the same format of RFID data, it should be able to receive and process the data. The output string sent to JMRI for a typical 7-byte Mifare Ultralite RFID tag would be a 14-character hexadecimal string, with no prefix. Each byte of the tag's unique identifier (UID) would be represented by two hexadecimal characters in the output string, resulting in a total of 14 characters. We get this to work and the rest is fairly straightforward.?

This code uses the MFRC522 library to read RFID cards using an RC522 RFID reader. The code starts by defining the SS and RST pins for the RFID reader. It then creates an instance of the MFRC522 class with these pins.

In the setup function, the serial port is initialized and the SPI bus and MFRC522 reader are initialized. An array of bytes is also created to store the key used for authentication with the RFID card.

In the loop function, the code first checks if a new card is present by calling the PICC_IsNewCardPresent function. If no new card is present, the function returns and the code waits for a new card.

If a new card is present, the code calls the PICC_ReadCardSerial function to read the card's UID. The UID is then stored in an array of bytes.

The code then formats the UID data as a string and sends it over the serial port using the Serial.println function.

?

Finally, the code halts the card and stops the cryptographic communication with the card using the PICC_HaltA and PCD_StopCrypto1 functions.

?

#include <SPI.h>

#include <MFRC522.h>

?

#define SS_PIN 10

#define RST_PIN 9

?

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

?

MFRC522::MIFARE_Key key;?

?

// Init array that will store new NUID?

byte nuidPICC[7];

?

void setup() {?

? Serial.begin(9600, SERIAL_8N1);

? SPI.begin(); // Init SPI bus

? rfid.PCD_Init(); // Init MFRC522?

?

? for (byte i = 0; i < 6; i++) {

? ? key.keyByte[i] = 0xFF;

? }

}

void loop() {

? // Look for new cards

? if ( ! rfid.PICC_IsNewCardPresent())

? ? return;

?

? // Verify if the NUID has been read

? if ( ! rfid.PICC_ReadCardSerial())

? ? return;

?

? // Store the 7-byte UID in the nuidPICC array

? for (byte i = 0; i < 7; i++) {

? ? nuidPICC[i] = rfid.uid.uidByte[i];

? }

?

? // Format the RFID data as a string

? String rfidString = "";

? for (byte i = 0; i < 7; i++) {

? ? if (nuidPICC[i] < 0x10) {

? ? ? rfidString += "0";

? ? }

? ? rfidString += String(nuidPICC[i], HEX);

? }

?

? // Send the RFID data as a string over the serial port

? Serial.println(rfidString);

?

? rfid.PICC_HaltA();

? rfid.PCD_StopCrypto1();

}

Join [email protected] to automatically receive all group messages.