diff --git a/custom_components/benq_smartboard/benq_smartboard_lib.py b/custom_components/benq_smartboard/benq_smartboard_lib.py index faab2e9..c01ae3b 100644 --- a/custom_components/benq_smartboard/benq_smartboard_lib.py +++ b/custom_components/benq_smartboard/benq_smartboard_lib.py @@ -324,12 +324,9 @@ class BenQSmartBoard: self._ensure_connection() # Protocol format: [length][tv_id][command_type][command_code][value][CR] - # Example: 801s!000\r where command_code 21(hex) = !(ASCII) - # Length includes the length byte itself: 1 + tv_id(2) + command_type(1) + command_code(1) + value(3) = 8 + # Length includes the length byte itself if len(command_code) != 2: raise ValueError("command_code must be 2 ASCII chars (hex).") - if len(value) != 3: - raise ValueError("value must be 3 ASCII chars.") # Convert hex command code to ASCII character try: @@ -338,12 +335,14 @@ class BenQSmartBoard: except ValueError: raise ValueError(f"Invalid hex command code: {command_code}") - # Length = length_byte(1) + tv_id(2) + command_type(1) + command_code(1) + value(3) = 8 bytes total - length_ascii = b'8' tv_id_ascii = b'01' command_type_ascii = b's' # Set command is 's' value_ascii = value.encode('ascii') + # Calculate dynamic length: length_byte(1) + tv_id(2) + command_type(1) + command_code(1) + value(variable) + total_length = 1 + len(tv_id_ascii) + len(command_type_ascii) + len(command_code_ascii) + len(value_ascii) + length_ascii = str(total_length).encode('ascii') + try: packet = length_ascii + tv_id_ascii + command_type_ascii + command_code_ascii + value_ascii + self.CR _log_packet("Sent", packet) @@ -378,12 +377,13 @@ class BenQSmartBoard: except ValueError: raise ValueError(f"Invalid hex command code: {command_code}") - # For get command: length + tv_id + command_type + command_code + CR - # Length = length_byte(1) + tv_id(2) + command_type(1) + command_code(1) = 5 bytes total - length_ascii = b'5' tv_id_ascii = b'01' command_type_ascii = b'g' # Get command is 'g' + # Calculate dynamic length: length_byte(1) + tv_id(2) + command_type(1) + command_code(1) + total_length = 1 + len(tv_id_ascii) + len(command_type_ascii) + len(command_code_ascii) + length_ascii = str(total_length).encode('ascii') + try: logging.debug(f"Sending get command: code={command_code}") packet = length_ascii + tv_id_ascii + command_type_ascii + command_code_ascii + self.CR diff --git a/custom_components/benq_smartboard/serial_test.py b/custom_components/benq_smartboard/serial_test.py index f355493..53f0339 100644 --- a/custom_components/benq_smartboard/serial_test.py +++ b/custom_components/benq_smartboard/serial_test.py @@ -50,6 +50,9 @@ def test_serial_connection(): try: print("Expected packet: 801s!000\\r (where 21 hex = ! ASCII, length=8 includes length byte)") board.set_power(PowerState.ON) + time.sleep(2) + board.set_volume(20) + board.set_video_source(VideoSource.OPS) print("Power OFF command sent successfully!") except Exception as e: print(f"Failed to set power OFF: {e}")