-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserialCommunication.py
More file actions
140 lines (104 loc) · 4.6 KB
/
serialCommunication.py
File metadata and controls
140 lines (104 loc) · 4.6 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import serial
import time
import numpy as np
import os
import matplotlib.pyplot as plt
import scipy.stats as stats
import Utils.ArtificialTimestamp as ArtificialTimestamp
import Utils.free_serial_port_finder as free_serial_port_finder
# Initialize the matrix and array for the data
data_matrix = []
data_array = []
time_array = []
# Initialize variables for time evaluation
start_time = time.time()
current_time = time.time()
evaluation_time = []
serial_port=free_serial_port_finder.findSerialPort()
if serial_port:
print("Serial port found: ", serial_port)
else: print("No serial port found")
# Open the serial connection and set parameters for non-blocking reading
# Change the serial port if it is different
ser = serial.Serial(port=serial_port, baudrate=115200,dsrdtr=False)
ser.setRTS(False)
ser.setDTR(False)
if ser.isOpen():
print("Serial connection established")
# Send the start command to the microcontroller
time.sleep(3)
ser.write(b'F')
time.sleep(2)
# Wait for the microcontroller to send current mode and data rate
while True:
if ser.in_waiting > 0:
line = ser.readline().decode('utf-8').strip()
if line == "START":
print("START command received!")
break
# Wait for the microcontroller to send current mode and data rate
while True:
if ser.in_waiting > 0:
current_mode = ser.readline().decode('utf-8').strip()
print("Current mode: " + current_mode)
k_value = float(ser.readline().decode('utf-8').strip())
print("K value: ", k_value)
offset = float(ser.readline().decode('utf-8').strip())
print("O value: ", offset)
data_rate = int(ser.readline().decode('utf-8').strip())
print("Data rate: ", data_rate)
factor= float(ser.readline().decode('utf-8').strip())
print("Conversion factor: ", factor)
print("\nStarting data acquisition...")
break
try:
while True:
if ser.in_waiting > 0:
start_byte = ser.read(1) # Read the start byte
if start_byte == b'\xCC': # Verify the start byte
high_byte = ser.read(1) # Read the high byte
low_byte = ser.read(1) # Read the low byte
measurement = (ord(high_byte) << 8) | ord(low_byte) # Merge the bytes
data_array.append(measurement) # Add the measurement to the array
if len(data_array) == data_rate: # If the array has reached the desired length
data_matrix.append(data_array) # Add the array to the matrix
data_array = [] # Reset the array
time_array.append(time.strftime("%H:%M:%S"))
current_time = time.time()
end=current_time
evaluation_time.append(current_time - start_time)
start_time = current_time
except KeyboardInterrupt:
# If the user stops the program
print("Data acquisition stopped by the user")
finally:
ser.close() # Close the serial connection
#remove the first 4 rows of data_matrix
data_matrix = data_matrix[2:]
#remove the first 4 rows of time_array
time_array = time_array[2:]
# Print the current mode and data rate
utils = "Current measure: " + str(current_mode) + "\n" + "Gain: " + str(k_value) + "\n" + "Offset: " + str(
offset) + "\n" + "Array length (Sample rate): " + str(data_rate) + "\n" + "factor: " + str(
factor)
'''
# Saving the last timestamp for dynamic plot
last_timestamp=time.strftime(time_array[-1],"%H:%M:%S")
end=time.struct_time(last_timestamp[:6]+(last_timestamp.tm_sec+1,))
endtimeline=time.strftime(end,"%H:%M:%S")'''
#cast data_matrix to str
data_matrix = np.array(data_matrix).astype(str)
# Merging the time array with the data matrix (time array as first column)
saving_matrix = np.column_stack((time_array, data_matrix))
# Create the filename with the current date and time
filename = "dataStorage_" + time.strftime("%Y-%m-%d_%H-%M-%S") + ".ds32"
# Save the data to the file
np.savetxt(filename, saving_matrix, delimiter=' ', comments='', fmt='%s', header=utils, encoding='utf-8')
print("\n\n\n\n\nData saved in '" + filename + "'")
end=ArtificialTimestamp.getLastTimestamp(time_array[-1])
# Adding the last timestamp for dynamic plot in the file
with open(filename, "a") as file:
file.write(str(end))
# Evaluating the mean time for each data acquisition
mean_time = np.mean((evaluation_time[1:]))
print("Mean time: ", mean_time)