-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_write.py
More file actions
96 lines (81 loc) · 2.49 KB
/
serial_write.py
File metadata and controls
96 lines (81 loc) · 2.49 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
# NAME: serial_write.py
# AUTHOR: Matthew Miller
# PURPOSE: Records data from iMet sensor into a file called IMETDATAXX.CSV.
# If the program detects that an ADC has been attached, the program
# will send ADC data, immediately followed by iMet data through the
# serial port. This is intended to be used with an xBee in coordination
# with a ground station computer. The ground station computer will be
# set up to display a live graph of the data.
import os
import time
import serial
from gpiozero import LED, Button
#check if Arduino ADC is connected via USB
#if not used, program continues with only iMET recording
try:
ser_ADC = serial.Serial(
port='/dev/ARD',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2
)
use_ADC = 1
except: #error if no adc attached.
print("No ADC attached. Not using.\n")
use_ADC = 0
ser_xBee = serial.Serial(
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
#check if iMet attached
try:
ser_iMET = serial.Serial(
port='/dev/IMET',
baudrate = 57600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
except:
print("no iMET attached, cannot continue. \n")
exit()
collect_ADC = LED(27)
stop_button = Button(25, pull_up=False)
counter=0#potentially unneccessary
#open iMET file
#first get filename
init_iMET_filename = "IMETDATA"
for i in range(100):
to_append = str(i)+".CSV"
iMET_appended = init_iMET_filename + to_append
file_exists = os.path.isfile(iMET_appended)
if file_exists == 0: #if file doesn't exist exit loop and create it
break
while stop_button.is_pressed == 0:
if use_ADC == 1:
iMET_file = open(iMET_appended, "a+") #open new iMET file appending
data_iMET = ser_iMET.readline()
collect_ADC.on() #tell arduino to grab data
collect_ADC.off()
data_ADC = ser_ADC.readline()
data_all = data_ADC + data_iMET
ser_xBee.write(data_all)
iMET_file.write(data_iMET)
iMET_file.write("\n")
iMET_file.close()
else:
iMET_file = open(iMET_appended, "a+") #open new iMET file appending
data_iMET = ser_iMET.readline()
iMET_file.write(data_iMET)
iMET_file.write("\n")
iMET_file.close()
print(counter)
counter += 1
print("done\n")