serial_reader.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import queue
  4. import subprocess
  5. import sys
  6. import time
  7. import serial
  8. from .constants import CHECK_ALIVE_FLAG_TIMEOUT, MINIMAL_EN_LOW_DELAY, RECONNECT_DELAY, TAG_SERIAL
  9. from .output_helpers import red_print, yellow_print
  10. from .stoppable_thread import StoppableThread
  11. class Reader(StoppableThread):
  12. """ Output Reader base class """
  13. class SerialReader(Reader):
  14. """ Read serial data from the serial port and push to the
  15. event queue, until stopped.
  16. """
  17. def __init__(self, serial_instance, event_queue):
  18. # type: (serial.Serial, queue.Queue) -> None
  19. super(SerialReader, self).__init__()
  20. self.baud = serial_instance.baudrate
  21. self.serial = serial_instance
  22. self.event_queue = event_queue
  23. self.gdb_exit = False
  24. if not hasattr(self.serial, 'cancel_read'):
  25. # enable timeout for checking alive flag,
  26. # if cancel_read not available
  27. self.serial.timeout = CHECK_ALIVE_FLAG_TIMEOUT
  28. def run(self):
  29. # type: () -> None
  30. if not self.serial.is_open:
  31. self.serial.baudrate = self.baud
  32. # We can come to this thread at startup or from external application line GDB.
  33. # If we come from GDB we would like to continue to run without reset.
  34. high = False
  35. low = True
  36. self.serial.dtr = low # Non reset state
  37. self.serial.rts = high # IO0=HIGH
  38. self.serial.dtr = self.serial.dtr # usbser.sys workaround
  39. # Current state not reset the target!
  40. self.serial.open()
  41. if not self.gdb_exit:
  42. self.serial.dtr = high # Set dtr to reset state (affected by rts)
  43. self.serial.rts = low # Set rts/dtr to the reset state
  44. self.serial.dtr = self.serial.dtr # usbser.sys workaround
  45. # Add a delay to meet the requirements of minimal EN low time (2ms for ESP32-C3)
  46. time.sleep(MINIMAL_EN_LOW_DELAY)
  47. self.gdb_exit = False
  48. self.serial.rts = high # Set rts/dtr to the working state
  49. self.serial.dtr = self.serial.dtr # usbser.sys workaround
  50. try:
  51. while self.alive:
  52. try:
  53. data = self.serial.read(self.serial.in_waiting or 1)
  54. except (serial.serialutil.SerialException, IOError) as e:
  55. data = b''
  56. # self.serial.open() was successful before, therefore, this is an issue related to
  57. # the disappearance of the device
  58. red_print(e)
  59. yellow_print('Waiting for the device to reconnect', newline='')
  60. self.serial.close()
  61. while self.alive: # so that exiting monitor works while waiting
  62. try:
  63. time.sleep(RECONNECT_DELAY)
  64. self.serial.open()
  65. break # device connected
  66. except serial.serialutil.SerialException:
  67. yellow_print('.', newline='')
  68. sys.stderr.flush()
  69. yellow_print('') # go to new line
  70. if data:
  71. self.event_queue.put((TAG_SERIAL, data), False)
  72. finally:
  73. self.serial.close()
  74. def _cancel(self):
  75. # type: () -> None
  76. if hasattr(self.serial, 'cancel_read'):
  77. try:
  78. self.serial.cancel_read()
  79. except Exception: # noqa
  80. pass
  81. class LinuxReader(Reader):
  82. """ Read data from the subprocess that runs runnable and push to the
  83. event queue, until stopped.
  84. """
  85. def __init__(self, process, event_queue):
  86. # type: (subprocess.Popen, queue.Queue) -> None
  87. super().__init__()
  88. self.proc = process
  89. self.event_queue = event_queue
  90. self._stdout = iter(self.proc.stdout.readline, b'') # type: ignore
  91. def run(self): # type: () -> None
  92. try:
  93. while self.alive:
  94. for line in self._stdout:
  95. if line:
  96. self.event_queue.put((TAG_SERIAL, line), False)
  97. finally:
  98. self.proc.terminate()
  99. def _cancel(self): # type: () -> None
  100. self.proc.terminate()