grid.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:grid-control 作者: akej74 项目源码 文件源码
def read_fan_voltage(ser, lock):
    """Reads the current voltage of each fan.

    Returns:
        - If success: a list with voltage data for each fan
        - If failure to read data: An empty list
    """

    # List to hold fan voltage data to be returned
    fans = []

    with lock:
        for fan in [0x01, 0x02, 0x03, 0x04, 0x05, 0x06]:
            try:
                # Define bytes to be sent to the Grid for reading voltage for a specific fan
                # Format is two bytes, e.g. [0x84, <fan id>]
                serial_data = [0x84, fan]

                ser.reset_output_buffer()
                bytes_written = ser.write(serial.to_bytes(serial_data))

                # Wait before checking response
                time.sleep(WAIT_GRID)

                # Expected response is 5 bytes
                # Example response: 00 00 00 0B 01 = 0x0B 0x01 = 11.01 volt
                response = ser.read(size=5)

                # Check if the Grid responded with any data
                if response:
                    # Check for correct response (first three bytes should be 0x00)
                    if response[0] == int("0xC0", 16) and response[1] == response[2] == int("0x00", 16):
                        # Convert last two bytes to a decimal float value
                        voltage = float(str(response[3]) + "." + str(response[4]))
                        # Add the voltage for the current fan to the list
                        fans.append(voltage)

                    # An incorrect response was received
                    else:
                        print("Error reading fan voltage, incorrect response")
                        return []

                # In case no response (0 bytes) is returned from the Grid
                else:
                    print("Error reading fan voltage, no data returned")
                    return []

            except Exception as e:
                helper.show_error("Could not read fan voltage.\n\n"
                                  "Please check serial port " + ser.port + ".\n\n"
                                  "Exception:\n" + str(e) + "\n\n"
                                  "The application will now exit.")
                print(str(e))
                sys.exit(0)

        return fans
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号