forked from wabson/chafon-rfid
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite-tag.py
More file actions
59 lines (44 loc) · 1.97 KB
/
write-tag.py
File metadata and controls
59 lines (44 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# Basic example to write a single tag with an EPC code, which must be a multiple of two bytes
import sys
from math import floor
from chafon_rfid.base import CommandRunner, ReaderCommand, ReaderResponseFrame
from chafon_rfid.command import G2_WRITE_EPC
from chafon_rfid.transport import TcpTransport
from chafon_rfid.transport_serial import SerialTransport
TCP_PORT = 6000
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 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()
if __name__ == "__main__":
if len(sys.argv) >= 2:
write_tag_value(sys.argv[1], list('1234'.encode('ascii')))
else:
print('Usage: {0} <reader-address>'.format(sys.argv[0]))