import socket import struct import time import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') # Configuration for the Smart Board: IP_ADDRESS = "10.10.4.173" # Replace with your board's IP PORT = 4660 TIMEOUT = 5.0 TV_ID = b'01' CR = b'\x0D' def log_packet(direction: str, data: bytes): hex_data = data.hex().upper() logging.debug(f"{direction} [{len(data)} bytes]: {hex_data}") def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(TIMEOUT) try: logging.debug(f"Connecting to {IP_ADDRESS}:{PORT}") sock.connect((IP_ADDRESS, PORT)) logging.info("Connected.") # Build "get_power" command # Protocol: length=5, TV_ID, 'g' (get command), command_code (1 byte) and CR. command_code = b'\x6C' # Power get command length = 5 packet = struct.pack(">B2s1s1s1s", length, TV_ID, b'g', command_code, CR) log_packet("Sent", packet) sock.sendall(packet) time.sleep(1) # Wait a bit for response # Receive header first (expected 5 bytes): header = sock.recv(5) log_packet("Received", header) if len(header) < 5: logging.error("Incomplete header received.") return command_type = header[3:4] if command_type != b'r': logging.error(f"Unexpected response type: {command_type}") return # Wait a bit before reading the value bytes. time.sleep(0.1) value = sock.recv(3) log_packet("Received", value) if len(value) < 3: logging.error("Incomplete value bytes received.") return power_state = value.decode() logging.info(f"Power state: {power_state}") except Exception as ex: logging.exception(f"Error during manual test: {ex}") finally: logging.debug("Closing socket.") sock.close() logging.info("Connection closed.") if __name__ == '__main__': main()