@@ -14,7 +14,7 @@ def __init__(self, port, baudrate=115200, timeout=5,
1414 self .baudrate = baudrate
1515 self .timeout = timeout
1616 self ._parent = parent
17-
17+
1818 if self ._parent is None :
1919 import logging
2020 self ._logger = logging .getLogger (__name__ )
@@ -183,28 +183,13 @@ def _process_commands(self):
183183 if time .time () - timeLastTrasmissionWasAsked > 0.1 : #timeout to wait until ESP32 responds
184184 lastTransmisionSuccess = True # something went wrong, we have to free serial now!
185185 except Exception as e :
186- time .sleep (0.1 ) # add an artifical delay in case the esp32 did reply but we didn't catch it and then free the serial connection
186+ time .sleep (0.01 ) # add an artifical delay in case the esp32 did reply but we didn't catch it and then free the serial connection
187187 lastTransmisionSuccess = True
188188 if not self .command_queue .empty () and not reading_json and lastTransmisionSuccess :
189189 currentIdentifier , command = self .command_queue .get ()
190-
191190 if self .DEBUG : self ._logger .debug ("Sending: " + str (command ))
192- json_command = json .dumps (command )
193- try :
194- self .ser .write (json_command .encode ('utf-8' ))
195- qeueIdSuccess [str (currentIdentifier )]= (False , time .time ())
196- except Exception as e :
197- try :
198- self .ser .write_timeout = 1
199- self .ser .write (json_command .encode ('utf-8' ))
200- self .ser .write_timeout = self .write_timeout
201- except Exception as e :
202- self ._logger .error ("Writing failed in sreial, %s" , e )
203- try :self .ser .write (b'\n ' )
204- except :
205- self ._logger .error ("Break the loop in serial" )
206- break
207-
191+ qeueIdSuccess [str (currentIdentifier )]= (False , time .time ())
192+
208193 # device not ready yet
209194 if self .ser is None :
210195 self .is_connected = False
@@ -320,6 +305,12 @@ def sendMessage(self, command, nResponses=1, timeout = 20):
320305 identifier = self ._generate_identifier ()
321306 command ["qid" ] = identifier
322307 self .command_queue .put ((identifier , command ))
308+ try :
309+ json_command = json .dumps (command )+ "\n "
310+ self .ser .write (json_command .encode ('utf-8' ) )
311+ except Exception as e :
312+ if self .DEBUG : self ._logger .error (e )
313+ return "Failed to Send"
323314 self .commands [identifier ]= command
324315 if nResponses <= 0 or not self .is_connected or not type (self .ser .BAUDRATES ) is tuple :
325316 return identifier
0 commit comments