shellmatta_transport_serial.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """ Wrapper arount a shellmatta with transport layer to send commands and receive the response """
  2. import time
  3. import serial
  4. import argparse
  5. from shellmatta_transport import ShellmattaTransport
  6. class ShellmattaSerial():
  7. """ Helper class to communicate with a shellmatta enabled device """
  8. def __init__(self, com_port, baudrate=115200, prompt="->", timeout=1, transport_layer_mandatory=False):
  9. """ create the transport layer instance using the passed com port """
  10. self.prompt = prompt
  11. self.com = com_port
  12. self.com_port = serial.Serial(com_port, baudrate=baudrate, timeout=timeout)
  13. self.transport = ShellmattaTransport(self.com_port, transport_layer_mandatory)
  14. def send_command(self, command):
  15. """ Send command and wait for the prompt in the response. """
  16. if isinstance(command, str):
  17. send_data = str.encode(command)
  18. else:
  19. send_data = command
  20. retries = 3
  21. while retries:
  22. send_data = send_data + b"\r"
  23. self.transport.write(send_data)
  24. data = b''
  25. while True:
  26. data += self.transport.read()
  27. if data.endswith(str.encode(self.prompt)):
  28. # return received string without echo and prompt
  29. data = data[:-len(str.encode(self.prompt))]
  30. if data.startswith(send_data):
  31. data = data[len(send_data):]
  32. break
  33. # todo implement proper retries / sequence checking
  34. if b"crc error\r\n" not in data:
  35. break
  36. retries -= 1
  37. return data
  38. def send_command_only(self, command):
  39. """ Send command without waiting for response. """
  40. send_string = command + "\r"
  41. self.transport.write(str.encode(send_string))
  42. def send_raw(self, data):
  43. """ Send a passed bytes string without any mercy. """
  44. self.transport.write_manual(data)
  45. def reset_communication(self):
  46. """ Clears all internal buffers, throwing away all data. """
  47. self.transport.reset()
  48. self.com_port.flush()
  49. self.com_port.reset_input_buffer()
  50. self.com_port.reset_output_buffer()
  51. def close_serial(self):
  52. """ Closes the serial port """
  53. self.com_port.close()
  54. def set_baudrate(self, baud):
  55. """ Set the baudrate of the serial port """
  56. self.com_port.baudrate = baud
  57. self.com_port.close()
  58. time.sleep(0.5)
  59. self.com_port = serial.Serial(self.com, baudrate=baud, timeout=self.com_port.timeout)
  60. self.transport.close()
  61. self.transport = ShellmattaTransport(self.com_port,
  62. False,
  63. None)
  64. # start interactive mode if not used as a module
  65. if __name__ == "__main__":
  66. # setup an argument parser
  67. parser = argparse.ArgumentParser()
  68. parser.add_argument("-c", "--com-port", dest="com_port", help="Com port to use.", required=True)
  69. parser.add_argument("-b", "--baudrate", dest="baudrate", help="Baudrate to use.", type=int, default=115200)
  70. parser.add_argument("-p", "--prompt", dest="prompt", help="Prompt text of the shellmatta instance.", default="->")
  71. parser.add_argument("-t", "--timeout", dest="timeout", help="Timeout on the com port.", type=float, default=0.1)
  72. parser.add_argument("-m", "--mandatory", dest="mandatory", help="Force transport layer usage.", default=True)
  73. args = parser.parse_args()
  74. # get our own shellmatta serial instance
  75. shellmatta = ShellmattaSerial(args.com_port,
  76. args.baudrate,
  77. args.prompt,
  78. args.timeout,
  79. args.mandatory)
  80. shellmatta.reset_communication()
  81. # process user input
  82. print("Shellmatta Transport Serial wrapper - type commands as you like:")
  83. while True:
  84. command = input()
  85. print(shellmatta.send_command(command))