-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpy_serial.py
More file actions
86 lines (68 loc) · 2.32 KB
/
py_serial.py
File metadata and controls
86 lines (68 loc) · 2.32 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import numpy as np
import serial
import msvcrt
# Data Type
dType = {1: "TYPE_U8", 2:"TYPE_S8", 3:"TYPE_U16",
4:"TYPE_S16", 5: "TYPE_U32", 6:"TYPE_S32", 7:"TYPE_F32"}
MCU_WRITES = 87
MCU_READS = 82
# Request Type
rqType = { MCU_WRITES: "MCU Write", MCU_READS: "MCU Read"}
# Init Com Port
def SERIAL_Init(port):
global __serial
__serial = serial.Serial(port, 2000000, timeout = 1)
__serial.flush()
print(__serial.name, "Opened")
print("")
# Wait for MCU Request
def SERIAL_PollForRequest():
global requestType
global dataType
global byteLength
while(1):
if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
print("Exit program!")
exit(0)
if np.frombuffer(__serial.read(1), dtype= np.uint8) == 83:
if np.frombuffer(__serial.read(1), dtype= np.uint8) == 84:
requestType = np.frombuffer(__serial.read(1), dtype= np.uint8)
dataType = np.frombuffer(__serial.read(1), dtype= np.uint8)
byteLength = np.frombuffer(__serial.read(4), dtype= np.uint32)
dataSize = byteLength/np.dtype(SERIAL_GetDType(dataType)).itemsize
print("Request Type : ", rqType[int(requestType)])
print("Data Type : ", dType[int(dataType)])
print("Byte Length : ", int(byteLength), "Bytes")
print("Data Size : ", int(dataSize), "Data")
return [int(requestType), int(dataSize), int(dataType)]
# Read MCU Data
def SERIAL_Read():
__type = SERIAL_GetDType(dataType)
data = np.frombuffer(__serial.read(int(byteLength)), dtype = __type)
print(data)
print()
return data
# Get np.dtype from MCU
def SERIAL_GetDType(__dataType : int):
__type = int(__dataType)
if __type == 1:
__type = np.uint8
elif __type == 2:
__type = np.int8
elif __type == 3:
__type = np.uint16
elif __type == 4:
__type = np.int16
elif __type == 5:
__type = np.uint32
elif __type == 6:
__type = np.int32
elif __type == 7:
__type = np.float32
return __type
# Writes data to MCU
def SERIAL_Write(data : np.array):
print(data)
print()
data = data.tobytes()
__serial.write(data)