| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- from nbus_hal.nbus_serial.serial_port import *
- from nbus_api.nbus_module_slave import NBusSlaveModule
- from nbus_hal.nbus_serial.serial_config import *
- from nbus_sensor_drivers.generic_sensor_driver import NBusGenericSensor
- from nbus_types.nbus_parameter_type import NBusParameterID
- # example config
- config = {
- "port_name": "COM6",
- "baud": NBusBaudrate.SPEED_921600,
- "parity": NBusParity.NONE,
- "timeout": 0.4,
- "request_attempts": 1,
- "enable_log": False
- }
- if __name__ == "__main__":
- # create port
- port = NBusSerialPort(NBusSerialConfig(**config))
- # create module
- module = NBusSlaveModule(port, 5)
- # assemble module
- module.add_sensor(NBusGenericSensor(1))
- module.add_sensor(NBusGenericSensor(2))
- module.add_sensor(NBusGenericSensor(3))
- module.add_sensor(NBusGenericSensor(4))
- module.add_sensor(NBusGenericSensor(5))
- module.add_sensor(NBusGenericSensor(129))
- module.add_sensor(NBusGenericSensor(130))
- # get sensors
- accelerometer = module.get_sensor(1)
- led = module.get_sensor(129)
- # test module get
- print("<<TEST MODULE GET>>")
- print("CMD GET ECHO: ", module.cmd_get_echo(bytearray("Hello world!", "ascii")))
- print("CMD GET PARAM SAMPLERATE: ", module.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
- print("CMD GET ALL PARAMS: ", module.cmd_get_all_params())
- print("CMD GET SENSOR COUNT: ", module.cmd_get_sensor_cnt())
- print("CMD GET SENSOR TYPE: ", module.cmd_get_sensor_type())
- print("CMD GET INFO: ", module.cmd_get_info())
- print("CMD GET SENSOR FORMAT: ", module.cmd_get_format())
- print("CMD GET SENSOR DATA: ", module.cmd_get_data())
- # test module set
- print("\n<<TEST MODULE SET>>")
- print("CMD SET STOP: ", module.cmd_set_module_stop())
- print("CMD SET START: ", module.cmd_set_module_start())
- print("CMD SET PARAM SAMPLERATE: ", module.cmd_set_param(NBusParameterID.PARAM_SAMPLERATE, 12345))
- print("CMD GET PARAM SAMPLERATE: ", module.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
- params = {NBusParameterID.PARAM_RANGE: 12, NBusParameterID.PARAM_RANGE0: 4234}
- print("CMD SET MULTI PARAMS: ", module.cmd_set_multi_params(params))
- print("CMD GET ALL PARAMS: ", module.cmd_get_all_params())
- print("CMD SET CALIBRATE: ", module.cmd_set_calibrate())
- data = {129: [1], 130: [10, -32]}
- print("CMD SET DATA: ", module.cmd_set_data(data))
- print("CMD GET SENSOR DATA: ", module.cmd_get_data())
- # test sensor get
- print("\n<<TEST SENSOR GET>>")
- print("CMD GET PARAM SAMPLERATE: ", accelerometer.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
- print("CMD GET ALL PARAMS: ", accelerometer.cmd_get_all_params())
- print("CMD GET SENSOR TYPE: ", accelerometer.cmd_get_sensor_type())
- print("CMD GET SENSOR FORMAT: ", accelerometer.cmd_get_format())
- print("CMD GET SENSOR DATA: ", accelerometer.cmd_get_data())
- # test sensor set
- print("\n<<TEST SENSOR SET>>")
- print("CMD SET FIND: ", led.cmd_set_find(True))
- print("CMD SET PARAM SAMPLERATE: ", led.cmd_set_param(NBusParameterID.PARAM_SAMPLERATE, 23456))
- print("CMD GET PARAM SAMPLERATE: ", led.cmd_get_param(NBusParameterID.PARAM_SAMPLERATE))
- params = {NBusParameterID.PARAM_RANGE: 12, NBusParameterID.PARAM_RANGE0: 4234}
- print("CMD SET MULTI PARAMS: ", led.cmd_set_multi_params(params))
- print("CMD GET ALL PARAMS: ", led.cmd_get_all_params())
- print("CMD SET CALIBRATE: ", led.cmd_set_calibrate())
- print("CMD SET DATA: ", led.cmd_set_data([1]))
- print("CMD GET SENSOR DATA: ", led.cmd_get_data())
|