Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def print_reader_info(reader_addr):

    # 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_info = get_reader_info(runner)
        print('Reader info: type %s version %d.%d' %
              (reader_info.type.name, reader_info.firmware_version[0],
               reader_info.firmware_version[1]))
        print('RF info: power %d dB ' % (reader_info.power))
        print(
            'Frequency info: region %s, %.1f-%.1f MHz' %
            (reader_info.frequency_band.name, reader_info.get_min_frequency(),
             reader_info.get_max_frequency()))
    except ValueError as e:
        print('Unknown reader type: {}'.format(e))
        sys.exit(1)

    transport.close()
Ejemplo n.º 4
0
def write_tag_value(reader_addr, tag_value):

    # 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:
        write_tag_status = write_epc(runner, tag_value)
        if write_tag_status == 0:
            print('Tag written successfully')
        elif write_tag_status == 0xFA:
            print('Error: Poor communication, try repositioning the tag')
        elif write_tag_status == 0xFB:
            print('Error: No tags found, is a tag in place?')
    except ValueError as e:
        print('Could not write value: {}'.format(e))
        sys.exit(1)

    transport.close()
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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()
Ejemplo n.º 7
0
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))
Ejemplo n.º 8
0
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)
get_inventory_uhfreader18 = ReaderCommand(G2_TAG_INVENTORY)

#transport = MockTransport(bytearray.fromhex('1100210000160c034e001e0a01000000e651' + '1500010301010c0000000000000000000003136bb1a51500010301010c3039606303c74380001a055940f93e1500010301010c49440000000000000a0003346457660d000103010104003230386da3d20700010101001e4b'))
#transport = SerialTransport(auto_connect=True)
#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)

reader_info = ReaderInfoFrame(runner.run(get_reader_info))
print('Reader info: type %s version %d.%d, power %d dB ' %
      (reader_info.type.name, reader_info.firmware_version[0],
       reader_info.firmware_version[1], reader_info.power))
print('Frequency info: region %s, %.1f-%.1f MHz' %
      (reader_info.frequency_band.name, reader_info.get_min_frequency(),
       reader_info.get_max_frequency()))

#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())
Ejemplo n.º 9
0
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()