app.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from comm import *
  2. import sys
  3. import time
  4. class AppTest:
  5. def __init__(self, adr_module, adr_sensor):
  6. self.serial_port = SerialComm('/dev/ttyUSB0')
  7. self.module = adr_module
  8. self.sensor = adr_sensor
  9. def finish(self):
  10. self.serial_port.close()
  11. def cmd_version(self):
  12. resp = self.serial_port.request(self.module, 0, CMD_VERSION,[])
  13. version = chr(resp[3])+chr(resp[4])+chr(resp[5])
  14. print("Version: "+version)
  15. def cmd_echo(self, msg):
  16. resp = self.serial_port.request(self.module, 0, CMD_ECHO,msg)
  17. echo = ""
  18. for r in range(len(msg)):
  19. echo = echo + chr(resp[3+r])
  20. print("Echo:"+echo)
  21. def cmd_set_param(self, param):
  22. resp = self.serial_port.request(self.module, self.srensor, (SET+CMD_PARAM),[])
  23. def cmd_get_param(self, param):
  24. resp = self.serial_port.request(self.module, self.sensor, (CMD_PARAM),[])
  25. def cmd_sensor_cnt(self):
  26. print("SENSOR CNT")
  27. resp = self.serial_port.request(self.module, 0, CMD_SENSOR_CNT,[])
  28. print(resp)
  29. return resp[3]
  30. def cmd_sensor_type(self, index):
  31. resp = self.serial_port.request(self.module, index, CMD_SENSOR_TYPE,[])
  32. return resp[3]
  33. def cmd_sensors_type(self, pocet):
  34. resp = self.serial_port.request(self.module, 0, CMD_SENSOR_TYPE,[])
  35. typy = [];
  36. for i in range(pocet):
  37. typy.append([resp[3+2*i],resp[4+2*i]])
  38. return typy
  39. def cmd_sensor_get_params(self, index):
  40. resp = self.serial_port.request(self.module, index, CMD_PARAM,[])
  41. return resp
  42. def cmd_sensor_get_data(self, index):
  43. resp = self.serial_port.request(self.module, index, CMD_DATA,[])
  44. if len(resp)>1:
  45. h = resp[5] * 256 + resp[4]
  46. # print(hex(h))
  47. return h/4096*3.3
  48. return 0
  49. def cmd_sensor_get_data_all(self, cnt):
  50. resp = self.serial_port.request(self.module, 0, CMD_DATA,[])
  51. # print(resp)
  52. if len(resp)>1:
  53. for i in range(cnt):
  54. sen = resp[3+3*i]
  55. h = resp[5+3*i] * 256 + resp[4+3*i]
  56. print("FSR",sen,":\t", h/4096.0*3.3)
  57. def cmd_module_info(self, param):
  58. resp = self.serial_port.request(self.module, 0, CMD_INFO,[param])
  59. if param == 0xE3:
  60. data=0
  61. for i in range(4):
  62. data=data+resp[i+3]*pow(256,i)
  63. return hex(data)
  64. if param == 0xE6:
  65. data=0
  66. for i in range(8):
  67. data=data+resp[i+3]*pow(256,7-i)
  68. return hex(data)
  69. l = len(resp)
  70. data=""
  71. for i in range(l-4):
  72. data=data+chr(resp[i+3])
  73. return data
  74. def cmd_module_start(self):
  75. print("MODULE START")
  76. self.serial_port.requestBroadcast(CMD_START,[])
  77. def cmd_module_stop(self):
  78. print("MODULE STOP")
  79. self.serial_port.requestBroadcast(CMD_STOP,[])
  80. if __name__ == "__main__":
  81. app = AppTest(0x05, 0x0)
  82. #app.cmd_version()
  83. #app.cmd_version()
  84. # app.cmd_module_stop()
  85. app.cmd_echo([65,66,67,68,69,70])
  86. app.cmd_echo([97,98,99,100])
  87. sys.exit()
  88. app.cmd_module_stop()
  89. app.cmd_module_start()
  90. #time.sleep(0.5)
  91. #app.cmd_module_stop()
  92. sys.exit()
  93. pocet = app.cmd_sensor_cnt()
  94. print("pocet senzorov=", pocet)
  95. app.cmd_sensor_get_data_all(pocet)
  96. sys.exit()
  97. for i in range(pocet):
  98. d=app.cmd_sensor_get_data(i+1)
  99. print("Data", i+1, ":\t", d)
  100. time.sleep(1)
  101. app.cmd_module_stop()
  102. print("Sensor type")
  103. for i in range(pocet):
  104. print(app.cmd_sensor_type(i+1))
  105. print("Sensors type")
  106. print(app.cmd_sensors_type(pocet))
  107. par = app.cmd_sensor_get_params(1)
  108. print(par)
  109. r=app.cmd_module_info(0xE1)
  110. print('NAME:' , r)
  111. r=app.cmd_module_info(0xE2)
  112. print('TYPE', r)
  113. r=app.cmd_module_info(0xE4)
  114. print('FW', r)
  115. r=app.cmd_module_info(0xE5)
  116. print('HW', r)
  117. r=app.cmd_module_info(0xE3)
  118. print("UUID", r)
  119. r=app.cmd_module_info(0xE6)
  120. print("MEM ID", r)
  121. app.finish()