shellmatta_transport_serial.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 shelmatta 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_port = serial.Serial(com_port, baudrate=baudrate, timeout=timeout)
  12. self.transport = ShellmattaTransport(self.com_port,
  13. transport_layer_mandatory,
  14. None)
  15. def send_command(self, command):
  16. """ Send command and wait for the prompt in the response. """
  17. if isinstance(command, str):
  18. send_data = str.encode(command)
  19. else:
  20. send_data = command
  21. retries = 3
  22. while retries:
  23. send_data = send_data + b"\r"
  24. self.transport.write(send_data)
  25. data = b''
  26. while True:
  27. data += self.transport.read()
  28. if data.endswith(str.encode(self.prompt)):
  29. # return received string without echo and prompt
  30. if data.startswith(send_data):
  31. data = data[len(send_data):-len(str.encode(self.prompt))]
  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. time.sleep(0.1)
  50. self.com_port.reset_input_buffer()
  51. self.com_port.reset_output_buffer()
  52. # start interactive mode if not used as a module
  53. if __name__ == "__main__":
  54. # setup an argument parser
  55. parser = argparse.ArgumentParser()
  56. parser.add_argument("-c", "--com-port", dest="com_port", help="Com port to use.", required=True)
  57. parser.add_argument("-b", "--baudrate", dest="baudrate", help="Baudrate to use.", type=int, default=115200)
  58. parser.add_argument("-p", "--prompt", dest="prompt", help="Prompt text of the shellmatta instance.", default="->")
  59. parser.add_argument("-t", "--timeout", dest="timeout", help="Timeout on the com port.", type=float, default=0.1)
  60. parser.add_argument("-m", "--mandatory", dest="mandatory", help="Force transport layer usage.", default=True)
  61. args = parser.parse_args()
  62. # get our own shellmatta serial instance
  63. shellmatta = ShellmattaSerial(args.com_port,
  64. args.baudrate,
  65. args.prompt,
  66. args.timeout,
  67. args.mandatory)
  68. shellmatta.reset_communication()
  69. # process user input
  70. print("Shellmatta Transport Serial wrapper - type commands as you like:")
  71. while True:
  72. command = input()
  73. print(shellmatta.send_command(command))