2727import threading
2828
2929
30+
3031# Dummy functions for callbacks
3132def dummy_func ():
33+
3234 return True
3335 pass
3436
@@ -80,21 +82,35 @@ def test(self):
8082 def Start (self ):
8183 try :
8284 while (1 ):
85+
8386 '''Schedule periodic tasks in loop_callback'''
8487 self .loop_callback ()
88+ if self .close == True :
89+ self .Stop ()
90+ exit
8591 pass
8692
8793 except :
8894 self .interrupt_callback ()
8995 pass
9096
9197 finally :
92- self .close = True
98+ self .Stop ()
9399 pass
100+
101+ def Stop (self ):
102+ self .close = True
103+ self .ports .clear ()
104+ for i_serial in self .ser :
105+ i_serial .close ()
106+ for i_serial in self .pause_ser :
107+ i_serial .close ()
108+
109+
94110
95111# Function to ignore future communication with port until the port is disconnected
96112 def ignore_port (self ,i_serial ):
97- #self.ser[index] .close()
113+ #i_serial .close()
98114 if i_serial in self .ser :
99115 self .ser .remove (i_serial )
100116 pass
@@ -113,14 +129,14 @@ def resume_port(self,i_serial):
113129
114130
115131# Constructor
116- def __init__ (self ):
132+ def __init__ (self ):
117133 self .scan_ports ()
118134 pass
119-
135+
120136
121137# Monitor port for incoming data
122138 def read_sink (self ,i_serial ,i_port ):
123-
139+ self . scan_ports ()
124140 if self .close :
125141 return
126142
@@ -150,6 +166,7 @@ def read_sink(self,i_serial,i_port):
150166 #Callback
151167 self .port_disconnection_callback (i_port )
152168 #Callback
169+ i_serial .close ()
153170 if i_serial in self .ser :
154171 self .ser .remove (i_serial )
155172 self .ports .discard (i_port )
@@ -161,7 +178,7 @@ def scan_ports(self):
161178 if self .close :
162179 return
163180 #print("scan_ports")
164- for i in range (self .portno_range ):
181+ for i in range (self .portno_range + 1 ):
165182 # Windows Ports
166183 t2 = threading .Thread (target = self .port_connect ,args = ([i ,"COM" ]))
167184 t2 .daemon = True
@@ -193,7 +210,6 @@ def port_connect(self,i,prefix):
193210
194211 self .ports .add (portno )
195212
196- #if self.ser in self.ser:
197213 t3 = threading .Thread (target = self .read_sink ,args = ([self .ser [temp_index ],portno ]))
198214 t3 .daemon = True
199215 t3 .start ()
0 commit comments