From af9e1e62dded7f65cc654a5ff70c903a720e61bc Mon Sep 17 00:00:00 2001 From: Bruno Rocci Date: Tue, 12 May 2026 12:16:01 +0200 Subject: [PATCH] Fix missing STX (0x02) DLE-escape in transmit() --- src/CMRI.cpp | 7 +++---- test/test_cmri/test_main.cpp | 10 ++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/CMRI.cpp b/src/CMRI.cpp index 6c91073..461cc6a 100644 --- a/src/CMRI.cpp +++ b/src/CMRI.cpp @@ -143,10 +143,9 @@ void CMRI::transmit() _serial.write(GET); for (int i = 0; i < _tx_length; i++) { - if (_tx_buffer[i] == ETX) - _serial.write(ESC); // escape because this looks like an STX bit (very basic protocol) - if (_tx_buffer[i] == ESC) - _serial.write(ESC); // escape because this looks like an escape bit (very basic protocol) + // DLE-escape STX, ETX and DLE characters + if (_tx_buffer[i] == STX || _tx_buffer[i] == ETX || _tx_buffer[i] == ESC) + _serial.write(ESC); _serial.write(_tx_buffer[i]); } _serial.write(ETX); diff --git a/test/test_cmri/test_main.cpp b/test/test_cmri/test_main.cpp index 4261a86..e4b60aa 100644 --- a/test/test_cmri/test_main.cpp +++ b/test/test_cmri/test_main.cpp @@ -61,9 +61,11 @@ void test_set_bit_reflected_in_transmit(void) cmri.transmit(); // Frame: FF FF STX 'A' GET <3 data bytes> ETX - TEST_ASSERT_EQUAL_UINT8(0x01, s.tx[5]); // byte 0, bit 0 - TEST_ASSERT_EQUAL_UINT8(0x02, s.tx[6]); // byte 1, bit 1 - TEST_ASSERT_EQUAL_UINT8(0x00, s.tx[7]); + // byte 1 = 0x02 (STX), so it gets DLE-escaped to ESC 0x02 + TEST_ASSERT_EQUAL_UINT8(0x01, s.tx[5]); // byte 0, bit 0 + TEST_ASSERT_EQUAL_UINT8(CMRI::ESC, s.tx[6]); // DLE escape for byte 1 (STX value) + TEST_ASSERT_EQUAL_UINT8(0x02, s.tx[7]); // byte 1, bit 1 + TEST_ASSERT_EQUAL_UINT8(0x00, s.tx[8]); // byte 2 } // Regression for the set_bit() bounds bug: the old (pos + 7) / 8 check wrongly @@ -154,7 +156,7 @@ void test_address_filtering(void) TEST_ASSERT_EQUAL_UINT8(0, cmri.get_byte(0)); } -// Data bytes that collide with ETX/ESC are escaped in the transmitted frame. +// Data bytes that collide with STX, ETX or ESC are escaped in the transmitted frame. void test_transmit_escapes_control_bytes(void) { Stream s;