example_slave_module.py 3.6 KB

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