shellmatta_transport_serial.py 4.1 KB

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