-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
36 lines (31 loc) · 1.13 KB
/
Copy pathcommand.py
File metadata and controls
36 lines (31 loc) · 1.13 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
CMD_INVENTORY = 0x01
CMD_READ_MEMORY = 0x02
CMD_WRITE_MEMORY = 0x03
CMD_READER_INFORMATION = 0x21
CMD_SET_READER_POWER = 0x2F
class Command:
def __init__(self, command: int, reader_address: int = 0xFF,
data: bytes | int | None = None):
self.command = command
self.reader_address = reader_address
self.data = data
if isinstance(data, int):
self.data = bytearray([data])
if data is None:
self.data = bytearray()
self.frame_length = 4 + len(self.data)
self.base_data = bytearray([self.frame_length, self.reader_address, self.command])
self.base_data.extend(self.data)
def serialize(self) -> bytes:
serialize = self.base_data
# Checksum CRC-16/MCRF4XX
value = 0xFFFF
for d in serialize:
value ^= d
for _ in range(8):
value = (value >> 1) ^ 0x8408 if value & 0x0001 else (value >> 1)
crc_msb = value >> 0x08
crc_lsb = value & 0xFF
serialize = serialize + bytes([crc_lsb])
serialize = serialize + bytes([crc_msb])
return serialize