-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDCM_Serial_Communication.py
More file actions
56 lines (44 loc) · 1.53 KB
/
DCM_Serial_Communication.py
File metadata and controls
56 lines (44 loc) · 1.53 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
import serial
import time
from serial.tools import list_ports
import struct
port = None
baudrate = 115200
for p in list_ports.comports():
if 'J-Link' in p.__str__():
port = p
break
port_name = port.device
print('here')
test_code, set_code, echo_code = 10,20,30
def echo_params():
### ECHO_PARAMS
with serial.Serial(port=port_name, baudrate=baudrate) as device:
params = [1,1,1,25,25,25,25,25,25,1,60,60,100,100,200,120,0,30,8,5]
params = struct.pack("<"+"B"*10+"H"*5, *params)
dat = serial.to_bytes([test_code, set_code]) + params
bytes_written = device.write(dat)
print('done writing ECHO_PARAMS')
def set_params():
### SET_PARAMS
with serial.Serial(port=port_name, baudrate=baudrate) as device:
dat = [test_code, set_code] + [i for i in range(20)]
dat = serial.to_bytes(dat)
bytes_written = device.write(dat)
print('SET_PARAMS complete, written ', bytes_written, 'bytes')
def read_params(n):
with serial.Serial(port=port_name, baudrate=baudrate) as device:
print('beginning read')
bytes_read = device.read(n)
print('bytes_read')
for byt in bytes_read:
print(byt)
set_params()
#echo_params()
#read_params(1)
###PROBLEMS
#Program gets stuck at device.read()
#Warning: Edit-time syntax highlighting disabled for performance on State SET_PARAMS. Character count 1085 exceeds 1000.
#Consider using a MATLAB Function.
#Sometimes to actually get the serial port to read the new data we must read twice, if you read just once you get the old result,
#I tried adding some delay as well, but unfortunately that did not work