121 """ |
121 """ |
122 if not self.__serial: |
122 if not self.__serial: |
123 return False |
123 return False |
124 |
124 |
125 pasteMessage = b"paste mode; Ctrl-C to cancel, Ctrl-D to finish\r\n=== " |
125 pasteMessage = b"paste mode; Ctrl-C to cancel, Ctrl-D to finish\r\n=== " |
|
126 |
|
127 self.__serial.clear() # clear any buffered output before entering paste mode |
126 self.__serial.write(b"\x02") # end raw mode if required |
128 self.__serial.write(b"\x02") # end raw mode if required |
127 written = self.__serial.waitForBytesWritten(500) |
129 written = self.__serial.waitForBytesWritten(500) |
128 # time out after 500ms if device is not responding |
130 # time out after 500ms if device is not responding |
129 if not written: |
131 if not written: |
130 return False |
132 return False |
369 return b"", b"Device not connected or not switched on." |
371 return b"", b"Device not connected or not switched on." |
370 |
372 |
371 if isinstance(commands, list): |
373 if isinstance(commands, list): |
372 commands = "\n".join(commands) |
374 commands = "\n".join(commands) |
373 |
375 |
374 # switch on raw mode |
376 # switch on paste mode |
375 self.__blockReadyRead = True |
377 self.__blockReadyRead = True |
376 ok = self.__pasteOn() |
378 ok = self.__pasteOn() |
377 if not ok: |
379 if not ok: |
378 self.__blockReadyRead = False |
380 self.__blockReadyRead = False |
379 return (b"", b"Could not switch to raw mode. Is the device switched on?") |
381 return (b"", b"Could not switch to paste mode. Is the device switched on?") |
380 |
382 |
381 # send commands |
383 # send commands |
382 QThread.msleep(10) |
384 QThread.msleep(10) |
383 for command in commands.splitlines(keepends=True): |
385 for command in commands.splitlines(keepends=True): |
384 # send the data as single lines |
386 # send the data as single lines |
399 ) |
401 ) |
400 |
402 |
401 # switch off paste mode causing the commands to be executed |
403 # switch off paste mode causing the commands to be executed |
402 self.__pasteOff() |
404 self.__pasteOff() |
403 QThread.msleep(10) |
405 QThread.msleep(10) |
404 result = self.__serial.readUntil(b">>> ", timeout=timeout).replace(b">>> ", b"") |
|
405 # read until Python prompt |
406 # read until Python prompt |
|
407 result = self.__serial.readUntil(b">>> ", timeout=timeout).replace(b">>> ", b"").strip() |
406 if self.__serial.hasTimedOut(): |
408 if self.__serial.hasTimedOut(): |
407 self.__blockReadyRead = False |
409 self.__blockReadyRead = False |
408 return b"", b"Timeout while processing commands." |
410 return b"", b"Timeout while processing commands." |
|
411 |
|
412 # get rid of any OSD string |
|
413 if result.startswith(b"\x1b]0;"): |
|
414 result = result.split(b"\x1b\\")[-1] |
409 |
415 |
410 if self.TracebackMarker in result: |
416 if self.TracebackMarker in result: |
411 errorIndex = result.find(self.TracebackMarker) |
417 errorIndex = result.find(self.TracebackMarker) |
412 out, err = result[:errorIndex], result[errorIndex:] |
418 out, err = result[:errorIndex], result[errorIndex:] |
413 else: |
419 else: |
414 out = result.strip() |
420 out = result |
415 err = b"" |
421 err = b"" |
416 |
422 |
417 self.__blockReadyRead = False |
423 self.__blockReadyRead = False |
418 return out, err |
424 return out, err |
419 |
425 |