From 8184134a2f866bbea0db990c2f50f10991b41cd5 Mon Sep 17 00:00:00 2001 From: hanhsuan Date: Thu, 2 Jan 2025 16:00:40 +0800 Subject: [PATCH 1/3] 1. enhance the server mode could handle binary data without crashing 2. clean up garbage in the serial port before testing --- .../bin/serial_test.py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py index 42bda9e548..be88ebf443 100755 --- a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py +++ b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py @@ -82,7 +82,9 @@ def __init__( try: self.group.append(self.serial_init(ser)) except Exception: - raise SystemError("Failed to init serial port: {}".format(ser)) + raise SystemError( + "Failed to init serial port: {}".format(ser) + ) def serial_init(self, node: str) -> serial.Serial: """Create a serial.Serial object based on the class variables""" @@ -119,8 +121,10 @@ def recv(self) -> bytes: if rcv: logging.info("Received: {}".format(rcv.decode())) except Exception: - logging.exception("Received unmanageable string format") - raise SystemExit(1) + logging.exception( + "Received unmanageable string format {}".format(rcv) + ) + return None return rcv @@ -157,6 +161,9 @@ def client_mode(ser: Serial, data_size: int = 1024): running on port /dev/ttymxc1 as a client $ sudo ./serial_test.py /dev/ttymxc1 --mode client --type RS485 """ + # clean up the garbage in the serial before test + while ser.recv() is not None: + continue random_string = generate_random_string(data_size) ser.send(random_string.encode()) for i in range(1, 6): @@ -192,7 +199,9 @@ def console_mode(ser: Serial): logging.info("[PASS] Serial console test successful.") else: logging.error("[FAIL] Serial console test failed.") - logging.error("Expected response should contain ':~$' or 'login:'") + logging.error( + "Expected response should contain ':~$' or 'login:'" + ) raise SystemExit(1) except Exception: logging.exception("Caught an exception.") @@ -203,7 +212,9 @@ def create_args(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument("node", help="Serial port device node e.g. /dev/ttyS1") + parser.add_argument( + "node", help="Serial port device node e.g. /dev/ttyS1" + ) parser.add_argument( "--mode", choices=["server", "client", "console"], From 584dc32f0559b8f791ffc58513cf0f5942800fcd Mon Sep 17 00:00:00 2001 From: hanhsuan Date: Wed, 15 Jan 2025 09:50:04 +0800 Subject: [PATCH 2/3] fix the clean up couldn't work issue --- .../checkbox-provider-ce-oem/bin/serial_test.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py index be88ebf443..a86686ae79 100755 --- a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py +++ b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py @@ -161,10 +161,12 @@ def client_mode(ser: Serial, data_size: int = 1024): running on port /dev/ttymxc1 as a client $ sudo ./serial_test.py /dev/ttymxc1 --mode client --type RS485 """ + + random_string = generate_random_string(data_size) + # clean up the garbage in the serial before test - while ser.recv() is not None: + while ser.recv() : continue - random_string = generate_random_string(data_size) ser.send(random_string.encode()) for i in range(1, 6): logging.info("Attempting receive string... {} time".format(i)) From d3842b75133154a23e5b0347a1a0cab610e4e434 Mon Sep 17 00:00:00 2001 From: hanhsuan Date: Wed, 15 Jan 2025 10:10:43 +0800 Subject: [PATCH 3/3] fix black format error --- .../checkbox-provider-ce-oem/bin/serial_test.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py index a86686ae79..af2f53ec8b 100755 --- a/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py +++ b/contrib/checkbox-ce-oem/checkbox-provider-ce-oem/bin/serial_test.py @@ -82,9 +82,7 @@ def __init__( try: self.group.append(self.serial_init(ser)) except Exception: - raise SystemError( - "Failed to init serial port: {}".format(ser) - ) + raise SystemError("Failed to init serial port: {}".format(ser)) def serial_init(self, node: str) -> serial.Serial: """Create a serial.Serial object based on the class variables""" @@ -165,7 +163,7 @@ def client_mode(ser: Serial, data_size: int = 1024): random_string = generate_random_string(data_size) # clean up the garbage in the serial before test - while ser.recv() : + while ser.recv(): continue ser.send(random_string.encode()) for i in range(1, 6): @@ -201,9 +199,7 @@ def console_mode(ser: Serial): logging.info("[PASS] Serial console test successful.") else: logging.error("[FAIL] Serial console test failed.") - logging.error( - "Expected response should contain ':~$' or 'login:'" - ) + logging.error("Expected response should contain ':~$' or 'login:'") raise SystemExit(1) except Exception: logging.exception("Caught an exception.") @@ -214,9 +210,7 @@ def create_args(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) - parser.add_argument( - "node", help="Serial port device node e.g. /dev/ttyS1" - ) + parser.add_argument("node", help="Serial port device node e.g. /dev/ttyS1") parser.add_argument( "--mode", choices=["server", "client", "console"],