@@ -20,39 +20,84 @@ def test_list(socketio):
20
20
assert any ("Ports" in i for i in message )
21
21
assert any ("Network" in i for i in message )
22
22
23
- # NOTE run the following tests on linux with a board connected to the PC and with the sketch found in test/testdata/SerialEcho.ino on it
23
+ # NOTE run the following tests with a board connected to the PC
24
24
@pytest .mark .skipif (
25
25
running_on_ci (),
26
26
reason = "VMs have no serial ports" ,
27
27
)
28
- def test_open_serial_default (socketio ):
29
- general_test_serial (socketio , "default" )
28
+ def test_open_serial_default (socketio , serial_port , baudrate ):
29
+ general_open_serial (socketio , serial_port , baudrate , "default" )
30
30
31
31
32
32
@pytest .mark .skipif (
33
33
running_on_ci (),
34
34
reason = "VMs have no serial ports" ,
35
35
)
36
- def test_open_serial_timed (socketio ):
37
- general_test_serial (socketio , "timed" )
36
+ def test_open_serial_timed (socketio , serial_port , baudrate ):
37
+ general_open_serial (socketio , serial_port , baudrate , "timed" )
38
38
39
39
40
40
@pytest .mark .skipif (
41
41
running_on_ci (),
42
42
reason = "VMs have no serial ports" ,
43
43
)
44
- def test_open_serial_timedraw (socketio ):
45
- general_test_serial (socketio , "timedraw" )
44
+ def test_open_serial_timedraw (socketio , serial_port , baudrate ):
45
+ general_open_serial (socketio , serial_port , baudrate , "timedraw" )
46
46
47
47
48
- def general_test_serial (socketio , buffertype ):
49
- port = "/dev/ttyACM0"
48
+ # NOTE run the following tests with a board connected to the PC and with the sketch found in test/testdata/SerialEcho.ino on it be sure to change serial_address in conftest.py
49
+ @pytest .mark .skipif (
50
+ running_on_ci (),
51
+ reason = "VMs have no serial ports" ,
52
+ )
53
+ def test_send_serial_default (socketio , close_port , serial_port , baudrate ):
54
+ general_send_serial (socketio , close_port , serial_port , baudrate , "default" )
55
+
56
+
57
+ @pytest .mark .skipif (
58
+ running_on_ci (),
59
+ reason = "VMs have no serial ports" ,
60
+ )
61
+ def test_send_serial_timed (socketio , close_port , serial_port , baudrate ):
62
+ general_send_serial (socketio , close_port , serial_port , baudrate , "timed" )
63
+
64
+
65
+ @pytest .mark .skipif (
66
+ running_on_ci (),
67
+ reason = "VMs have no serial ports" ,
68
+ )
69
+ def test_send_serial_timedraw (socketio , close_port , serial_port , baudrate ):
70
+ general_send_serial (socketio , close_port , serial_port , baudrate , "timedraw" )
71
+
72
+
73
+ def general_open_serial (socketio , serial_port , baudrate , buffertype ):
74
+ global message
75
+ message = []
76
+ # in message var we will find the "response"
77
+ socketio .on ('message' , message_handler )
78
+ socketio .emit ('command' , 'open ' + serial_port + ' ' + baudrate + ' ' + buffertype )
79
+ # give time to the message var to be filled
80
+ time .sleep (.5 )
81
+ print (message )
82
+ # the serial connection should be open now
83
+ assert any ("\" IsOpen\" : true" in i for i in message )
84
+
85
+ # close the serial port
86
+ socketio .emit ('command' , 'close ' + serial_port )
87
+ time .sleep (.2 )
88
+ print (message )
89
+ #check if port has been closed
90
+ assert any ("\" IsOpen\" : false," in i for i in message )
91
+
92
+
93
+
94
+ def general_send_serial (socketio , close_port , serial_port , baudrate , buffertype ):
50
95
global message
51
96
message = []
52
97
#in message var we will find the "response"
53
98
socketio .on ('message' , message_handler )
54
99
#open a new serial connection with the specified buffertype, if buffertype is empty it will use the default one
55
- socketio .emit ('command' , 'open ' + port + ' 9600 ' + buffertype )
100
+ socketio .emit ('command' , 'open ' + serial_port + ' ' + baudrate + ' ' + buffertype )
56
101
# give time to the message var to be filled
57
102
time .sleep (.5 )
58
103
print (message )
@@ -61,11 +106,11 @@ def general_test_serial(socketio, buffertype):
61
106
62
107
#test with string
63
108
# send the string "ciao" using the serial connection
64
- socketio .emit ('command' , 'send ' + port + ' /"ciao/"' )
109
+ socketio .emit ('command' , 'send ' + serial_port + ' /"ciao/"' )
65
110
time .sleep (1 )
66
111
print (message )
67
112
# check if the send command has been registered
68
- assert any ("send " + port + " /\" ciao/\" " in i for i in message )
113
+ assert any ("send " + serial_port + " /\" ciao/\" " in i for i in message )
69
114
#check if message has been sent back by the connected board
70
115
if buffertype == "timedraw" :
71
116
output = decode_output (extract_serial_data (message ))
@@ -76,23 +121,45 @@ def general_test_serial(socketio, buffertype):
76
121
#test with emoji
77
122
message = [] # reinitialize the message buffer to have a clean situation
78
123
# send a lot of emoji: they can be messed up
79
- socketio .emit ('command' , 'send ' + port + ' /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"' )
80
- time .sleep (.2 )
124
+ socketio .emit ('command' , 'send ' + serial_port + ' /"🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/"' )
125
+ time .sleep (.5 )
81
126
print (message )
82
127
# check if the send command has been registered
83
- assert any ("send " + port + " /\" 🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\" " in i for i in message )
128
+ assert any ("send " + serial_port + " /\" 🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\" " in i for i in message )
84
129
if buffertype == "timedraw" :
85
130
output = decode_output (extract_serial_data (message ))
86
131
elif buffertype in ("default" , "timed" ):
87
132
output = extract_serial_data (message )
88
133
assert "/\" 🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\" " in output
134
+ # the serial connection is closed by close_port() fixture: even if in case of test failure
89
135
90
- #finally close the serial port
91
- socketio .emit ('command' , 'close ' + port )
92
- time .sleep (.2 )
93
- print (message )
94
- #check if port has been closed
95
- assert any ("\" IsOpen\" : false," in i for i in message )
136
+
137
+ def test_sendraw_serial (socketio , close_port , serial_port , baudrate ):
138
+ global message
139
+ message = []
140
+ #in message var we will find the "response"
141
+ socketio .on ('message' , message_handler )
142
+ #open a new serial connection with the specified buffertype, if buffertype is empty it will use the default one
143
+ socketio .emit ('command' , 'open ' + serial_port + ' ' + baudrate + ' timedraw' )
144
+ # give time to the message var to be filled
145
+ time .sleep (.5 )
146
+ print (message )
147
+ # the serial connection should be open now
148
+ assert any ("\" IsOpen\" : true" in i for i in message )
149
+
150
+ #test with bytes
151
+ integers = [1 , 2 , 3 , 4 , 5 ]
152
+ bytes_array = bytearray (integers )
153
+ encoded_integers = base64 .b64encode (bytes_array ).decode ('ascii' )
154
+ socketio .emit ('command' , 'sendraw ' + serial_port + ' ' + encoded_integers )
155
+ time .sleep (1 )
156
+ print (message )
157
+ # check if the send command has been registered
158
+ assert any (("sendraw " + serial_port + ' ' + encoded_integers ) in i for i in message )
159
+ #check if message has been sent back by the connected board
160
+ output = extract_serial_data (message ) # TODO use decode_output()
161
+ print (output )
162
+ assert encoded_integers in output
96
163
97
164
98
165
# callback called by socketio when a message is received
0 commit comments