example_slave_module.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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": 1.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() # 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("CMD SET PARAM SAMPLERATE: ", module.cmd_set_param(NBusParameterID.PARAM_SAMPLERATE, 12345))
  41. print("CMD GET PARAM SAMPLERATE: ", module.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  42. params = {NBusParameterID.PARAM_RANGE: 12, NBusParameterID.PARAM_RANGE0: 4234}
  43. print("CMD SET MULTI PARAMS: ", module.cmd_set_multi_params(params))
  44. print("CMD GET ALL PARAMS: ", module.cmd_get_all_params())
  45. print("CMD SET CALIBRATE: ", module.cmd_set_calibrate())
  46. data = {129: [1], 130: [10, -32]}
  47. print("CMD SET DATA: ", module.cmd_set_data(data))
  48. print("CMD GET SENSOR DATA: ", module.cmd_get_data())
  49. # test sensor get
  50. print("\n<<TEST SENSOR GET>>")
  51. print("CMD GET PARAM SAMPLERATE: ", accelerometer.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  52. print("CMD GET ALL PARAMS: ", accelerometer.cmd_get_all_params())
  53. print("CMD GET SENSOR TYPE: ", accelerometer.cmd_get_sensor_type())
  54. print("CMD GET SENSOR FORMAT: ", accelerometer.cmd_get_format())
  55. print("CMD GET SENSOR DATA: ", accelerometer.cmd_get_data())
  56. # test sensor set
  57. print("\n<<TEST SENSOR SET>>")
  58. print("CMD SET FIND: ", led.cmd_set_find(True))
  59. print("CMD SET PARAM SAMPLERATE: ", led.cmd_set_param(NBusParameterID.PARAM_SAMPLERATE, 23456))
  60. print("CMD GET PARAM SAMPLERATE: ", led.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  61. params = {NBusParameterID.PARAM_RANGE: 12, NBusParameterID.PARAM_RANGE0: 4234}
  62. print("CMD SET MULTI PARAMS: ", led.cmd_set_multi_params(params))
  63. print("CMD GET ALL PARAMS: ", led.cmd_get_all_params())
  64. print("CMD SET CALIBRATE: ", led.cmd_set_calibrate())
  65. print("CMD SET DATA: ", led.cmd_set_data([1]))
  66. print("CMD GET SENSOR DATA: ", led.cmd_get_data())
  67. except Exception as e:
  68. print(e)
  69. finally:
  70. port.flush()
  71. port.close()