example_slave_module_stream.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from nbus_api.nbus_module_slave import NBusSlaveModule
  2. from nbus_hal.nbus_serial.serial_port import *
  3. from nbus_hal.nbus_serial.serial_config import *
  4. # example config
  5. config = {
  6. "port_name": "COM14",
  7. "baud": NBusBaudrate.SPEED_921600,
  8. "parity": NBusParity.NONE,
  9. "timeout": 1.0,
  10. "flush_delay": 3.0,
  11. "request_attempts": 1,
  12. "enable_log": False
  13. }
  14. BRIDGE_ACQUIRE_WAIT = 0.5
  15. BRIDGE_STREAM_INTERVAL = 5.0
  16. if __name__ == "__main__":
  17. # create port
  18. port = NBusSerialPort(NBusSerialConfig(**config))
  19. # create module
  20. module = NBusSlaveModule(port, 5, BRIDGE_ACQUIRE_WAIT)
  21. # run commands
  22. try:
  23. # initialize module
  24. module.init()
  25. # perform stream commands
  26. print(f"START STREAM FOR {BRIDGE_STREAM_INTERVAL} SECONDS")
  27. module.start_streaming()
  28. time.sleep(BRIDGE_STREAM_INTERVAL)
  29. module.stop_streaming()
  30. print(f"STOP STREAM AFTER {BRIDGE_STREAM_INTERVAL} SECONDS")
  31. df = module.fetch_full_stream()
  32. print("STREAM DATA ", df)
  33. df.to_csv("data.csv", index=False)
  34. except Exception as e:
  35. print("EXCEPTION ", str(e))
  36. finally:
  37. port.flush()
  38. port.close()