1717from time import sleep
1818
1919### DRIVER ATTRIBUTES ###
20- #BAUDRATE = 115200
21- BAUDRATE = 2000000
2220
2321class CommandError (Exception ):
2422 """Command error."""
@@ -75,10 +73,10 @@ class ZeroMqArduinoServer(ZeroMqServer):
7573 APPLICATION_ID = 0x01
7674 BOOTLOADER_ID = 0x80
7775
78- def __init__ (self , port : str , cmd_poll_interval = 1.0 ) -> None :
76+ def __init__ (self , port : str , cmd_poll_interval = 1.0 , baudrate = 2000000 ) -> None :
7977 super ().__init__ (cmd_poll_interval = cmd_poll_interval )
8078
81- self ._com = Serial (baudrate = BAUDRATE , timeout = 0.1 )
79+ self ._com = Serial (baudrate = baudrate , timeout = 0.1 )
8280 self ._com .port = port
8381 self ._cmd_resp_lines = Queue ()
8482 self ._read_thread : Optional [Thread ] = None
@@ -87,16 +85,35 @@ def __init__(self, port: str, cmd_poll_interval=1.0) -> None:
8785 self .rawHistograms = 0
8886 self .hostType = TMF8829_ZEROMQ_HOST_ARDUINO_BOARD
8987 self ._result_object = bytearray ()
90-
88+
9189 @staticmethod
92- def device_connected () -> List [str ]:
90+ def print_connected_com_ports ():
91+ """
92+ print connected com ports
9393 """
94- device connected
94+ ports = list_ports .comports ()
95+ for port in ports :
96+ print (f"Com Port Device: { port .device } , Description: { port .description } , HWID: { port .hwid } " )
97+ return
9598
99+ @staticmethod
100+ def device_connected (dev = "Uno" ) -> List [str ]:
101+ """
102+ Return a list of devices that are connected.
103+ Args:
104+ dev: "Uno" for Arduino Uno,"Zero" for Arduino Zero; other devices not implemented
96105 Returns:
97106 List[str]: list ports
98107 """
99- return [e .name for e in list_ports .grep (r"USB VID:PID=2341.*" )]
108+ dl = []
109+ if dev == "Uno" :
110+ dl = [e .name for e in list_ports .grep (r"USB VID:PID=2341.*" )]
111+ elif dev == "Zero" :
112+ dl = [e .name for e in list_ports .grep (r"USB VID:PID=03EB:2157*" )]
113+ else :
114+ logging .info ("device search not implemented" )
115+
116+ return dl
100117
101118 def _clear_queue (self , queue : Queue ) -> None :
102119 """
@@ -266,7 +283,7 @@ def _reopen_communication_to_device(self) -> None:
266283 """
267284 logger .info ("--XXX Reopen device connection. XXX--" )
268285
269- self ._close_communication_to_device ()
286+ self ._com . close ()
270287 sleep (0.1 )
271288 self ._open_communication_to_device ()
272289
@@ -310,16 +327,6 @@ def stop_measurement(self) -> bool:
310327 state = line .split (b"=" )[1 ]
311328 except Exception as exc :
312329 state = FwStates .UNKNOWN .value
313-
314-
315- if state != FwStates .STOPPED .value :
316- logger .info ("Could not stop the measurement. Try a second time!!!" )
317- try : # Try once more
318- for line in self ._send_command (Commands .STOP_MEAS ):
319- if line .startswith (b"state" ):
320- state = line .split (b"=" )[1 ]
321- except Exception as exc :
322- state = FwStates .UNKNOWN .value
323330
324331 self ._meas_running = False
325332 self ._nr_subframes = 0
@@ -330,7 +337,7 @@ def stop_measurement(self) -> bool:
330337 logger .info ("{} Result processing stopped." .format (time .time ()))
331338 logger .info ("Number lost result frames are at least {}" .format (self .lost_results ))
332339 else :
333- logger .info ("Stop the measurement FAILED. Try a second time !!!" )
340+ logger .info ("Stop the measurement FAILED. Re-open com !!!" )
334341 self ._reopen_communication_to_device
335342
336343 return self ._meas_running
@@ -430,17 +437,24 @@ def set_pre_config_cmd(self, cmd:bytes) -> None:
430437#####################################################################################
431438
432439if __name__ == "__main__" :
433- import pathlib
440+
441+ BAUDRATE = 2000000
434442
435443 logging .basicConfig (level = logging .DEBUG ,format = '%(levelname)s %(name)s.%(funcName)s:%(lineno)d %(message)s' )
436-
444+ ZeroMqArduinoServer .print_connected_com_ports ()
445+
437446 com_port = ZeroMqArduinoServer .device_connected ()
438447
439448 if len (com_port ) >= 1 :
440- logging .info ("Board found Com {}" .format (com_port [0 ]))
441- server = ZeroMqArduinoServer (port = com_port [0 ], cmd_poll_interval = 0.01 )
442- server .start (cmd_addr = TMF8829_ZEROMQ_CMD_SERVER_ADDR , result_addr = TMF8829_ZEROMQ_RESULT_SERVER_ADDR )
449+ logging .info ("Arduino Uno Board found Com {}" .format (com_port [0 ]))
450+ else :
451+ logging .info ("No Arduino Uno Board found" )
452+ com_port = ZeroMqArduinoServer .device_connected ("Zero" )
453+ BAUDRATE = 1000000
443454
455+ if len (com_port ) >= 1 :
456+ server = ZeroMqArduinoServer (port = com_port [0 ], cmd_poll_interval = 0.01 , baudrate = BAUDRATE )
457+ server .start (cmd_addr = TMF8829_ZEROMQ_CMD_SERVER_ADDR , result_addr = TMF8829_ZEROMQ_RESULT_SERVER_ADDR )
444458 try :
445459 while True :
446460 server .process ()
0 commit comments