example_slave_module.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. BRIDGE_ACQUIRE_WAIT = 0.5
  17. if __name__ == "__main__":
  18. # create port
  19. port = NBusSerialPort(NBusSerialConfig(**config))
  20. # create module
  21. module = NBusSlaveModule(port, 5, BRIDGE_ACQUIRE_WAIT)
  22. # run commands
  23. try:
  24. # initialize module from hardware
  25. module.init() # initialize and load format
  26. # get devices
  27. devices = module.get_devices()
  28. accelerometer = devices[1]
  29. led = devices[129]
  30. # test module get
  31. print("<<TEST MODULE GET>>")
  32. print("CMD GET ECHO: ", module.cmd_get_echo(bytearray("Hello world!", "ascii")))
  33. print("CMD GET PARAM SAMPLERATE: ", module.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  34. print("CMD GET ALL PARAMS: ", module.cmd_get_all_params())
  35. print("CMD GET SENSOR COUNT: ", module.cmd_get_sensor_cnt())
  36. print("CMD GET SENSOR TYPE: ", module.cmd_get_sensor_type())
  37. print("CMD GET INFO: ", module.cmd_get_info())
  38. print("CMD GET SENSOR FORMAT: ", module.cmd_get_format())
  39. print("CMD GET SENSOR DATA: ", module.cmd_get_data())
  40. # test module set
  41. print("CMD SET PARAM SAMPLERATE: ", module.cmd_set_param(NBusParameterID.PARAM_SAMPLERATE, 12345))
  42. print("CMD GET PARAM SAMPLERATE: ", module.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  43. params = {NBusParameterID.PARAM_RANGE: 12, NBusParameterID.PARAM_RANGE0: 4234}
  44. print("CMD SET MULTI PARAMS: ", module.cmd_set_multi_params(params))
  45. print("CMD GET ALL PARAMS: ", module.cmd_get_all_params())
  46. print("CMD SET CALIBRATE: ", module.cmd_set_calibrate())
  47. data = {129: [1], 130: [10, -32]}
  48. print("CMD SET DATA: ", module.cmd_set_data(data))
  49. print("CMD GET SENSOR DATA: ", module.cmd_get_data())
  50. # test sensor get
  51. print("\n<<TEST SENSOR GET>>")
  52. print("CMD GET PARAM SAMPLERATE: ", accelerometer.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  53. print("CMD GET ALL PARAMS: ", accelerometer.cmd_get_all_params())
  54. print("CMD GET SENSOR TYPE: ", accelerometer.cmd_get_sensor_type())
  55. print("CMD GET SENSOR FORMAT: ", accelerometer.cmd_get_format())
  56. print("CMD GET SENSOR DATA: ", accelerometer.cmd_get_data())
  57. # test sensor set
  58. print("\n<<TEST SENSOR SET>>")
  59. print("CMD SET FIND: ", led.cmd_set_find(True))
  60. print("CMD SET PARAM SAMPLERATE: ", led.cmd_set_param(NBusParameterID.PARAM_SAMPLERATE, 23456))
  61. print("CMD GET PARAM SAMPLERATE: ", led.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
  62. params = {NBusParameterID.PARAM_RANGE: 12, NBusParameterID.PARAM_RANGE0: 4234}
  63. print("CMD SET MULTI PARAMS: ", led.cmd_set_multi_params(params))
  64. print("CMD GET ALL PARAMS: ", led.cmd_get_all_params())
  65. print("CMD SET CALIBRATE: ", led.cmd_set_calibrate())
  66. print("CMD SET DATA: ", led.cmd_set_data([1]))
  67. print("CMD GET SENSOR DATA: ", led.cmd_get_data())
  68. except Exception as e:
  69. print(e)
  70. finally:
  71. port.flush()
  72. port.close()