-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathucSerialComm.py
47 lines (37 loc) · 1.03 KB
/
ucSerialComm.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
35
36
37
38
39
40
41
42
43
44
#ucComm
import serial
import time
def checksum(sentence):
calc_cksum = 0
for s in sentence:
calc_cksum ^= ord(s)
""" Return the nmeadata, the checksum from
sentence, and the calculated checksum
"""
return calc_cksum
class ucComm:
def __init__(self):
self.ser = serial.Serial('/dev/ttyAMA0', 9600)
def send(self,value):
cs = checksum(value)
value += '*'+str(cs)+'\n'
self.ser.write(value.encode('ascii'))
def msgWaiting(self):
return self.ser.in_waiting > 0
# return msg
def read(self):
value = ""
success = True
while self.msgWaiting():
try:
data = self.ser.readline().decode()
l = data[:-1].split('*')
value = l[0]
crc = int(l[1])
calccrc =checksum(value)
success= (calccrc == crc)
except:
success = False
value = ""
break
return (success,value)