shellmatta_transport_serial.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. data = data[:-len(str.encode(self.prompt))]
  31. if data.startswith(send_data):
  32. data = data[len(send_data):]
  33. break
  34. # todo implement proper retries / sequence checking
  35. if b"crc error\r\n" not in data:
  36. break
  37. retries -= 1
  38. return data
  39. def send_command_only(self, command):
  40. """ Send command without waiting for response. """
  41. send_string = command + "\r"
  42. self.transport.write(str.encode(send_string))
  43. def send_raw(self, data):
  44. """ Send a passed bytes string without any mercy. """
  45. self.transport.write_manual(data)
  46. def reset_communication(self):
  47. """ Clears all internal buffers, throwing away all data. """
  48. self.transport.reset()
  49. self.com_port.flush()
  50. time.sleep(0.1)
  51. self.com_port.reset_input_buffer()
  52. self.com_port.reset_output_buffer()
  53. # start interactive mode if not used as a module
  54. if __name__ == "__main__":
  55. # setup an argument parser
  56. parser = argparse.ArgumentParser()
  57. parser.add_argument("-c", "--com-port", dest="com_port", help="Com port to use.", required=True)
  58. parser.add_argument("-b", "--baudrate", dest="baudrate", help="Baudrate to use.", type=int, default=115200)
  59. parser.add_argument("-p", "--prompt", dest="prompt", help="Prompt text of the shellmatta instance.", default="->")
  60. parser.add_argument("-t", "--timeout", dest="timeout", help="Timeout on the com port.", type=float, default=0.1)
  61. parser.add_argument("-m", "--mandatory", dest="mandatory", help="Force transport layer usage.", default=True)
  62. args = parser.parse_args()
  63. # get our own shellmatta serial instance
  64. shellmatta = ShellmattaSerial(args.com_port,
  65. args.baudrate,
  66. args.prompt,
  67. args.timeout,
  68. args.mandatory)
  69. shellmatta.reset_communication()
  70. # process user input
  71. print("Shellmatta Transport Serial wrapper - type commands as you like:")
  72. while True:
  73. command = input()
  74. print(shellmatta.send_command(command))