| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- from nbus_hal.nbus_serial.serial_port import *
- from nbus_api.nbus_slave_module import NBusSlaveModule
- from nbus_api.nbus_slave_device import NBusSlaveDevice
- from nbus_hal.nbus_serial.serial_config import *
- from nbus_types.nbus_data_fomat import NBusDataValue
- from nbus_types.nbus_parameter_type import NBusParameterID, NBusParameterValue
- # example config
- config = {
- "port_name": "COM6",
- "baud": NBusBaudrate.SPEED_921600,
- "parity": NBusParity.NONE,
- "timeout": 0.4,
- "request_attempts": 1,
- "enable_log": False
- }
- class DummySlave(NBusSlaveDevice):
- def data_parameters_loaded(self) -> bool:
- return True
- def map_parameter_get(self, param_id: NBusParameterID, param_value: int) -> NBusParameterValue:
- return param_value
- def map_parameter_set(self, param_id: NBusParameterID, param_value: NBusParameterValue) -> int:
- return param_value
- def map_data_get(self, values: list[int]) -> list[NBusDataValue]:
- return values
- def map_data_set(self, values: list[NBusDataValue]) -> list[int]:
- return values
- if __name__ == "__main__":
- try:
- port = NBusSerialPort(NBusSerialConfig(**config))
- module = NBusSlaveModule(port, 5)
- module.add_device(DummySlave(1))
- module.add_device(DummySlave(2))
- module.add_device(DummySlave(3))
- module.add_device(DummySlave(4))
- module.add_device(DummySlave(5))
- module.add_device(DummySlave(129))
- module.add_device(DummySlave(130))
- # 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())
- # print("ALL PARAMS: ", sensing_element.cmd_get_all_params())
- #print("PARAM GAIN: ", sensing_element.cmd_get_param(NBusParameterID.PARAM_GAIN))
- # print("SENSOR TYPE: ", sensing_element.cmd_get_sensor_type())
- # print("SENSOR FORMAT: ", sensing_element.cmd_get_info(NBusInfoParam.INFO_FORMAT))
- #print("DATA: ", sensing_element.cmd_get_data())
- #print(module.cmd_get_format())
- # print(sensing_element.cmd_get_data())
- #print(sensing_element.cmd_set_data([129, 1]))
- #print(sensing_element.cmd_get_data())
- except Exception as Ex:
- print("Error")
- print(str(Ex))
- print(Ex.args)
|