Skip to content

Commit 82e4a67

Browse files
committed
Added seral port read/write wrappers to handle serial port communication issues. Added IOERR_SERIAL handler for basic test. Other tests will be refactored in separate commits.
1 parent f1db89e commit 82e4a67

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

workspace_tools/host_tests/host_test.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,33 @@ def __init__(self):
5757
print 'Mbed: "%s" "%s"' % (self.port, self.disk)
5858

5959
def init_serial(self, baud=9600, extra_baud=9600):
60-
self.serial = Serial(self.port, timeout = 1)
61-
self.serial.setBaudrate(baud)
62-
if self.extra_port:
63-
self.extra_serial = Serial(self.extra_port, timeout = 1)
64-
self.extra_serial.setBaudrate(extra_baud)
65-
self.flush()
60+
result = True
61+
try:
62+
self.serial = Serial(self.port, timeout=1)
63+
except Exception as e:
64+
result = False
65+
# Port can be opened
66+
if result:
67+
self.serial.setBaudrate(baud)
68+
if self.extra_port:
69+
self.extra_serial = Serial(self.extra_port, timeout = 1)
70+
self.extra_serial.setBaudrate(extra_baud)
71+
self.flush()
72+
return result
73+
74+
def serial_read(self, count=1):
75+
""" Wraps self.mbed.serial object read method """
76+
result = None
77+
if self.serial:
78+
result = self.serial.read(count)
79+
return result
80+
81+
def serial_write(self, write_buffer):
82+
""" Wraps self.mbed.serial object write method """
83+
result = -1
84+
if self.serial:
85+
result = self.serial.write(write_buffer)
86+
return result
6687

6788
def safe_sendBreak(self, serial):
6889
""" Wraps serial.sendBreak() to avoid serial::serialposix.py exception on Linux
@@ -110,18 +131,28 @@ def run(self):
110131
print str(e)
111132
self.print_result("error")
112133

134+
def setup(self):
135+
""" Setup and check if configuration for test is correct. E.g. if serial port can be opened """
136+
result = True
137+
if not self.mbed.serial:
138+
result = False
139+
self.print_result("ioerr_serial")
140+
return result
141+
113142
def notify(self, message):
143+
""" On screen notification function """
114144
print message
115145
stdout.flush()
116146

117147
def print_result(self, result):
148+
""" Test result unified printing function """
118149
self.notify("\n{%s}\n{end}" % result)
119150

120151

121152
class DefaultTest(Test):
122153
def __init__(self):
123154
Test.__init__(self)
124-
self.mbed.init_serial()
155+
serial_init_res = self.mbed.init_serial()
125156
self.mbed.reset()
126157

127158
"""
@@ -140,7 +171,10 @@ class Simple(DefaultTest):
140171
def run(self):
141172
try:
142173
while True:
143-
c = self.mbed.serial.read(512)
174+
c = self.mbed.serial_read(512)
175+
if c is None:
176+
self.print_result("ioerr_serial")
177+
break
144178
stdout.write(c)
145179
stdout.flush()
146180
except KeyboardInterrupt, _:

0 commit comments

Comments
 (0)