Skip to content

Connect Device

This tutorial shows how to connect to a Senxor device and retrieve its basic information.

1. Import the connect and list_senxor functions.

from senxor import connect, list_senxor

2. List all available devices.

devices = list_senxor("serial")

The interface argument is a positional parameter that specifies the interface type to search for devices, by default is "serial".

Note: Currently, only the serial port interface ("serial") is supported.

3. Connect to the first available device.

if not devices:
    raise ValueError("No devices found")

dev = connect(devices[0])

4. Print the device connection status and basic information.

print(f"Connected to {dev.name}")
print(f"Device: {dev.device}")
print(f"Is connected: {dev.is_connected}")
print(f"Is streaming: {dev.is_streaming}")

Output:

Connected to COM7
Device: SerialPort COM7
Is connected: True
Is streaming: False

5. Query and print device hardware and firmware information.

print(f"Module type: {dev.get_module_type()}")
print(f"Firmware version: {dev.get_fw_version()}")
print(f"Serial number: {dev.get_sn()}")
print(f"MCU type: {dev.get_mcu_type()}")

Output:

Module type: MI1602M6C
Firmware version: 4.6.36
Serial number: 182A01001769
MCU type: MI48E

6. Close the device connection.

dev.close()

Remember to close the device connection after using to release resources.

7. Using context manager for automatic resource management.

You can also use the with statement to automatically close the device connection after the block is executed.

with connect(devices[0]) as dev:
    print(f"Connected to {dev.name}")
    print(f"Is connected: {dev.is_connected}")

print(f"Is connected: {dev.is_connected}")

Output:

Connected to COM7
Is connected: True
Is connected: False

Summary

  • list_senxor() lists all available devices.
  • connect() connects to a device.