def write_epc(runner, value): if len(value) % 2 > 0: raise ValueError('Value must be a whole number of words, i.e. multiple of two bytes') password = [0, 0, 0, 0] write_data = [floor(len(value) / 2)] + password + value write_cmd = ReaderCommand(G2_WRITE_EPC, data=write_data) write_resp = ReaderResponseFrame(runner.run(write_cmd)) return write_resp.result_status
def read_tags(reader_addr, appender): get_inventory_uhfreader18 = ReaderCommand(G2_TAG_INVENTORY) transport = TcpTransport(reader_addr=reader_addr, reader_port=TCP_PORT) set_power(transport, 30) running = True while running: start = time.time() try: now = datetime.now().time() transport.write(get_inventory_uhfreader18.serialize()) inventory_status = None while inventory_status is None or inventory_status == G2_TAG_INVENTORY_STATUS_MORE_FRAMES: resp = G2InventoryResponseFrame(transport.read_frame()) inventory_status = resp.result_status for tag in resp.get_tag(): if (is_marathon_tag(tag)): boat_num = str(tag.epc.lstrip('\0')) boat_time = str(now)[:12] print('{0} {1}'.format(boat_num, boat_time)) if appender is not None: appender.add_row([boat_num, boat_time, '', '']) else: print("Non-marathon tag 0x%s" % (binascii.hexlify(tag.epc))) #print "received %s tags" % (resp.num_tags) except KeyboardInterrupt: running = False print("KeyboardInterrupt") except socket.error as err: print('Unable to connect to reader') continue end = time.time() #print("elapsed time %.2f" % (end - start)) try: time.sleep(DELAY) except KeyboardInterrupt: running = False print("KeyboardInterrupt") transport.close()
def test_tag_inventory(): transport = MockTransport(bytearray.fromhex(RESP_TAGS_1)) command = ReaderCommand(G2_TAG_INVENTORY) runner = CommandRunner(transport) frame = G2InventoryResponseFrame(runner.run(command)) assert frame.result_status == 3 assert frame.resp_cmd == 0x01 assert frame.reader_addr == 0 assert frame.num_tags == 1 tag = next(frame.get_tag()) assert tag.epc == bytearray.fromhex('000000000000000000000313') assert tag.rssi is None assert tag.antenna_num == 1
def test_reader_info_frame(): transport = MockTransport(bytearray.fromhex(RESP_READER_INFO)) get_reader_info = ReaderCommand(CF_GET_READER_INFO) runner = CommandRunner(transport) reader_info = ReaderInfoFrame(runner.run(get_reader_info)) assert reader_info.result_status == 0 assert reader_info.resp_cmd == 0x21 assert reader_info.reader_addr == 0 assert reader_info.data == bytearray.fromhex('00160c034e001e0a01000000') assert reader_info.firmware_version == bytearray([0, 22]) # 0x4E = 0b 01 00 1110 Max # 0x00 = 0b0 Min assert reader_info.frequency_band == ReaderFrequencyBand.EU assert reader_info.get_max_frequency() == 865.1 + 0.2 * 14 assert reader_info.get_min_frequency() == 865.1
def test_multiple_tags(): transport = MockTransport(bytearray.fromhex(RESP_MULTIPLE_TAGS)) command = ReaderCommand(G2_TAG_INVENTORY) runner = CommandRunner(transport) response = G2InventoryResponse(runner.run(command)) frame_generator = response.get_frame() frame = next(frame_generator) assert frame.result_status == 3 assert frame.resp_cmd == 0x01 assert frame.reader_addr == 0 assert frame.num_tags == 2 tag_generator = frame.get_tag() tag1 = next(tag_generator) assert tag1.epc == bytearray.fromhex('000000000000000000000313') assert tag1.rssi is None assert tag1.antenna_num == 1 tag2 = next(tag_generator) assert tag2.epc == bytearray.fromhex('000000000000000000000314') assert tag2.rssi is None assert tag2.antenna_num == 1
# communication with a reader. # # For readers that support RSSI values returned with the tags (e.g. fixed reader series or UHF # modules), comment out the get_inventory_uhfreader18 command and G2InventoryResponseFrame18 # format, and instead use the lines immediately preceding them (get_inventory_288 and # G2InventoryResponseFrame288) from chafon_rfid.base import CommandRunner, ReaderCommand, ReaderInfoFrame from chafon_rfid.command import CF_GET_READER_INFO, G2_TAG_INVENTORY from chafon_rfid.response import G2_TAG_INVENTORY_STATUS_MORE_FRAMES from chafon_rfid.transport import TcpTransport, MockTransport from chafon_rfid.transport_serial import SerialTransport from chafon_rfid.uhfreader18 import G2InventoryResponseFrame as G2InventoryResponseFrame18 from chafon_rfid.uhfreader288m import G2InventoryCommand, G2InventoryResponseFrame as G2InventoryResponseFrame288 get_reader_info = ReaderCommand(CF_GET_READER_INFO) get_inventory_288 = G2InventoryCommand(q_value=4, antenna=0x80) get_inventory_uhfreader18 = ReaderCommand(G2_TAG_INVENTORY) transport = SerialTransport(device='/dev/ttyUSB0') #transport = TcpTransport(reader_addr='192.168.0.250', reader_port=27011) #transport = TcpTransport(reader_addr='192.168.1.190', reader_port=6000) runner = CommandRunner(transport) #transport.write(get_inventory_288.serialize()) transport.write(get_inventory_uhfreader18.serialize()) inventory_status = None while inventory_status is None or inventory_status == G2_TAG_INVENTORY_STATUS_MORE_FRAMES: #g2_response = G2InventoryResponseFrame288(transport.read_frame()) g2_response = G2InventoryResponseFrame18(transport.read_frame()) inventory_status = g2_response.result_status
def test_reader_frame_bad_checksum(): transport = MockTransport(bytearray.fromhex(RESP_TAGS_BAD_CHECKSUM)) get_reader_info = ReaderCommand(G2_TAG_INVENTORY) runner = CommandRunner(transport) with pytest.raises(ValueError): assert ReaderResponseFrame(runner.run(get_reader_info))
def get_reader_info(runner): get_reader_info = ReaderCommand(CF_GET_READER_INFO) reader_info = ReaderInfoFrame(runner.run(get_reader_info)) return reader_info
def read_tags(reader_addr, appender): # transport = TcpTransport(reader_addr=reader_addr, reader_port=TCP_PORT) # transport = SerialTransport(device='/dev/ttyS0') # transport = SerialTransport(device='/dev/ttyAMA0') # transport = SerialTransport(device='/dev/ttyUSB0') if reader_addr.startswith('/') or reader_addr.startswith('COM'): transport = SerialTransport(device=reader_addr) else: transport = TcpTransport(reader_addr, reader_port=TCP_PORT) runner = CommandRunner(transport) try: reader_type = get_reader_type(runner) if reader_type == ReaderType.UHFReader18: set_answer_mode_reader_18(transport) get_inventory_cmd = ReaderCommand(G2_TAG_INVENTORY) frame_type = G2InventoryResponseFrame18 set_power(transport, 27) elif reader_type in (ReaderType.UHFReader288M, ReaderType.UHFReader288MP): set_answer_mode_reader_288m(transport) get_inventory_cmd = G2InventoryCommand(q_value=4, antenna=0x80) frame_type = G2InventoryResponseFrame set_power(transport, 27) elif reader_type in (ReaderType.UHFReader86, ReaderType.UHFReader86_1): get_inventory_cmd = G2InventoryCommand(q_value=4, antenna=0x80) frame_type = G2InventoryResponseFrame set_power(transport, 26) set_buzzer_enabled(transport, True) elif reader_type in (ReaderType.RRU9803M, ReaderType.RRU9803M_1): get_inventory_cmd = ReaderCommand(G2_TAG_INVENTORY) frame_type = G2InventoryResponseFrame18 set_power(transport, 13) else: print('Unsupported reader type: {}'.format(reader_type)) sys.exit(1) except ValueError as e: print('Unknown reader type: {}'.format(e)) sys.exit(1) verbose = False running = True response_times = [] tag_counts = {} rssi_values = {} while running: start = time.time() try: now = datetime.now().time() transport.write(get_inventory_cmd.serialize()) inventory_status = None while inventory_status is None or inventory_status == G2_TAG_INVENTORY_STATUS_MORE_FRAMES: resp = frame_type(transport.read_frame()) inventory_status = resp.result_status for tag in resp.get_tag(): tag_id = tag.epc.hex() tag_counts[tag_id] = tag_counts.get(tag_id, 0) + 1 if is_marathon_tag(tag): boat_num = (tag.epc.lstrip(bytearray( [0]))).decode('ascii') boat_time = str(now)[:12] rssi = tag.rssi if verbose: print('{0} {1} {2}'.format(boat_num, boat_time, rssi)) if appender is not None: appender.add_row([boat_num, boat_time, '', '']) else: if verbose: print("Non-marathon tag 0x%s" % (tag.epc.hex())) if tag.rssi is not None: if tag_id not in rssi_values: rssi_values[tag_id] = [] rssi_values[tag_id].append(tag.rssi) # print "received %s tags" % (resp.num_tags) except KeyboardInterrupt: running = False print("KeyboardInterrupt") except socket.error: print('Unable to connect to reader') continue end = time.time() response_times.append(end - start) # print("elapsed time %.2f" % (end - start)) try: time.sleep(DELAY) except KeyboardInterrupt: running = False print("KeyboardInterrupt") if len(tag_counts) > 0: print('Found {} tags:'.format(len(tag_counts))) for tag_id, count in tag_counts.items(): print(' {}: {} times{}'.format( tag_id, count, rssi_values and ', average RSSI: %.2f' % (statistics.mean(rssi_values[tag_id]), ) or '')) print('Performed {} inventories, average time {:.3f}'.format( len(response_times), statistics.mean(response_times))) if len(rssi_values) > 0: print('Average RSSI {:.2f} dBm'.format( statistics.mean(item for sublist in rssi_values.values() for item in sublist))) else: print('No tags found') transport.close()
def set_answer_mode_reader_288m(transport): return run_command(transport, ReaderCommand(CF_SET_WORK_MODE_288M, data=[0]))
def set_answer_mode_reader_18(transport): return run_command(transport, ReaderCommand(CF_SET_WORK_MODE_18, data=[0] * 6))
def set_accousto_optic_times(transport, active_time, silent_time, times): return run_command( transport, ReaderCommand(CF_SET_ACCOUSTO_OPTIC_TIMES, data=[active_time, silent_time, times]))
def set_buzzer_enabled(transport, buzzer_enabled): return run_command( transport, ReaderCommand(CF_SET_BUZZER_ENABLED, data=[buzzer_enabled and 1 or 0]))
def set_power(transport, power_db): return run_command(transport, ReaderCommand(CF_SET_RF_POWER, data=[power_db]))
def set_power(transport, power_db): set_power = ReaderCommand(CF_SET_RF_POWER, data=[power_db]) transport.write(set_power.serialize()) status = ReaderResponseFrame(transport.read_frame()).result_status return status