开云体育

Locked Re: RFID Connectivity #rfid


 

Seeedstudio 125KHz, Ethernet/Serial, Uno

Micheal,

Actually, the Ethernet shield type (W5100 or W5500) shouldn't even matter since the 125KHz readers don't use SPI. To get you started, upload the following sketch to the Uno, being sure to change the IP and port accordingly, attach the Ethernet shield and readers (see correct pins in the sketch), and plug in the CAT cable. In JMRI preferences, create a new connection (RFID) using the network interface type and the MERG Concentrator (A-H). Define the IP and port number you assigned to your Uno in the sketch, and then restart JMRI. The Uno can handle 7 of these readers, but will work for any number of readers from 1-7. Also, this sketch should also allow you to use the direct serial connection (same MERG Concentrator (A-H) connection type) as the output is sent to the Serial output in ALL cases and to the Ethernet connection if a shield is attached and a connection established.

// Import required libraries
#include <SoftwareSerial.h> // SoftwareSerial library for serial communication with the RFID readers
#include <Ethernet.h> // Ethernet library for communication over Ethernet
#include <SPI.h> // SPI library for communication with the Ethernet module
?
// MAC address of the Ethernet module
const byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
?
// IP address of the Ethernet module
const IPAddress ip(192, 168, 1, 177); // Change Uno's IP address as desired
?
// TCP server object listening on port 8888
EthernetServer server(8888); // Change port number as desired
?
// Create an array of SoftwareSerial objects to communicate with the RFID readers
SoftwareSerial rfidSerial[] = {
? SoftwareSerial(2, 3), // RX, TX for reader A
? SoftwareSerial(4, 5), // RX, TX for reader B
? SoftwareSerial(6, 7), // RX, TX for reader C
? SoftwareSerial(8, 9), // RX, TX for reader D
? SoftwareSerial(A0, A1), // RX, TX for reader E
? SoftwareSerial(A2, A3), // RX, TX for reader F
? SoftwareSerial(A4, A5) // RX, TX for reader G
};
?
// Array of reader IDs, used to determine which reader has sent a tag
const char readerIDs[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
?
void setup() {
? // Initialize the hardware serial port (USB connection)
? Serial.begin(9600);
?
? // Initialize the SoftwareSerial objects
? for (int i = 0; i < 7; i++) {
? ? rfidSerial[i].begin(9600);
? }
?
? // Initialize Ethernet communication
? Ethernet.begin((uint8_t*)mac, ip);
?
? // Start the TCP server
? server.begin();
}
?
char getReaderID(const unsigned char stx) {
? // Compare the STX byte to the known STX bytes for each reader
? for (int i = 0; i < 7; i++) {
? ? if (stx == (0xA0 + i)) {
? ? ? // If the STX byte matches, return the corresponding reader ID
? ? ? return readerIDs[i];
? ? }
? }
?
? // If no match is found, return an invalid reader ID
? return 'X';
}
?
// This function takes the tag data and the index of the reader that sent the tag data,
// formats the tag data as a string, adds the reader ID, and sends the data to the client.
void parseData(const unsigned char* data, const int readerIndex) {
? // Get the reader ID based on the index
? const char reader = readerIDs[readerIndex];
?
? // Copy the tag data into a character array
? char tag[8];
? for (int i = 1; i <= 8; i++) {
? ? tag[i - 1] = *(data + i);
? }
?
? // Get the received checksum
? uint8_t checksum = data[9];
?
? // Create the output string
? char output[15];
? snprintf(output, sizeof(output), "%c%s%02X\r\n>", reader, tag, checksum);
?
? // Send the output string to the hardware serial port (USB connection)
? Serial.print(output);
?
? // If a client is connected, send the output string
? EthernetClient client = server.available();
? if (client) {
? ? client.print(output);
? }
}
?
// This function runs repeatedly in the main loop
void loop() {
? // Check if data is available from any of the RFID readers
? for (int i = 0; i < 7; i++) {
? ? // If data is available from the current reader
? ? while (rfidSerial[i].available()) {
? ? ? // Read the tag data from the reader
? ? ? unsigned char tagData[10];
? ? ? int count = 0;
? ? ? while (count < 10 && rfidSerial[i].available()) {
? ? ? ? tagData[count++] = rfidSerial[i].read();
? ? ? }
?
? ? ? // Parse the tag data and send it to the client
? ? ? parseData(tagData, i);
? ? }
? }
?
? // Check if a client is connected to the server
? EthernetClient client = server.available();
? if (client) {
? ? // Do nothing if the client is connected
? }
}


Tom

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