@@ -20,39 +20,84 @@ def test_list(socketio):
2020 assert any ("Ports" in i for i in message )
2121 assert any ("Network" in i for i in message )
2222
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
2424@pytest .mark .skipif (
2525 running_on_ci (),
2626 reason = "VMs have no serial ports" ,
2727)
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" )
3030
3131
3232@pytest .mark .skipif (
3333 running_on_ci (),
3434 reason = "VMs have no serial ports" ,
3535)
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" )
3838
3939
4040@pytest .mark .skipif (
4141 running_on_ci (),
4242 reason = "VMs have no serial ports" ,
4343)
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" )
4646
4747
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 ):
5095 global message
5196 message = []
5297 #in message var we will find the "response"
5398 socketio .on ('message' , message_handler )
5499 #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 )
56101 # give time to the message var to be filled
57102 time .sleep (.5 )
58103 print (message )
@@ -61,11 +106,11 @@ def general_test_serial(socketio, buffertype):
61106
62107 #test with string
63108 # send the string "ciao" using the serial connection
64- socketio .emit ('command' , 'send ' + port + ' /"ciao/"' )
109+ socketio .emit ('command' , 'send ' + serial_port + ' /"ciao/"' )
65110 time .sleep (1 )
66111 print (message )
67112 # 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 )
69114 #check if message has been sent back by the connected board
70115 if buffertype == "timedraw" :
71116 output = decode_output (extract_serial_data (message ))
@@ -76,23 +121,45 @@ def general_test_serial(socketio, buffertype):
76121 #test with emoji
77122 message = [] # reinitialize the message buffer to have a clean situation
78123 # 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 )
81126 print (message )
82127 # 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 )
84129 if buffertype == "timedraw" :
85130 output = decode_output (extract_serial_data (message ))
86131 elif buffertype in ("default" , "timed" ):
87132 output = extract_serial_data (message )
88133 assert "/\" 🧀🧀🧀🧀🧀🧀🧀🧀🧀🧀/\" " in output
134+ # the serial connection is closed by close_port() fixture: even if in case of test failure
89135
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
96163
97164
98165# callback called by socketio when a message is received
0 commit comments