test.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from nbus_hal.nbus_serial.serial_port import *
  2. from nbus_api.nbus_module_slave import NBusSlaveModule
  3. from nbus_hal.nbus_serial.serial_config import *
  4. from nbus_sensor_drivers.generic_sensor_driver import NBusGenericSensor
  5. from nbus_types.nbus_parameter_type import NBusParameterID
  6. # example config
  7. config = {
  8. "port_name": "COM6",
  9. "baud": NBusBaudrate.SPEED_921600,
  10. "parity": NBusParity.NONE,
  11. "timeout": 0.4,
  12. "request_attempts": 1,
  13. "enable_log": False
  14. }
  15. if __name__ == "__main__":
  16. # create port
  17. port = NBusSerialPort(NBusSerialConfig(**config))
  18. # create module
  19. module = NBusSlaveModule(port, 5)
  20. # assemble module
  21. module.add_sensor(NBusGenericSensor(1))
  22. module.add_sensor(NBusGenericSensor(2))
  23. module.add_sensor(NBusGenericSensor(3))
  24. module.add_sensor(NBusGenericSensor(4))
  25. module.add_sensor(NBusGenericSensor(5))
  26. module.add_sensor(NBusGenericSensor(129))
  27. module.add_sensor(NBusGenericSensor(130))
  28. # get sensors
  29. accelerometer = module.get_sensor(1)
  30. led = module.get_sensor(129)
  31. # test module get
  32. print("<<TEST MODULE GET>>")
  33. print("CMD GET ECHO: ", module.cmd_get_echo(bytearray("Hello world!", "ascii")))
  34. print("CMD GET PARAM SAMPLERATE: ", module.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  35. print("CMD GET ALL PARAMS: ", module.cmd_get_all_params())
  36. print("CMD GET SENSOR COUNT: ", module.cmd_get_sensor_cnt())
  37. print("CMD GET SENSOR TYPE: ", module.cmd_get_sensor_type())
  38. print("CMD GET INFO: ", module.cmd_get_info())
  39. print("CMD GET SENSOR FORMAT: ", module.cmd_get_format())
  40. print("CMD GET SENSOR DATA: ", module.cmd_get_data())
  41. # test module set
  42. print("\n<<TEST MODULE SET>>")
  43. print("CMD SET STOP: ", module.cmd_set_module_stop())
  44. print("CMD SET START: ", module.cmd_set_module_start())
  45. print("CMD SET PARAM SAMPLERATE: ", module.cmd_set_param(NBusParameterID.PARAM_SAMPLERATE, 12345))
  46. print("CMD GET PARAM SAMPLERATE: ", module.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  47. params = {NBusParameterID.PARAM_RANGE: 12, NBusParameterID.PARAM_RANGE0: 4234}
  48. print("CMD SET MULTI PARAMS: ", module.cmd_set_multi_params(params))
  49. print("CMD GET ALL PARAMS: ", module.cmd_get_all_params())
  50. print("CMD SET CALIBRATE: ", module.cmd_set_calibrate())
  51. data = {129: [1], 130: [10, -32]}
  52. print("CMD SET DATA: ", module.cmd_set_data(data))
  53. print("CMD GET SENSOR DATA: ", module.cmd_get_data())
  54. # test sensor get
  55. print("\n<<TEST SENSOR GET>>")
  56. print("CMD GET PARAM SAMPLERATE: ", accelerometer.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  57. print("CMD GET ALL PARAMS: ", accelerometer.cmd_get_all_params())
  58. print("CMD GET SENSOR TYPE: ", accelerometer.cmd_get_sensor_type())
  59. print("CMD GET SENSOR FORMAT: ", accelerometer.cmd_get_format())
  60. print("CMD GET SENSOR DATA: ", accelerometer.cmd_get_data())
  61. # test sensor set
  62. print("\n<<TEST SENSOR SET>>")
  63. print("CMD SET FIND: ", led.cmd_set_find(True))
  64. print("CMD SET PARAM SAMPLERATE: ", led.cmd_set_param(NBusParameterID.PARAM_SAMPLERATE, 23456))
  65. print("CMD GET PARAM SAMPLERATE: ", led.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  66. params = {NBusParameterID.PARAM_RANGE: 12, NBusParameterID.PARAM_RANGE0: 4234}
  67. print("CMD SET MULTI PARAMS: ", led.cmd_set_multi_params(params))
  68. print("CMD GET ALL PARAMS: ", led.cmd_get_all_params())
  69. print("CMD SET CALIBRATE: ", led.cmd_set_calibrate())
  70. print("CMD SET DATA: ", led.cmd_set_data([1]))
  71. print("CMD GET SENSOR DATA: ", led.cmd_get_data())