serial_reader.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright 2015-2021 Espressif Systems (Shanghai) CO LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import sys
  15. import time
  16. import serial
  17. from .constants import TAG_SERIAL
  18. from .output_helpers import red_print, yellow_print
  19. from .stoppable_thread import StoppableThread
  20. try:
  21. import queue
  22. except ImportError:
  23. import Queue as queue # type: ignore # noqa
  24. class SerialReader(StoppableThread):
  25. """ Read serial data from the serial port and push to the
  26. event queue, until stopped.
  27. """
  28. def __init__(self, serial_instance, event_queue):
  29. # type: (serial.Serial, queue.Queue) -> None
  30. super(SerialReader, self).__init__()
  31. self.baud = serial_instance.baudrate
  32. self.serial = serial_instance
  33. self.event_queue = event_queue
  34. if not hasattr(self.serial, 'cancel_read'):
  35. # enable timeout for checking alive flag,
  36. # if cancel_read not available
  37. self.serial.timeout = 0.25
  38. def run(self):
  39. # type: () -> None
  40. if not self.serial.is_open:
  41. self.serial.baudrate = self.baud
  42. self.serial.rts = True # Force an RTS reset on open
  43. self.serial.open()
  44. self.serial.rts = False
  45. self.serial.dtr = self.serial.dtr # usbser.sys workaround
  46. try:
  47. while self.alive:
  48. try:
  49. data = self.serial.read(self.serial.in_waiting or 1)
  50. except (serial.serialutil.SerialException, IOError) as e:
  51. data = b''
  52. # self.serial.open() was successful before, therefore, this is an issue related to
  53. # the disapperence of the device
  54. red_print(e)
  55. yellow_print('Waiting for the device to reconnect', newline='')
  56. self.serial.close()
  57. while self.alive: # so that exiting monitor works while waiting
  58. try:
  59. time.sleep(0.5)
  60. self.serial.open()
  61. break # device connected
  62. except serial.serialutil.SerialException:
  63. yellow_print('.', newline='')
  64. sys.stderr.flush()
  65. yellow_print('') # go to new line
  66. if data:
  67. self.event_queue.put((TAG_SERIAL, data), False)
  68. finally:
  69. self.serial.close()
  70. def _cancel(self):
  71. # type: () -> None
  72. if hasattr(self.serial, 'cancel_read'):
  73. try:
  74. self.serial.cancel_read()
  75. except Exception: # noqa
  76. pass