@@ -26,115 +26,80 @@ def test_list(socketio):
26
26
reason = "VMs have no serial ports" ,
27
27
)
28
28
def test_open_serial_default (socketio ):
29
- time .sleep (.2 )
30
- global message
31
- message = []
32
- socketio .on ('message' , message_handler )
33
- socketio .emit ('command' , 'open /dev/ttyACM0 9600' )
34
- time .sleep (.5 ) # give time to message to be filled
35
- assert any ("\" IsOpen\" : true" in i for i in message )
36
- socketio .emit ('command' , 'send /dev/ttyACM0 /"ciao/"' )
37
- time .sleep (.2 )
38
- assert any ("send /dev/ttyACM0 /\" ciao/\" " in i for i in message )
39
- assert "ciao" in extract_serial_data (message )
29
+ general_test_serial (socketio , "default" )
40
30
41
- # test with a lot of emoji: they can be messed up
42
- # message = [] # reinitialize the message buffer
43
- socketio .emit ('command' , 'send /dev/ttyACM0 /"π§π§π§π§π§π§π§π§π§π§/"' )
44
- time .sleep (.2 )
45
- print (message )
46
- assert any ("send /dev/ttyACM0 /\" π§π§π§π§π§π§π§π§π§π§/\" " in i for i in message )
47
- emoji_output = extract_serial_data (message )
48
- assert "/\" π§π§π§π§π§π§π§π§π§π§/\" " in emoji_output # this could be failing because of UTF8 encoding problems
49
- message = []
50
- socketio .emit ('command' , 'close /dev/ttyACM0' )
51
- time .sleep (.2 )
52
- assert any ("\" IsOpen\" : false," in i for i in message )
53
31
54
32
@pytest .mark .skipif (
55
33
running_on_ci (),
56
34
reason = "VMs have no serial ports" ,
57
35
)
58
36
def test_open_serial_timed (socketio ):
59
- time .sleep (.2 )
60
- global message
61
- message = []
62
- socketio .on ('message' , message_handler )
63
- socketio .emit ('command' , 'open /dev/ttyACM0 9600 timed' )
64
- time .sleep (.5 ) # give time to message to be filled
65
- print (message )
66
- assert any ("\" IsOpen\" : true" in i for i in message )
67
- socketio .emit ('command' , 'send /dev/ttyACM0 /"ciao/"' )
68
- time .sleep (.2 )
69
- assert any ("send /dev/ttyACM0 /\" ciao/\" " in i for i in message )
70
- assert "ciao" in extract_serial_data (message )
37
+ general_test_serial (socketio , "timed" )
71
38
72
- # test with a lot of emoji: usually they get messed up
73
- message = [] # reinitialize the message buffer
74
- socketio .emit ('command' , 'send /dev/ttyACM0 /"π§π§π§π§π§π§π§π§π§π§/"' )
75
- time .sleep (.2 )
76
- assert any ("send /dev/ttyACM0 /\" π§π§π§π§π§π§π§π§π§π§/\" " in i for i in message )
77
- assert "/\" π§π§π§π§π§π§π§π§π§π§/\" " in extract_serial_data (message )
78
- message = []
79
- socketio .emit ('command' , 'close /dev/ttyACM0' )
80
- time .sleep (.2 )
81
- # print (message)
82
- assert any ("\" IsOpen\" : false," in i for i in message )
83
39
84
40
@pytest .mark .skipif (
85
41
running_on_ci (),
86
42
reason = "VMs have no serial ports" ,
87
43
)
88
44
def test_open_serial_timedraw (socketio ):
89
- global message
90
- message = []
91
- socketio .on ('message' , message_handler )
92
- socketio .emit ('command' , 'open /dev/ttyACM0 9600 timedraw' )
93
- time .sleep (.5 ) # give time to message to be filled
94
- assert any ("\" IsOpen\" : true" in i for i in message )
95
- socketio .emit ('command' , 'send /dev/ttyACM0 /"ciao/"' )
96
- time .sleep (.2 )
97
- assert any ("send /dev/ttyACM0 /\" ciao/\" " in i for i in message )
98
- assert "ciao" in decode_output (extract_serial_data (message ))
45
+ general_test_serial (socketio , "timedraw" )
99
46
100
- # test with a lot of emoji: usually they get messed up
101
- message = [] # reinitialize the message buffer
102
- socketio .emit ('command' , 'send /dev/ttyACM0 /"π§π§π§π§π§π§π§π§π§π§/"' )
103
- time .sleep (.2 )
104
- assert any ("send /dev/ttyACM0 /\" π§π§π§π§π§π§π§π§π§π§/\" " in i for i in message )
105
- # print (message)
106
- assert "/\" π§π§π§π§π§π§π§π§π§π§/\" " in decode_output (extract_serial_data (message ))
107
- socketio .emit ('command' , 'close /dev/ttyACM0' )
108
- time .sleep (.2 )
109
- # print (message)
110
- assert any ("\" IsOpen\" : false," in i for i in message )
111
47
112
48
@pytest .mark .skipif (
113
49
running_on_ci (),
114
50
reason = "VMs have no serial ports" ,
115
51
)
116
52
def test_open_serial_timedbinary (socketio ):
53
+ general_test_serial (socketio , "timedbinary" )
54
+
55
+
56
+ def general_test_serial (socketio , buffertype ):
57
+ port = "/dev/ttyACM0"
117
58
global message
118
59
message = []
60
+ #in message var we will find the "response"
119
61
socketio .on ('message' , message_handler )
120
- socketio .emit ('command' , 'open /dev/ttyACM0 9600 timedbinary' )
121
- time .sleep (.5 ) # give time to message to be filled
62
+ #open a new serial connection with the specified buffertype, if buffertype s empty it will use the default one
63
+ socketio .emit ('command' , 'open ' + port + ' 9600 ' + buffertype )
64
+ # give time to the message var to be filled
65
+ time .sleep (.5 )
66
+ print (message )
67
+ # the serial connection should be open now
122
68
assert any ("\" IsOpen\" : true" in i for i in message )
123
- socketio .emit ('command' , 'send /dev/ttyACM0 /"ciao/"' )
124
- time .sleep (.2 )
125
- assert any ("send /dev/ttyACM0 /\" ciao/\" " in i for i in message )
126
- print (message )
127
- assert "ciao" in decode_output (extract_serial_data (message ))
128
69
129
- # test with a lot of emoji: usually they get messed up
130
- message = [] # reinitialize the message buffer
131
- socketio .emit ('command' , 'send /dev/ttyACM0 /"π§π§π§π§π§π§π§π§π§π§/"' )
70
+ #test with string
71
+ # send the string "ciao" using the serial connection
72
+ socketio .emit ('command' , 'send ' + port + ' /"ciao/"' )
73
+ time .sleep (1 )
74
+ print (message )
75
+ # check if the send command has been registered
76
+ assert any ("send " + port + " /\" ciao/\" " in i for i in message )
77
+ #check if message has been sent back by the connected board
78
+ if buffertype in ("timedbinary" , "timedraw" ):
79
+ output = decode_output (extract_serial_data (message ))
80
+ elif buffertype in ("default" , "timed" ):
81
+ output = extract_serial_data (message )
82
+ assert "ciao" in output
83
+
84
+ #test with emoji
85
+ message = [] # reinitialize the message buffer to have a clean situation
86
+ # send a lot of emoji: they can be messed up
87
+ socketio .emit ('command' , 'send ' + port + ' /"π§π§π§π§π§π§π§π§π§π§/"' )
132
88
time .sleep (.2 )
133
- assert any ("send /dev/ttyACM0 /\" π§π§π§π§π§π§π§π§π§π§/\" " in i for i in message )
134
- assert "/\" π§π§π§π§π§π§π§π§π§π§/\" " in decode_output (extract_serial_data (message ))
135
- socketio .emit ('command' , 'close /dev/ttyACM0' )
89
+ print (message )
90
+ # check if the send command has been registered
91
+ assert any ("send " + port + " /\" π§π§π§π§π§π§π§π§π§π§/\" " in i for i in message )
92
+ if buffertype in ("timedbinary" , "timedraw" ):
93
+ output = decode_output (extract_serial_data (message ))
94
+ elif buffertype in ("default" , "timed" ):
95
+ output = extract_serial_data (message )
96
+ assert "/\" π§π§π§π§π§π§π§π§π§π§/\" " in output
97
+
98
+ #finally close the serial port
99
+ socketio .emit ('command' , 'close ' + port )
136
100
time .sleep (.2 )
137
- # print (message)
101
+ print (message )
102
+ #check if port has been closed
138
103
assert any ("\" IsOpen\" : false," in i for i in message )
139
104
140
105
0 commit comments