@@ -54,10 +54,13 @@ def __init__ (self, handler, device='auto'):
54
54
self .device = device
55
55
self .connected = False
56
56
self .handler = handler
57
- self .serial_out = queue . Queue ()
57
+ self .uart = None
58
58
59
59
def send_led (self , message : bytes ):
60
- self .serial_out .put (message )
60
+ # logging.debug(f'Sending to serial: {message}')
61
+ if self .connected :
62
+ return self .uart .write (message )
63
+ return None
61
64
62
65
def run (self ):
63
66
while True :
@@ -73,27 +76,28 @@ def run(self):
73
76
time .sleep (1 )
74
77
continue
75
78
logging .info (f'Opening serial port { serialport } ' )
76
- uart = serial .Serial (serialport , 38400 , timeout = 2.5 ) # 0-COM1, 1-COM2 / speed /
79
+ self .uart = serial .Serial (serialport , 38400 ) # 0-COM1, 1-COM2 / speed /
80
+ # self.uart = serial.Serial(serialport, 38400, timeout=2.5) # 0-COM1, 1-COM2 / speed /
77
81
if os .name == 'posix' :
78
82
logging .debug (f'Attempting to lock { serialport } ' )
79
- fcntl .flock (uart .fileno (), fcntl .LOCK_EX | fcntl .LOCK_NB )
83
+ fcntl .flock (self . uart .fileno (), fcntl .LOCK_EX | fcntl .LOCK_NB )
80
84
logging .debug (f'Flushing input on { serialport } ' )
81
- uart .flushInput ()
82
- uart .write (b'U\xaa U\xaa U\xaa U\xaa ' )
85
+ self . uart .flushInput ()
86
+ self . uart .write (b'U\xaa U\xaa U\xaa U\xaa ' )
83
87
time .sleep (1 )
84
- uart .write (b'\xaa U\xaa U\xaa U\xaa U' )
88
+ self . uart .write (b'\xaa U\xaa U\xaa U\xaa U' )
85
89
time .sleep (1 )
86
- uart .write (b'\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' )
90
+ self . uart .write (b'\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' )
87
91
self .connected = True
88
92
except Exception as e :
89
93
logging .info (f'ERROR: Cannot open serial port { serialport } : { str (e )} ' )
90
94
self .connected = False
91
95
time .sleep (0.1 )
92
96
else :
93
97
try :
94
- while uart . inWaiting () :
98
+ while True :
95
99
# logging.debug(f'serial data pending')
96
- raw_message = uart .readline ()
100
+ raw_message = self . uart .readline ()
97
101
try :
98
102
message = raw_message .decode ("ascii" )[1 : - 3 ]
99
103
#if DEBUG:
@@ -103,12 +107,6 @@ def run(self):
103
107
message = ""
104
108
except Exception as e :
105
109
logging .info (f'Exception during message decode: { str (e )} ' )
106
- time .sleep (0.001 )
107
- if not self .serial_out .empty ():
108
- data = self .serial_out .get ()
109
- self .serial_out .task_done ()
110
- # logging.debug(f'Sending to serial: {data}')
111
- uart .write (data )
112
110
except Exception as e :
113
111
logging .info (f'Exception during serial communication: { str (e )} ' )
114
112
self .connected = False
0 commit comments