Skip to content

Commit

Permalink
add crc and checksum in default protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Jan 15, 2024
1 parent adc1143 commit e46746c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
9 changes: 5 additions & 4 deletions COMTool/plugins/crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
]

def crc16(array):
def crc16(array, crc=0x0000):
'''
crc16 IBM
crc16 IBM if crc is 0x0000
crc16 MODBUS if crc is 0xffff
'''
crchi = 0x00
crclo = 0x00
crchi = crc >> 8
crclo = crc & 0xff
for i in range(0, len(array)):
crcindex = crchi ^ array[i]
crchi = crclo ^ auchCRCHi[crcindex]
Expand Down
25 changes: 23 additions & 2 deletions COMTool/plugins/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,36 @@
protocols_dir = os.path.join(parameters.dataPath, "protocols")

default = '''
def decode(data):
def decode(data:bytes) -> bytes:
return data
def encode(data):
def encode(data:bytes) -> bytes:
return data
'''

add_crc16 = '''
def decode(data:bytes) -> bytes:
return data
def encode(data:bytes) -> bytes:
crc_bytes = pack("<H", crc.crc16(data))
return data + crc_bytes
'''

add_sum = '''
def decode(data:bytes) -> bytes:
return data
def encode(data:bytes) -> bytes:
return data + bytes([sum(a) % 256])
'''


defaultProtocols = {
"default": default,
"add_crc16": add_crc16,
"add_sum": add_sum,
}
ignoreList = ["maix-smart"]

Expand Down

0 comments on commit e46746c

Please sign in to comment.