How to communicate with BENCHLAB in Python

How to communicate with BENCHLAB in Python

This blog post will guide you through utilizing the provided Python code example for interacting with BENCHLAB. The sample code allows you to connect to communicate with the BENCHLAB in Python to retrieve sensor readings and process the data.

Prerequisites

Before diving into the code, ensure you have the following ready:

  • Python: I prefer to use an environment manager like Anaconda or Miniconda to manage my Python installations.
  • BENCHLAB: You'll need a BENCHLAB connected to your computer via the USB port
  • BENCHLAB Python Sample: you'll need the Python sample code which you can find on the BENCHLAB GitHub.

     

    Code Breakdown

    The provided BENCHLAB Python Sample code incorporates several key functionalities:

    Package Imports:

    • serial: This library is used for communicating with serial devices, such as Benchlab.
    • serial.tools.list_ports: This module helps in finding available serial ports.
    • ctypes: This library allows for interaction with C-types data structures used in this sample code.
    • enum: This library is used to create enumerations, which make code more readable and maintainable by defining named constants.

    Constants

    • BENCHLAB_VENDOR_ID, BENCHLAB_PRODUCT_ID, BENCHLAB_FIRMWARE_VERSION: These values identify the BENCHLAB device and its expected firmware version.
    • SENSOR_VIN_NUM, SENSOR_POWER_NUM, FAN_NUM: These numbers define the expected number of voltage, power, and fan sensors in the device.

    Structures

    • VendorDataStruct: This structure stores information about the device, such as its vendor ID, product ID, and firmware version.
    • PowerSensor: This structure represents a power sensor, holding values for voltage, current, and power.
    • FanSensor: This structure represents a fan sensor, holding values for fan enable state, duty cycle, and tachometer readout.
    • SensorStruct: This structure is used for various sensor readings, including voltage, temperature, humidity, power readings, and fan readings.

    Enums

    BENCHLAB_CMD: This enumeration defines command codes that can be sent to the Benchlab device, such as reading sensor values, reading the welcome string, or reading vendor data.

    The sample code illustrates how you can connect to the BENCHLAB and read out sensor information. The process is as follows:

    1. Find BENCHLAB Serial Port: The code searches for available serial ports using serial.tools.list_ports. It specifically looks for ports with vendor ID 0xEE and product ID 0x10.
    2. Establish Serial Connection: If a Benchlab port is found, it establishes a serial connection to that port using the serial library.
    3. Communicate with Benchlab Device: The code sends various commands to the Benchlab device using the ser.write() function. It reads responses back from the device using ser.read(). Commands include:
      • Reading the welcome string
      • Reading vendor data
      • Reading sensor values
    4. Parse and Print Data: The code parses the responses from the device using the defined structures (VendorDataStruct and SensorStruct). It prints the parsed data in a user-readable format, organizing information about voltages, temperatures, fan readings, power consumption, etc.

     

    Running the Code

    To get started, I will first set up a new Conda environment for the BENCHLAB project, then install all the prerequisites, and end with an example to capture the output.

    Installation:

    conda create -n benchlab

    > creates the new conda environment named benchlab

    conda activate benchlab

    > activates our benchlab environment

    conda install python

    > installs the python package

    conda install git

    > downloads the git installer package

    pip install pyserial

    > this package is required to communicate with the benchlab

    git clone https://github.com/BenchLab-io/BENCHLAB.PythonSample benchlab

    > copies the benchlab python sample to a folder named benchlab

    Running the code:

    python ./benchlab/benchlab.py

    If you ran everything correctly, the following output should be seen upon execution the command:

    Error Messages:

    "No BENCHLAB Serial Ports found" > your benchlab is not connected to the system"

    ModuleNotFoundError: No module named 'serial'" > you didn’t install pyserial package

     

    Explanation of Output

    The code outputs various sensor readings and status, including:
    • ATX24V, 12V, 5V, 5VSB, and 3.3V readings (voltage, current, and power)
    • EPS1 and EPS2 readings (voltage, current, and power)
    • PCIE and 12VHPWR readings (voltage, current, and power)
    • System, CPU, GPU, and motherboard total power consumption
    • TS1-4 temperature readings
    • Ambient temperature (TAMB) and humidity (HUM) reading
    • Fan Ext Duty status, Fan Switch status, RGB Switch status, and RGB Ext status
    • VIN1-12 sensor readings
    • Fan 1-9 enable status, duty cycle, and tachometer

     

    Conclusion

    This example code provides a foundation for interacting with BENCHLAB using Python.

    By understanding the code structure and functionalities, you can adapt it to your specific needs, such as logging sensor data to a file or creating visualizations for further analysis.

    We'll cover such examples in future blog posts, so stay tuned for more!

    Back to blog