import sys 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_types.nbus_parameter_type import NBusParameterID # example config config = { "port_name": "COM4", "baud": NBusBaudrate.SPEED_921600, "parity": NBusParity.NONE, "timeout": 99.0, "flush_delay": 0.05, "request_attempts": 1, "enable_log": False } if __name__ == "__main__": # create port port = NBusSerialPort(NBusSerialConfig(**config)) # create module module = NBusSlaveModule(port, 5) # run commands try: # initialize module from hardware module.init(False) # initialize and load format # get devices devices = module.get_devices() accelerometer = devices[1] led = devices[129] # test module get print("<>") 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<>") 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<>") 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<>") 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()) except Exception as e: print(e) finally: port.flush() port.close()