diff --git a/libraries/tests/mbed/hello/main.cpp b/libraries/tests/mbed/hello/main.cpp index 4b4b91f994..dc83e1c586 100644 --- a/libraries/tests/mbed/hello/main.cpp +++ b/libraries/tests/mbed/hello/main.cpp @@ -2,6 +2,6 @@ int main() { - printf("Hello World\n"); + printf("Hello World\r\n"); while(1); } diff --git a/libraries/tests/mbed/stdio/main.cpp b/libraries/tests/mbed/stdio/main.cpp index f16e4741a8..81552d8715 100644 --- a/libraries/tests/mbed/stdio/main.cpp +++ b/libraries/tests/mbed/stdio/main.cpp @@ -7,24 +7,31 @@ */ int main() { + DigitalOut led1(LED1); + DigitalOut led2(LED2); + union { int value_int; }; notify_start(); + const char* PRINT_PATTERN = "MBED: Your value was: %d\r\n"; + while (true) { // SCANF PRINTF family value_int = 0; + led1 = 1; scanf("%d", &value_int); - printf("Your value was: %d\r\n", value_int); + printf(PRINT_PATTERN, value_int); + led1 = 0; // FSCANF, FPRINTF family value_int = 0; + led2 = 1; fscanf(stdin, "%d", &value_int); - fprintf(stdout, "Your value was: %d\r\n", value_int); - - //... + fprintf(stdout, PRINT_PATTERN, value_int); + led2 = 0; } } diff --git a/workspace_tools/host_tests/dev_null_auto.py b/workspace_tools/host_tests/dev_null_auto.py index cbcfdbb38a..630f07a96a 100644 --- a/workspace_tools/host_tests/dev_null_auto.py +++ b/workspace_tools/host_tests/dev_null_auto.py @@ -19,29 +19,42 @@ from host_test import DefaultTest from sys import stdout class DevNullTest(DefaultTest): + + def check_readline(self, text): + """ Reads line from serial port and checks if text was part of read string + """ + result = False + c = self.mbed.serial_readline() + if c is None: + self.print_result("ioerr_serial") + return None + if text in c: + result = True + return result + def run(self): result = True - str = '' - for i in range(3): - c = self.mbed.serial_read(128) - if c is None: - self.print_result("ioerr_serial") - return - else: - str += c - # Check for expected and unexpected prints in Mbed output - if "re-routing stdout to /null" not in str: - result = False - if "printf redirected to /null" in str: - result = False - if "{failure}" in str: - result = False - if not result: - break - # Data from serial received correctly - print "Received %d bytes:"% len(str) - print str - stdout.flush() + + # Test should print some text and later stop printing + res = self.check_readline("MBED: re-routing stdout to /null") + if not res: + # We haven't read preamble line + result = False + else: + # Check if there are printed characters + str = '' + for i in range(3): + c = self.mbed.serial_read(32) + if c is None: + self.print_result("ioerr_serial") + return + else: + str += c + if len(str) > 0: + result = False + break + print "Received %d bytes: %s"% (len(str), str) + stdout.flush() if result: self.print_result('success') else: diff --git a/workspace_tools/host_tests/hello_auto.py b/workspace_tools/host_tests/hello_auto.py index 8361eb560d..0d5796267d 100644 --- a/workspace_tools/host_tests/hello_auto.py +++ b/workspace_tools/host_tests/hello_auto.py @@ -23,7 +23,7 @@ class HelloTest(DefaultTest): HELLO_WORLD = "Hello World" def run(self): - c = self.mbed.serial_read(128) + c = self.mbed.serial_readline() if c is None: self.print_result("ioerr_serial") return @@ -39,7 +39,7 @@ class HelloTest(DefaultTest): res = re.search('^[$]+[0-9a-fA-F]+' + self.HELLO_WORLD, c) result = res is not None else: - result = (c.startswith(self.HELLO_WORLD)) + result = self.HELLO_WORLD in c if result: # Hello World received self.print_result('success') diff --git a/workspace_tools/host_tests/host_test.py b/workspace_tools/host_tests/host_test.py index 00641e1f07..3fdcb10881 100644 --- a/workspace_tools/host_tests/host_test.py +++ b/workspace_tools/host_tests/host_test.py @@ -24,7 +24,7 @@ except ImportError, e: import os from optparse import OptionParser -from time import sleep +from time import sleep, time from sys import stdout # This is a little tricky. We need to add upper directory to path so @@ -128,6 +128,22 @@ class Mbed: result = None return result + def serial_readline(self, timeout=5): + """ Wraps self.mbed.serial object read method to read one line from serial port + """ + result = '' + start = time() + while (time() - start) < timeout: + if self.serial: + try: + c = self.serial.read(1) + result += c + except: + result = None + if c == '\n': + break + return result + def serial_write(self, write_buffer): """ Wraps self.mbed.serial object write method """ diff --git a/workspace_tools/host_tests/rtc_auto.py b/workspace_tools/host_tests/rtc_auto.py index 91a196eb53..9cdcd829be 100644 --- a/workspace_tools/host_tests/rtc_auto.py +++ b/workspace_tools/host_tests/rtc_auto.py @@ -21,20 +21,17 @@ from time import strftime, gmtime from sys import stdout class RTCTest(DefaultTest): - PATTERN_RTC_VALUE = "^\[(\d+)\] \[(\d+-\d+-\d+ \d+:\d+:\d+ [AaPpMm]{2})\]\\n" + PATTERN_RTC_VALUE = "\[(\d+)\] \[(\d+-\d+-\d+ \d+:\d+:\d+ [AaPpMm]{2})\]" re_detect_rtc_value = re.compile(PATTERN_RTC_VALUE) def run(self): test_result = True - if self.mbed.serial_timeout(None) is None: - self.print_result("ioerr_serial") - return for i in range(0, 5): - c = self.mbed.serial_read(38) # 38 len("[1256729742] [2009-10-28 11:35:42 AM]\n" + c = self.mbed.serial_readline() if c is None: self.print_result("ioerr_serial") return - stdout.flush() + m = self.re_detect_rtc_value.search(c) if m and len(m.groups()): sec = m.groups()[0] @@ -46,6 +43,7 @@ class RTCTest(DefaultTest): stdout.flush() else: print c + stdout.fluch() test_result = False break diff --git a/workspace_tools/host_tests/stdio_auto.py b/workspace_tools/host_tests/stdio_auto.py index 55e026ddea..73f077524b 100644 --- a/workspace_tools/host_tests/stdio_auto.py +++ b/workspace_tools/host_tests/stdio_auto.py @@ -28,52 +28,39 @@ class StdioTest(DefaultTest): def run(self): test_result = True - # Let's wait for Mbed to print its readiness, usually "{{start}}" - if self.mbed.serial_timeout(None) is None: - self.print_result("ioerr_serial") - return - - c = self.mbed.serial_read(len('{{start}}')) + c = self.mbed.serial_readline() # {{start}} preamble if c is None: self.print_result("ioerr_serial") return print c stdout.flush() - if self.mbed.serial_timeout(1) is None: - self.print_result("ioerr_serial") - return - - for i in range(1, 5): + for i in range(0, 5): random_integer = random.randint(-99999, 99999) - print "Generated number: " + str(random_integer) + print "HOST: Generated number: " + str(random_integer) stdout.flush() self.mbed.serial_write(str(random_integer) + "\n") - serial_stdio_msg = "" - ip_msg_timeout = self.mbed.options.timeout - start_serial_pool = time(); - while (time() - start_serial_pool) < ip_msg_timeout: - c = self.mbed.serial_read(512) - if c is None: - self.print_result("ioerr_serial") - return - stdout.write(c) + serial_stdio_msg = self.mbed.serial_readline() + if c is None: + self.print_result("ioerr_serial") + return + print serial_stdio_msg.strip() + stdout.flush() + + # Searching for reply with scanned values + m = self.re_detect_int_value.search(serial_stdio_msg) + if m and len(m.groups()): + int_value = m.groups()[0] + int_value_cmp = random_integer == int(int_value) + test_result = test_result and int_value_cmp + print "Number read %s ... [%s]"% (int_value, "OK" if int_value_cmp else "FAIL") + print stdout.flush() - serial_stdio_msg += c - # Searching for reply with scanned values - m = self.re_detect_int_value.search(serial_stdio_msg) - if m and len(m.groups()): - duration = time() - start_serial_pool - print "Number: " + str(m.groups()[0]) - test_result = test_result and (random_integer == int(m.groups()[0])) - stdout.flush() - break else: - print "Error: No data from MUT sent" - self.print_result('error') - exit(-2) - + test_result = False + break + if test_result: # All numbers are the same self.print_result('success') else: diff --git a/workspace_tools/tests.py b/workspace_tools/tests.py index 01e83031df..8a71f4907d 100644 --- a/workspace_tools/tests.py +++ b/workspace_tools/tests.py @@ -339,7 +339,7 @@ TESTS = [ "source_dir": join(TEST_DIR, "mbed", "sleep"), "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], "duration": 30, - "mcu": ["LPC1768", "LPC11U24", "LPC4088","NRF51822"] + "mcu": ["LPC1768", "LPC11U24", "LPC4088", "NRF51822"] }, { "id": "MBED_5", "description": "PWM",