@@ -19,7 +19,7 @@ class SerialPort:
1919 8 data bits, 1 stop bit, no parity
2020 """
2121
22- def __init__ (self , serial_port : str , baudrate : int , timeout : int = 15 ):
22+ def __init__ (self , serial_port : str , baudrate : int , timeout : int = 5 ):
2323 """
2424 :param serial_port: full name of the serial device port
2525 :param baudrate: buadrate [bps]
@@ -75,20 +75,21 @@ def send(
7575 raise OSError ("Serial port is closed" )
7676
7777 try :
78- time .sleep (0.25 )
7978 self .serial_port .write_timeout = self ._timeout
8079 logger .info (f"[{ self .serial_port .port } ] Serial --> { message } " )
8180 line_termination : str = "\n " if add_line_termination else ""
8281 self .serial_port .write ((message + line_termination ).encode ())
82+ time .sleep (0.25 )
8383 if get_response :
84- start = time .time ()
85- while (self .serial_port .in_waiting == 0 ) and (time .time () - start < self ._timeout ):
86- # no data in buffer yet
87- time .sleep (0.1 )
88- while (self .serial_port .in_waiting > 0 ) and (time .time () - start < self ._timeout ):
89- time .sleep (0.1 )
90- response += self .serial_port .read_all ().decode ()
91- logger .info (f"Serial <-- { response } " )
84+ start = time .perf_counter ()
85+ while time .perf_counter () - start < self ._timeout :
86+ if self .serial_port .in_waiting == 0 :
87+ time .sleep (0.1 )
88+ else :
89+ response += self .serial_port .read_all ().decode ()
90+ logger .info (f"Serial <-- { response } " )
91+ if response :
92+ break
9293 return response
9394
9495 except serial .SerialTimeoutException as exc :
0 commit comments