9
9
import os , sys , csv , re , math
10
10
import numpy as np
11
11
12
+ # flag for if we should stop the program
12
13
stop_requested = False
14
+ # bc channel corrective value
13
15
bc_correction = 0.88
16
+ # instrument dictionary - initialized instruments are automatically added
14
17
instruments = []
15
18
19
+ # generate a serial connection with DEVICE over baudrate BAUDRATE
16
20
def serialGeneric (device ,baudrate ):
17
21
ser = serial .Serial (port = device ,
18
22
baudrate = baudrate ,
@@ -22,6 +26,8 @@ def serialGeneric(device,baudrate):
22
26
)
23
27
return ser
24
28
29
+
30
+ # signal handler for ctrl-c (quit)
25
31
def sig_handler (signum , frame ):
26
32
sys .stdout .write ("handling signal: %s\n " % signum )
27
33
sys .stdout .flush ()
@@ -30,6 +36,8 @@ def sig_handler(signum, frame):
30
36
stop_requested = True
31
37
32
38
class Instrument (object ):
39
+
40
+ # initialize instrument with serial connection and add it to instrument dict
33
41
def __init__ (self , comport , baudrate ):
34
42
if comport :
35
43
self .serial = serialGeneric (comport , baudrate )
@@ -42,6 +50,7 @@ def __str__(self):
42
50
def __repr__ (self ):
43
51
return self .__str__ ()
44
52
53
+ # dumps the serial output SER, which was received at datetime DT, into rawraw_data.csv
45
54
def dumpserial (self , ser , dt ):
46
55
with open (filepath + 'rawraw_data.csv' , mode = 'ab' ) as rawFile :
47
56
fcntl .flock (rawFile , fcntl .LOCK_EX )
@@ -50,6 +59,7 @@ def dumpserial(self, ser, dt):
50
59
fcntl .flock (rawFile , fcntl .LOCK_UN )
51
60
return
52
61
62
+ # continually call Instr get_values() func to receive serial output until STOP_REQUESTED
53
63
def run (self , queue ):
54
64
while not stop_requested :
55
65
values = self .get_values ()
@@ -62,6 +72,9 @@ def run(self, queue):
62
72
if stop_requested :
63
73
self .serial .close ()
64
74
75
+ # def get_values(self):
76
+ # exists for each defined instrument below, parses serial output to a datetime and value (sometimes includes attn and flow)
77
+
65
78
# BC Instr
66
79
67
80
class AE33_Instrument (Instrument ):
@@ -411,21 +424,25 @@ def get_values(self):
411
424
return None
412
425
return nox_values
413
426
427
+ # main method for use when file is run through multiprocessing (like in this ../cli.py)
414
428
def main_wrapper (q ):
415
429
global filepath
416
430
filepath = q .get ()
417
431
432
+ # create output dir using filepath (sent to queue Q from plotter.py)
418
433
if not os .path .exists (os .path .dirname (filepath )):
419
434
try :
420
435
os .makedirs (os .path .dirname (filepath ))
421
436
except OSError as exc : # Guard against race condition
422
437
if exc .errno != errno .EEXIST :
423
438
raise
424
439
440
+ # initialize rawraw_data.py header
425
441
with open (filepath + 'rawraw_data.csv' , mode = 'wb' ) as rawFile :
426
442
rawData = csv .writer (rawFile , delimiter = ',' , quotechar = '"' , quoting = csv .QUOTE_MINIMAL )
427
443
rawData .writerow (['Timestamp' , 'Instr' , 'S#' , 'Message' ])
428
444
445
+ # dictionary mapping serial numbers to Instrument objects ** CHANGE THIS TO ADD MORE INSTRUMENTS **
429
446
comport_dict = {
430
447
'FTXP6UA4' : AE33_Instrument ,
431
448
'FTXP9NHN' : AE16_Instrument ,
@@ -451,6 +468,8 @@ def main_wrapper(q):
451
468
comport_pattern = re .compile ('\/dev\/cu\.usbserial(\-(.)*)?$' )
452
469
serial_comlist = []
453
470
471
+ # initialize instrument objects for each serial connection (view in terminal with ls /dev/cu.*) that matches
472
+ # comport_pattern (/dev/cu.usbserial- or /dev/cu.wchusbsereial for ABCD)
454
473
for element in comlist :
455
474
if comport_pattern .match (element .device ):
456
475
try :
0 commit comments