Example: RFID reader integration with LogicMachine

Idesco_Access_7_c

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 easy adjustable if there is a necessity to trigger specific KNX, Bacnet etc objects.

Resident script

Add the following resident script with sleep-time = 0.

Source code    
  1. -- init
  2. if not port then
  3. require('serial')
  4.  
  5. port = serial.open('/dev/RS232', { baudrate = 9600 })
  6.  
  7. function readid(port)
  8. local char, byte, line, id, csum, val
  9.  
  10. -- wait for start byte
  11. char = port:read(1, 1)
  12. if not char then
  13. return nil, 'timeout'
  14. end
  15.  
  16. -- start byte must be STX
  17. byte = char:byte(1)
  18. if byte ~= 2 then
  19. return nil, 'wrong start byte'
  20. end
  21.  
  22. -- read remaining line
  23. line = port:read(15, 1)
  24. if not line then
  25. return nil, 'failed to read data'
  26. end
  27.  
  28. -- calculate checksum
  29. csum = 0
  30. for i = 1, 11 do
  31. val = tonumber(line:sub(i, i), 16)
  32. csum = bit.bxor(csum, val)
  33. end
  34.  
  35. -- verify checksum
  36. if csum ~= tonumber(line:sub(12, 12), 16) then
  37. return nil, 'invalid checksum'
  38. end
  39.  
  40. -- return ID
  41. return line:sub(1, 10)
  42. end
  43. end
  44.  
  45. id = readid(port)
  46. if id then
  47. log(id)
  48. end