@@ -57,12 +57,33 @@ def __init__(self):
57
57
print 'Mbed: "%s" "%s"' % (self .port , self .disk )
58
58
59
59
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
66
87
67
88
def safe_sendBreak (self , serial ):
68
89
""" Wraps serial.sendBreak() to avoid serial::serialposix.py exception on Linux
@@ -110,18 +131,28 @@ def run(self):
110
131
print str (e )
111
132
self .print_result ("error" )
112
133
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
+
113
142
def notify (self , message ):
143
+ """ On screen notification function """
114
144
print message
115
145
stdout .flush ()
116
146
117
147
def print_result (self , result ):
148
+ """ Test result unified printing function """
118
149
self .notify ("\n {%s}\n {end}" % result )
119
150
120
151
121
152
class DefaultTest (Test ):
122
153
def __init__ (self ):
123
154
Test .__init__ (self )
124
- self .mbed .init_serial ()
155
+ serial_init_res = self .mbed .init_serial ()
125
156
self .mbed .reset ()
126
157
127
158
"""
@@ -140,7 +171,10 @@ class Simple(DefaultTest):
140
171
def run (self ):
141
172
try :
142
173
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
144
178
stdout .write (c )
145
179
stdout .flush ()
146
180
except KeyboardInterrupt , _ :
0 commit comments