Skip to content

Commit 2196fd8

Browse files
authored
Merge pull request #70 from openUC2/mergemaster
Send data immediately rather than waiting for the queue
2 parents 8582e67 + d8e6dce commit 2196fd8

1 file changed

Lines changed: 8 additions & 21 deletions

File tree

uc2rest/mserial.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,11 @@ def _process_commands(self):
168168
currentIdentifier = None
169169
nLineCountTimeout = 50 # maximum number of lines read before timeout
170170
lineCounter = 0
171-
lastTransmisionSuccess = True
172-
173-
qeueIdSuccess = {}
174-
timeLastTrasmissionWasAsked=time.time()
175-
t0 = time.time()
176171
while self.running:
177172

178173
# Check if the last command went through successfully
179-
if currentIdentifier is not None:
180-
try:
181-
lastTransmisionSuccess = qeueIdSuccess[str(currentIdentifier)][0] # contains the success of the last transmission
182-
timeLastTrasmissionWasAsked = qeueIdSuccess[str(currentIdentifier)][1] # contains the time when the last transmission was asked in case we need to timeout
183-
if time.time() - timeLastTrasmissionWasAsked > 0.1: #timeout to wait until ESP32 responds
184-
lastTransmisionSuccess = True # something went wrong, we have to free serial now!
185-
except Exception as e:
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
187-
lastTransmisionSuccess = True
188-
if not self.command_queue.empty() and not reading_json and lastTransmisionSuccess:
189-
currentIdentifier, command = self.command_queue.get()
190-
if self.DEBUG: self._logger.debug("Sending: "+ str(command))
191-
qeueIdSuccess[str(currentIdentifier)]=(False, time.time())
174+
#if not self.command_queue.empty() and not reading_json:
175+
# currentIdentifier, command = self.command_queue.get()
192176

193177
# device not ready yet
194178
if self.ser is None:
@@ -217,8 +201,6 @@ def _process_commands(self):
217201
reading_json = False
218202
try:
219203
json_response = json.loads(buffer)
220-
# add success to the dictionary
221-
qeueIdSuccess[str(json_response["qid"])]=(1, time.time())
222204
if len(self.callBackList) > 0:
223205
for callback in self.callBackList:
224206
# check if json has key
@@ -307,7 +289,12 @@ def sendMessage(self, command, nResponses=1, timeout = 20):
307289
self.command_queue.put((identifier, command))
308290
try:
309291
json_command = json.dumps(command)+"\n"
310-
self.ser.write(json_command.encode('utf-8') )
292+
with self.lock:
293+
t0 = time.time()
294+
self.ser.flush()
295+
self._logger.debug("[SendMessage]: "+str(json_command))
296+
self.ser.write(json_command.encode('utf-8') )
297+
self._logger.debug("[SendMessage] took: "+str(time.time()-t0))
311298
except Exception as e:
312299
if self.DEBUG: self._logger.error(e)
313300
return "Failed to Send"

0 commit comments

Comments
 (0)