Skip to content

Commit b6a1e3f

Browse files
committed
Add way to resend request if no response from esp in time?
1 parent 445e086 commit b6a1e3f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

uc2rest/mserial.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,11 @@ def sendMessage(self, command, nResponses=1, timeout = 20):
342342
except Exception as e:
343343
if self.DEBUG: self._logger.error(e)
344344
return "Failed to Send"
345-
self.commands[identifier]=command
345+
self.commands[identifier]=command # FIXME: Need to clear this after the response is received
346346
if nResponses <= 0 or not self.is_connected or self.manufacturer=="UC2Mock":
347347
return identifier
348348
t0 = time.time()
349+
timeReturnReceived = 0.3
349350
while self.running:
350351
time.sleep(0.002)
351352
if self.resetLastCommand or time.time()-t0>timeout or not self.is_connected:
@@ -358,6 +359,15 @@ def sendMessage(self, command, nResponses=1, timeout = 20):
358359
if -identifier in self.responses:
359360
self._logger.debug("You have sent the wrong command!")
360361
return "Wrong Command"
362+
if time.time()-t0>timeReturnReceived and not (identifier in self.responses and len(self.responses[identifier]) > 0):
363+
self._logger.debug("It takes too long to get a response, we will resend the last command")
364+
try:
365+
self.DEBUG=1
366+
self.serialdevice.write(json.dumps(self.commands[identifier]).encode('utf-8'))
367+
time.sleep(0.1)
368+
except Exception as e:
369+
self._logger.error("Failed to write the line in serial: "+str(e))
370+
361371

362372
def interruptCurrentSerialCommunication(self):
363373
self.resetLastCommand = True

0 commit comments

Comments
 (0)