grid.py 文件源码

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

项目:grid-control 作者: akej74 项目源码 文件源码
def initialize_grid(ser, lock):
    """Initialize the Grid by sending "0xC0", expected response is "0x21"

    Returns:
        - True for successful initialization
        - False otherwise
    """

    try:
        with lock:
            # Flush input and output buffers
            ser.reset_input_buffer()
            ser.reset_output_buffer()

            # Write data to serial port to initialize the Grid
            bytes_written = ser.write(serial.to_bytes([0xC0]))

            # Wait before checking response
            time.sleep(WAIT_GRID)

            # Read response, one byte = 0x21 is expected for a successful initialization
            response = ser.read(size=1)

            # Check if the Grid responded with any data
            if response:
                # Check for correct response (should be 0x21)
                if response[0] == int("0x21", 16):
                    print("Grid initialized")
                    return True

                # Incorrect response received from the grid
                else:
                    helper.show_error("Problem initializing the Grid unit.\n\n"
                                      "Response 0x21 expected, got " + hex(ord(response)) + ".\n\n"
                                      "Please check serial port " + ser.port +".\n")
                    return False

            # In case no response (0 bytes) from the Grid
            else:
                helper.show_error("Problem initializing the Grid unit.\n\n"
                                  "Response 0x21 expected, no response received.\n\n"
                                   "Please check serial port " + ser.port +".\n")
                return False

    except Exception as e:
            helper.show_error("Problem initializing the Grid unit.\n\n"
                              "Exception:\n" + str(e) + "\n\n"
                              "The application will now exit.")
            sys.exit(0)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号