I would like to read RFID Cards with NodeJS. I have a basic RFID-Reader (really don't know the model) that is connected via usb with my computer.
I've tried to read the serial data with NPM Package "serialport" using the following code:
var SerialPort = require('serialport');
var Readline = require('@serialport/parser-readline');
var port = new SerialPort('/dev/cu.usbserial-0001', { baudRate: 9600, parser: new Readline() });
port.on('data', (data) => {
var serialData = data.toString('hex');
console.log(data);
});
I start the program and when I hover the Card over the reader I get something like this (as long as I have the card on the reader it changes very fast):
0
1818
18
18
98
98
e0
98
f898
e6
18
7e
7e
18
f8
00
18
1818
18
98
98
e0
98f898e6
187e
7e
18
f8
00
18
18
18
18
98
98e0
98
f898
e618
7e7e
18
f8
...
...
...
Alternativ I tried to parse the data with a redline like this:
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const port = new SerialPort('/dev/tty.usbserial-0001', {baudRate: 9600});
const parser = port.pipe(new Readline())
parser.on('data', (data) => console.log(data))
but unfortunately I don't get an output on this one.
The expected output is, that I get the RFID-Card ID which I can use in the rest of my program. Previously the ID stored in the Database had a format like: "MT_0E00635C65" and the ID printed on the card is for example: "0015642441 241,48265". I guess in the old program (to read the card) the developer used something like this:
private char[]const HexpartsReadline = new char[16] {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'
string text = RFID_Port.ReadLine ();
if (textparser.Length == 10) {
string text2 = "";
string text3 = text;
foreach (char c in text3) {
text2 += Hexparts [on(byte)c - 48];
}
LastKart = text2;
LastKartView ='data', DateTimeconsole.Now;
}
}
};
Its my first time working with RFID-Cards so it's kinda hard to wrap my head around it. Can you assist me on how to achieve my desired result?
Thanks.
// Update
i was able to figure out a pattern I guess, I checked multiple cards and the beginning is always the same it looks like this (have removed the blank spaces )
Card 1:
00
18181818
9898e098
7ee01898f8 #this line changes
18
f8
Card 2:
00
18181818
9898e098
f898e6187e7e
18
f8
Card3:
00
18181818
9898e098
f898187efe
18
f8
// Update Still not figured out how to solve this, anyone has an idea?
I've got the desired result using this code:
var SerialPort = require('serialport');
var sp = new SerialPort('/dev/tty.usbserial-0001', {
baudRate: 4800,
});
let package = '';
const Hexparts = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
sp.on('data', (data) => {
const stringData = data.toString().trim();
if (stringData) {
package += stringData;
if (package.length == 10) {
let text2 = '';
let text3 = package;
for (const c of text3) {
text2 += Hexparts[c.charCodeAt(0) - 48];
}
const RFID_Data = text2;
console.log(RFID_Data);
}
} else {
package = '';
}
});