-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrx_python_3.py
68 lines (56 loc) · 2.05 KB
/
rx_python_3.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import serial
import time
class Receiver:
START_BYTE = 0xA8
FRAME_LEN = 29
NUM_CHANNELS = 12
def __init__(self, port):
self._frame = bytearray()
self.ser = serial.Serial(
port,
baudrate=115200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE
)
self.frames = self.ser.read(size=Receiver.FRAME_LEN*3)
self.data_received(self.frames)
def data_received(self, data):
self._in_frame = False
for b in data:
if self._in_frame:
self._frame.append(b)
if len(self._frame) == Receiver.FRAME_LEN:
decoded_frame = self.decode( self._frame)
#print('decoded', decoded_frame)
self._in_frame = False
del self.frames
self.frames = self.ser.read(size=Receiver.FRAME_LEN*3)
else:
if b == Receiver.START_BYTE:
self._in_frame = True
i=0
self._frame.clear()
self._frame.append(b)
def decode(self, frame):
self.Channels = [None] * Receiver.NUM_CHANNELS
channel_sum_data=[]
for n in range(len(frame)-5):
channel_sum_data.append(frame[3+n])
val=0
for ch in range(0, Receiver.NUM_CHANNELS):
self.Channels[ch] = channel_sum_data[val]*32 + channel_sum_data[(val+1)]/8#channel_sum & 0xff #0x7ff
val=val+2
return self.Channels
def main(port='COM9'): #'/dev/ttyS0'
sumd = Receiver(port)
channels=sumd.Channels
#print('ch 1',channels[0],'ch 2',channels[1],'ch 3',channels[2],'ch 4',channels[3],'ch 5',channels[4],
#'ch 6',channels[5],'ch 7',channels[6],'ch 8',channels[7])
return channels
if __name__ == '__main__':
while True:
channels = main()
print(channels)
time.sleep(.3)