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. if not port then
  2. require('serial')
  3. port = serial.open('/dev/RS232', { baudrate = 9600, parity = 'none', databits = 8, stopbits = 1, duplex = 'full' })
  4.  
  5. function readid(port)
  6. local char, byte, line, id, csum, val
  7. cdOutChars = { 0x01, 0x02, 0x04, 0x07,
  8. 0x08, 0x0B, 0x0D, 0x0E,
  9. 0x10, 0x13, 0x15, 0x16,
  10. 0x19, 0x1A, 0x1C, 0x1F
  11. }
  12.  
  13. -- wait for start byte
  14. char = port:read(1, 1)
  15. if not char then
  16. return nil, 'timeout'
  17. end
  18.  
  19. -- start byte must be Start character constant ‘B’
  20.  
  21. if char ~= 'B' then
  22. return nil, 'wrong start byte'
  23. end
  24.  
  25. -- read remaining line
  26.  
  27. line = port:read(26,5)
  28. if not line then
  29. return nil, 'failed to read data'
  30. end
  31. csum = 0
  32.  
  33. if line:sub(23, 23) ~= 'T' then
  34. return nil, 'End character constant T not found'
  35. end
  36.  
  37. csum = 0
  38.  
  39. for i = 1, 16 do
  40. val = tonumber(line:sub(i, i), 16)
  41. -- Incorporation of error in telegram for Testing LRC-checksum
  42. if math.random(1 ,16) > 15 then
  43. end
  44. csum = bit.bxor(csum, cdOutChars[val+1])
  45. end
  46.  
  47. --// Add constants, 'B' at beginning + separation char '=' + 'T' in the end
  48. --lrc ^= 0x0B ^ 0x0A ^ 0x0A;
  49.  
  50. csum = bit.bxor(csum, 0x0B)
  51. csum = bit.bxor(csum, 0x0A)
  52. csum = bit.bxor(csum, 0x0A)
  53.  
  54. Tagtype = line:sub(22, 22)
  55.  
  56. if Tagtype == '1' then
  57. --lrc ^= 1;
  58. csum = bit.bxor(csum, 1)
  59. end
  60.  
  61. if Tagtype == '2' then
  62. --lrc ^= 2;
  63. csum = bit.bxor(csum, 2)
  64. end
  65.  
  66. if Tagtype == '3' then
  67. --lrc ^= 3;
  68. csum = bit.bxor(csum, 3)
  69. end
  70.  
  71. if Tagtype == '4' then
  72. --lrc ^= 4;
  73. csum = bit.bxor(csum, 4)
  74. end
  75.  
  76. if Tagtype == '5' then
  77. --lrc ^= 5;
  78. csum = bit.bxor(csum, 5)
  79. end
  80.  
  81. if Tagtype == '6' then
  82. --lrc ^= 6;
  83. csum = bit.bxor(csum, 6)
  84. end
  85.  
  86. for i = 18, 22 do
  87. val = tonumber(line:sub(i, i), 16)
  88. csum = bit.bxor(csum, val)
  89. end
  90.  
  91. csum = bit.rshift(csum, 1)
  92.  
  93. -- verify checksum
  94. if csum ~= tonumber(line:sub(24, 24), 16) then
  95. return nil, 'invalid checksum'
  96. end
  97.  
  98. -- return ID
  99.  
  100. return line:sub(1, 16)
  101. end
  102. end
  103.  
  104. id , errorstatus = readid(port)
  105.  
  106. if errorstatus ~= nil then
  107. if errorstatus ~= 'timeout' then
  108. log ('error status', errorstatus)
  109. else
  110. end
  111.  
  112. end
  113.  
  114. if id then
  115. log('RFID', id)
  116. end