22The main module for the LabJack DAQ source.
33"""
44
5+ import argparse
56import sys
67import time
78from typing import TypedDict , cast
@@ -76,7 +77,7 @@ class DAQ_SEND_MESSAGE_TYPE(TypedDict):
7677
7778# Function to pass to the callback function. This needs have one
7879# parameter/argument, which will be the handle.
79- def read_data (handle , num_addresses , scans_per_read , scan_rate ):
80+ def read_data (handle , num_addresses , scans_per_read , scan_rate , * , quiet = False , no_built_in_log = False ):
8081 # Converting to nanoseconds to avoid floating point inaccuracy.
8182 READ_PERIOD : int = int (1 / cast (int , scan_rate ) * 1000000000 )
8283
@@ -86,8 +87,12 @@ def read_data(handle, num_addresses, scans_per_read, scan_rate):
8687 # Use current time to have a unique starting point on every collection, ns to prevent floating point error.
8788 relative_last_read_time : float = time .time_ns ()
8889
89- now = time .strftime ("%Y-%m-%d_%H-%M-%S" , time .localtime ()) # 2021-07-12_22-35-08
90- with open (f"log_{ now } .dat" , "wb" ) as log :
90+ log = None
91+ if not no_built_in_log :
92+ now = time .strftime ("%Y-%m-%d_%H-%M-%S" , time .localtime ()) # 2021-07-12_22-35-08
93+ log = open (f"log_{ now } .dat" , "wb" )
94+
95+ try :
9196 while True :
9297 rates .append (time .time ())
9398 if len (rates ) > 50 :
@@ -143,18 +148,36 @@ def read_data(handle, num_addresses, scans_per_read, scan_rate):
143148 else relative_timestamps [- 1 ] + READ_PERIOD
144149 )
145150
146- log .write (msgpack .packb (data_parsed ))
151+ if log :
152+ log .write (msgpack .packb (data_parsed ))
147153
148154 # Send data to omnibus.
149155 sender .send (CHANNEL , data_parsed )
150156
151- print (
152- f"\r Rate: { scans_per_read * len (rates ) / (time .time () - rates [0 ]): >6.0f} " ,
153- end = "" ,
154- )
157+ if not quiet :
158+ print (
159+ f"\r Rate: { scans_per_read * len (rates ) / (time .time () - rates [0 ]): >6.0f} " ,
160+ end = "" ,
161+ )
162+ finally :
163+ if log :
164+ log .close ()
155165
156166
157167def main ():
168+ parser = argparse .ArgumentParser (description = "LabJack DAQ Source" )
169+ parser .add_argument (
170+ "--quiet" ,
171+ action = "store_true" ,
172+ help = "Suppress continuous output except for errors and setup information" ,
173+ )
174+ parser .add_argument (
175+ "--no-built-in-log" ,
176+ action = "store_true" ,
177+ help = "Disable writing to the built-in log file" ,
178+ )
179+ args = parser .parse_args ()
180+
158181 try :
159182 # Open first found LabJack T7 device with any connection type and any indentifier.
160183 handle = ljm .openS ("T7" , "ANY" , "ANY" )
@@ -188,9 +211,12 @@ def main():
188211 )
189212
190213 try :
191- read_data (handle , num_addresses , config .SCANS_PER_READ , scan_rate )
214+ read_data (
215+ handle , num_addresses , config .SCANS_PER_READ , scan_rate ,
216+ quiet = args .quiet , no_built_in_log = args .no_built_in_log ,
217+ )
192218 except KeyboardInterrupt :
193- print ( "KeyboardInterrupt Triggered" )
219+ pass
194220
195221 # Stop LJM stream.
196222 print ("Stopping stream..." )
0 commit comments