-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathj1708.py
34 lines (31 loc) · 935 Bytes
/
j1708.py
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
class Message:
def __init__(self, mid,content,checksum,pos,length):
self.mid= mid
self.content = content
self.length = length
self.pos = pos
self.checksum = checksum
def checksum(self,array): #requires refactoring!
checksum = 0
for x in range(0, (len(array)-1)):
checksum += array[x]
checksum &= 255
checksum = 256 - checksum
return checksum
def __eq__(self,other):
return self.content == other.content
def __hash__(self):
return hash(('content',str(self.content)))
def to_dec(string):
array = []
n = 2
array = [string[i:i+n] for i in range(0,len(string),n)]
array = [int(x, 16) for x in array]
return array
def checksum(array):
checksum = 0
for x in range(0, (len(array)-1)):
checksum += array[x]
checksum &= 255
checksum = 256 - checksum
return checksum