Skip to content

Commit

Permalink
Use CRC-8 instead of checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerBroker2 authored Dec 20, 2019
1 parent 0fb277d commit acebf1a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
56 changes: 56 additions & 0 deletions pySerialTransfer/CRC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sys


class CRC(object):
def __init__(self, polynomial=0x9B, crc_len=8):
self.poly = polynomial & 0xFF
self.crc_len = crc_len
self.table_len = pow(2, crc_len)
self.cs_table = [' ' for x in range(self.table_len)]

self.generate_table()

def generate_table(self):
for i in range(len(self.cs_table)):
curr = i

for j in range(8):
if (curr & 0x80) != 0:
curr = ((curr << 1) & 0xFF) ^ self.poly
else:
curr <<= 1

self.cs_table[i] = curr

def print_table(self):
for i in range(len(self.cs_table)):
sys.stdout.write(hex(self.cs_table[i]).upper().replace('X', 'x'))

if (i + 1) % 16:
sys.stdout.write(' ')
else:
sys.stdout.write('\n')

def calculate(self, arr, dist=None):
crc = 0

try:
if dist:
indicies = dist
else:
indicies = len(arr)

for i in range(indicies):
crc = self.cs_table[crc ^ arr[i]]

except TypeError:
crc = self.cs_table[arr]

return crc


if __name__ == '__main__':
crc = CRC()
print(crc.print_table())
print(' ')
print(hex(crc.calculate(0x31)).upper().replace('X', 'x'))
2 changes: 1 addition & 1 deletion pySerialTransfer/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import *
from . import *
46 changes: 11 additions & 35 deletions pySerialTransfer/pySerialTransfer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import serial
from platform import system
from CRC import CRC


CONTINUE = 2
NEW_DATA = 1
NO_DATA = 0
CHECKSUM_ERROR = -1
CRC_ERROR = -1
PAYLOAD_ERROR = -2
STOP_BYTE_ERROR = -3

Expand All @@ -18,7 +19,7 @@
find_overhead_byte = 1
find_payload_len = 2
find_payload = 3
find_checksum = 4
find_crc = 4
find_end_byte = 5


Expand All @@ -43,8 +44,8 @@ def __init__(self, port_num, baud=115200):
:return: void
'''

self.txBuff = (', ' * (MAX_PACKET_SIZE - 1)).split(',')
self.rxBuff = (', ' * (MAX_PACKET_SIZE - 1)).split(',')
self.txBuff = [' ' for i in (MAX_PACKET_SIZE - 1)]
self.rxBuff = [' ' for i in (MAX_PACKET_SIZE - 1)]

self.bytesRead = 0
self.status = 0
Expand All @@ -57,6 +58,7 @@ def __init__(self, port_num, baud=115200):
else:
port_name = '/dev/ttyUSB{}'.format(port_num)

self.crc = CRC()
self.connection = serial.Serial()
self.connection.port = port_name
self.connection.baudrate = baud
Expand Down Expand Up @@ -150,32 +152,6 @@ def stuff_packet(self, pay_len):
if self.txBuff[i] == START_BYTE:
self.txBuff[i] = refByte - i
refByte = i

def checksum(self, arr, pay_len):
'''
Description:
------------
Determine the 8-bit checksum of a given number of elements of
a given array
:param arr: list - list to calculate the checksum over
:param pay_len: int - number of bytes in the payload
:return checksum: int - resulting checksum
'''

checksum = 0

for i in range(pay_len):
if type(arr[i]) == str:
checksum += ord(arr[i])
else:
checksum += int(arr[i])

checksum = ~checksum
checksum = checksum & 0xFF

return checksum

def send(self, message_len):
'''
Expand All @@ -195,7 +171,7 @@ def send(self, message_len):
try:
self.calc_overhead(message_len)
self.stuff_packet(message_len)
found_checksum = self.checksum(self.txBuff, message_len)
found_checksum = self.crc.calculate(self.txBuff, message_len)

stack.append(START_BYTE)
stack.append(self.overheadByte)
Expand Down Expand Up @@ -293,17 +269,17 @@ def available(self):
self.payIndex += 1

if self.payIndex == self.bytesToRec:
self.state = find_checksum
self.state = find_crc

elif self.state == find_checksum:##############################
found_checksum = self.checksum(self.rxBuff, self.bytesToRec)
elif self.state == find_crc:###################################
found_checksum = self.crc.calculate(self.rxBuff, self.bytesToRec)

if found_checksum == recChar:
self.state = find_end_byte
else:
self.bytesRead = 0
self.state = find_start_byte
self.status = CHECKSUM_ERROR
self.status = CRC_ERROR
return self.bytesRead

elif self.state == find_end_byte:##############################
Expand Down

0 comments on commit acebf1a

Please sign in to comment.