term.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from .utils import default_port
  4. try:
  5. from serial.tools.miniterm import Miniterm, console, NEWLINE_CONVERISON_MAP
  6. import serial
  7. MINITERM_AVAILABLE=True
  8. except ImportError:
  9. MINITERM_AVAILABLE=False
  10. class McuMiniterm(Miniterm):
  11. def __init__(self, serial):
  12. if not MINITERM_AVAILABLE:
  13. print "Miniterm is not available on this system"
  14. return
  15. self.serial = serial
  16. self.echo = False
  17. self.convert_outgoing = 2
  18. self.repr_mode = 1
  19. self.newline = NEWLINE_CONVERISON_MAP[self.convert_outgoing]
  20. self.dtr_state = True
  21. self.rts_state = True
  22. self.break_state = False
  23. def terminal(port=default_port()):
  24. if not MINITERM_AVAILABLE:
  25. print "Miniterm is not available on this system"
  26. return False
  27. sp = serial.Serial(port, 9600)
  28. # Keeps things working, if following conections are made:
  29. ## RTS = CH_PD (i.e reset)
  30. ## DTR = GPIO0
  31. sp.setRTS(False)
  32. sp.setDTR(False)
  33. miniterm = McuMiniterm(sp)
  34. log.info('Started terminal. Hit ctrl-] to leave terminal')
  35. console.setup()
  36. miniterm.start()
  37. try:
  38. miniterm.join(True)
  39. except KeyboardInterrupt:
  40. pass
  41. miniterm.join()
  42. sp.close()