benq-rm-homeassistant/custom_components/benq_smartboard/serial_test.py
2025-06-06 17:05:15 +07:00

100 lines
2.9 KiB
Python

#!/usr/bin/env python3
"""
Test script for BenQ Smart Board library using USB Serial connection.
"""
import logging
import time
from benq_smartboard_lib import (
BenQSmartBoard,
ConnectionType,
PowerState,
PowerState,
VideoSource,
BenQSmartBoardError
)
# Configure logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def test_serial_connection():
"""Test the serial connection functionality."""
# Configure your serial port here
SERIAL_PORT = 'COM11' # Windows example, use '/dev/ttyUSB0' for Linux
BAUDRATE = 115200
print(f"Testing BenQ Smart Board via Serial: {SERIAL_PORT} @ {BAUDRATE}")
# Create Smart Board instance with Serial connection
board = BenQSmartBoard(
connection_type=ConnectionType.SERIAL,
serial_port=SERIAL_PORT,
baudrate=BAUDRATE,
timeout=5.0
)
try:
# Connect to the Smart Board
print("Connecting to Smart Board...")
board.connect()
print("Connected successfully!")
# Test basic commands
print("\n=== Testing Basic Commands ===")
# Get current power state
try:
print("Expected packet: 801s!000\\r (where 21 hex = ! ASCII, length=8 includes length byte)")
board.set_power(PowerState.ON)
print("Power OFF command sent successfully!")
except Exception as e:
print(f"Failed to set power OFF: {e}")
print("\n=== Test Completed Successfully ===")
except BenQSmartBoardError as e:
print(f"Smart Board Error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
logging.exception("Unexpected error occurred")
finally:
# Always disconnect
print("Disconnecting...")
board.disconnect()
print("Disconnected.")
def list_serial_ports():
"""List available serial ports."""
try:
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
print("Available serial ports:")
for port in ports:
print(f" {port.device} - {port.description}")
return [port.device for port in ports]
except ImportError:
print("pyserial tools not available. Install with: pip install pyserial")
return []
if __name__ == "__main__":
print("BenQ Smart Board Serial Test")
print("=" * 40)
# List available ports
available_ports = list_serial_ports()
if not available_ports:
print("No serial ports found or pyserial not installed.")
print("Make sure to:")
print("1. Install pyserial: pip install pyserial")
print("2. Connect your BenQ Smart Board via USB")
print("3. Update the SERIAL_PORT variable in this script")
else:
print(f"\nFound {len(available_ports)} serial port(s)")
print("\nStarting test...")
test_serial_connection()