##################################################################################################### # Active RFID with the Wavetrend L-RX300 and Synapse RF Engine # http://www.ns-tech.co.uk/blog/2011/03/active-rfid-with-the-wavetrend-l-rx300-and-synapse-rf-engine/ ##################################################################################################### from synapse.switchboard import * #Init def startup(): initUart(1, 57600) #Set UART1 to 57600 baud #Connect serial port to STDIN uniConnect(DS_STDIO, DS_UART1) @setHook(HOOK_STDIN) def stdinEvent(data): if (len(data) != 0): parse(data) #Convert bytes to hex def bin2hex(str): hex = "" count = len(str) index = 0 while index < count: byte = ord(str[index]) hex += bin2hex_nibble(byte >> 4) hex += bin2hex_nibble(byte) index += 1 return hex def bin2hex_nibble(nibble): hexStr = "0123456789ABCDEF" return hexStr[nibble & 0xF] #Install event handlers snappyGen.setHook(SnapConstants.HOOK_STARTUP, startup) def _nibble_to_dec(s): if s >= "0" and s <= "9": return ord(s) - ord("0") else: return ord(s) - ord("A") + 10 def _get_byte(s, i): return ord(s[i - 1]) def _sum_bytes(s, i1, i2): i = i1 r = 0 while i <= i2: r = r + ord(s[i - 1]) i = i + 1 return r def parse(s): if s[0] != "\x55": s = s[1:] if len(s) > 28: expected_checksum = ord(s[28]) actual_checksum = (_sum_bytes(s, 9, 10) + _sum_bytes(s, 14, 26)) & 255 if expected_checksum == actual_checksum: logdata = "" \ "Site: " + bin2hex(s[18:21]) + "\n" \ "TagID: " + bin2hex(s[21:25]) + "\n" \ "RSSI: " + str(ord(s[27])) + "\n" \ "Trig: " + str((not not (ord(s[9]) & 128))) + "\n" \ "--\n" rpc('\x00\x00\x01', 'logEvent', logdata) logdata = "" \ "Age: " + bin2hex(s[13:17]) + "\n" \ "---------\n" rpc('\x00\x00\x01', 'logEvent', logdata)