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()
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 for tag in g2_response.get_tag(): print('Antenna %d: EPC %s, RSSI %s' % (tag.antenna_num, tag.epc.hex(), tag.rssi)) transport.close()
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_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