-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows_read_rpm.py
More file actions
69 lines (55 loc) · 1.99 KB
/
windows_read_rpm.py
File metadata and controls
69 lines (55 loc) · 1.99 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
import serial
import time
import signal
# Based on : https://petrimaki.com/2013/04/28/reading-arduino-serial-ports-in-windows-7/
ctrl_c = 0
def signal_handler(signal, frame):
global ctrl_c
ctrl_c = 1
print("CTRL C\n")
signal.signal(signal.SIGINT, signal_handler)
try:
arduino1 = serial.Serial('COM3', 115200, timeout=0)
except:
print("Failed to access Arduino1")
exit()
try:
arduino2 = serial.Serial('COM4', 115200, timeout=0)
except:
print("Failed to access Arduino2")
exit()
output_txt_prefix = "SWOD_test_"
output_file_name = output_txt_prefix + datetime.datetime.now().isoformat('T')[:-7] #remove the last 7 characters, which are the fractions of a second
output_file = open(output_file_name, "w+")
print("Writing to file: %s" % output_file_name)
print("Begin reading (there is a slight delay)")
print("time, [RPM1], [RPM2], amps ")
writing_delay = 5.0 #when initially reading serial ports in, there are transient errors
rpm_line = ""
amp_line = ""
new_data = False
start = time.time()
while (ctrl_c == 0):
sl = select.select([arduino1, arduino2], [], [], 0.0)[0]
#read from arduino RPM
if (arduino1 in sl):
rpm_line = arduino1.readline()
rpm_line = rpm_line.decode("utf-8",errors="replace").replace('\n',' ').replace('\r','')
new_data = True
#read from arduino Thermo1
if (arduino2 in sl):
amp_line = arduino2.readline()
amp_line = amp.decode("utf-8",errors="replace").replace('\n',' ').replace('\r','')
new_data = True
#If we just read something in, print it out!
if (new_data):
stamp = time.time() - start
output_string = ("%.4f, %s, %s \n" % (stamp, rpm_line, amp_line))
#amp_normal = 906
#amp_slope = 300.0/ 0.625 / 32768 * 6.144 / 6
print(" ", end='\r')
if(stamp > writing_delay):
output_file.write(output_string)
new_data = False
arduino1.close()
arduino2.close()