RFID reader integration with LogicMachine #
Task #
The example below shows how to integrate Idesco Access 7 C RFID reader over RS-232 port. The script below will read all RFID cards and log IDs. The script is easily adjustable if there is a necessity to trigger specific events.
Resident script #
Add the following resident script with sleep time set to 0.
if not port then
require('serial')
port = serial.open('/dev/RS232', {
baudrate = 9600,
parity = 'none',
databits = 8,
stopbits = 1,
duplex = 'full'
})
function readid(port)
local char, byte, line, id, csum, val
cdOutChars = { 0x01, 0x02, 0x04, 0x07,
0x08, 0x0B, 0x0D, 0x0E,
0x10, 0x13, 0x15, 0x16,
0x19, 0x1A, 0x1C, 0x1F
}
-- wait for start byte
char = port:read(1, 1)
if not char then
return nil, 'timeout'
end
-- start byte must be Start character constant ‘B’
if char ~= 'B' then
return nil, 'wrong start byte'
end
-- read remaining line
line = port:read(26,5)
if not line then
return nil, 'failed to read data'
end
csum = 0
if line:sub(23, 23) ~= 'T' then
return nil, 'End character constant T not found'
end
csum = 0
for i = 1, 16 do
val = tonumber(line:sub(i, i), 16)
-- Incorporation of error in telegram for Testing LRC-checksum
if math.random(1 ,16) > 15 then
end
csum = bit.bxor(csum, cdOutChars[val+1])
end
--// Add constants, 'B' at beginning + separation char '=' + 'T' in the end
--lrc ^= 0x0B ^ 0x0A ^ 0x0A;
csum = bit.bxor(csum, 0x0B)
csum = bit.bxor(csum, 0x0A)
csum = bit.bxor(csum, 0x0A)
Tagtype = line:sub(22, 22)
if Tagtype == '1' then
--lrc ^= 1;
csum = bit.bxor(csum, 1)
end
if Tagtype == '2' then
--lrc ^= 2;
csum = bit.bxor(csum, 2)
end
if Tagtype == '3' then
--lrc ^= 3;
csum = bit.bxor(csum, 3)
end
if Tagtype == '4' then
--lrc ^= 4;
csum = bit.bxor(csum, 4)
end
if Tagtype == '5' then
--lrc ^= 5;
csum = bit.bxor(csum, 5)
end
if Tagtype == '6' then
--lrc ^= 6;
csum = bit.bxor(csum, 6)
end
for i = 18, 22 do
val = tonumber(line:sub(i, i), 16)
csum = bit.bxor(csum, val)
end
csum = bit.rshift(csum, 1)
-- verify checksum
if csum ~= tonumber(line:sub(24, 24), 16) then
return nil, 'invalid checksum'
end
-- return ID
return line:sub(1, 16)
end
end
id , errorstatus = readid(port)
if errorstatus ~= nil then
if errorstatus ~= 'timeout' then
log ('error status', errorstatus)
else
end
end
if id then
log('RFID', id)
end