-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_com1.py
More file actions
67 lines (52 loc) · 1.39 KB
/
serial_com1.py
File metadata and controls
67 lines (52 loc) · 1.39 KB
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
# # from curses import baudrate
# import serial
# ser=serial.Serial("/dev/ttyUSB0",baudrate=9600)
# # if ser.is_open()==False:
# ser=ser.open()
# while ser.is_open()==True:
# # ser=ser.open()
# val=ser.read()
# print(val)
# ser.close()
import serial
import struct
try:
ser = serial.Serial( # set parameters, in fact use your own :-)
port="/dev/ttyUSB0",
baudrate=9600,
bytesize=serial.SEVENBITS,
parity=serial.PARITY_EVEN,
stopbits=serial.STOPBITS_ONE
)
ser.isOpen() # try to open port, if possible print message and proceed with 'while True:'
print ("port is opened!")
except IOError: # if port is already opened, close it and open it again and print message
ser.close()
ser.open()
print ("port was already open, was closed and opened again!")
# typedef struct{
# int16_t val;
# int16_t other_val;
# int16_t other_val2;
# uint16_t checksum;
# //int16_t a;
# } SerialCommand2;
while True:
val=ser.readline()
# print(val)
# https://docs.python.org/3/library/struct.html#format-characters
"""
page is our c/cpp struct valuable datas
"""
value=struct.unpack_from("HHHH",val)
# print(value)
if value[3]==25:
print(value[0:3])
elif value[3]==55:
print(value[0:3])
# if value[2]==
else:
print("not true val")
# print(value[0:3])
# print(value)
ser.close()