@@ -168,12 +168,19 @@ def get_option_info(port):
168168 }
169169
170170import serial
171- def start_capture (port ):
171+ def capture (port ):
172+ global keepAlive
172173 serial_params = get_option_info (port )
173174 try :
174175 serial_port = serial .Serial (** serial_params )
175- except ( serial .SerialException , OSError ) :
176+ except serial .SerialException as SE :
176177 print ('Can\' t open serial port: %s' % port )
178+ print ("Serial exception occurred." )
179+ print (SE .strerror )
180+ return
181+ except OSError as OSE :
182+ print ("OSError occurred" )
183+ print (OSE .strerror )
177184 return
178185
179186 if not serial_port .isOpen ():
@@ -186,35 +193,60 @@ def start_capture(port):
186193
187194 # Grab data from the serial port.
188195 raw = serial_port .read (1 )
196+ keepAlive = True
189197 if not raw :
190198 continue
191199
192200 # Send the character view the keyboard emulator
193201 kbd_emulator .send_string (chr (raw [0 ]))
194202 except KeyboardInterrupt :
195203 pass
204+
205+ import time
206+ def keepAliveThread ():
207+ global keepAlive
208+ keepAlive = True
209+ while keepAlive :
210+ keepAlive = False
211+ time .sleep (5 )
196212
213+ print ("\n %s stopped responding. Was the device unplugged?" % device )
214+ print ("Close and restart this program when the device is again available" )
215+ # sys.stdin.readline()
216+
217+
197218import serial .tools .list_ports as LP
198219import sys
199- device = None
200- if len (sys .argv ) > 1 :
201- device = sys .argv [1 ]
202- else :
203- print ("Looking for a Magtek reader" )
204- ports = LP .comports ()
205- for p in ports :
206- if p .manufacturer == 'MagTek' :
207- print ("Found a MagTek reader with Vendor ID: 0x%x and Product ID: 0x%x on %s" %
208- (p .vid , p .pid , p .device ))
209- device = p .device
210- if device :
211- print ("Opening reader on %s" % device )
212- print ("Type Ctrl-C or close this window to exit" )
213- start_capture (device )
214- else :
215- print ("Did not find a supported reader; Use the device manager to identify the COM port" )
216- print ("and then enter the command" )
217- print (" SerialToKbd COMn" )
218- print ("where n is the number of the COM port you identified in the device manager" )
219- sys .stdin .readline ()
220+
221+ import threading
222+ if __name__ == "__main__" :
223+ device = None
224+ if len (sys .argv ) > 1 :
225+ device = sys .argv [1 ]
226+ else :
227+ print ("Looking for a Magtek reader" )
228+ ports = LP .comports ()
229+ for p in ports :
230+ if p .manufacturer == 'MagTek' :
231+ print ("Found a MagTek reader with Vendor ID: 0x%x and Product ID: 0x%x on %s" %
232+ (p .vid , p .pid , p .device ))
233+ device = p .device
234+ try :
235+ if device :
236+ print ("Opening reader on %s" % device )
237+ print ("Type Ctrl-C or close this window to exit" )
238+ # This whole multi-threaded design is ridiculous, but required,
239+ # because when the device is unplugged, and even perhaps
240+ # when the system sleeps or hibernates, the serial.read() function
241+ # hangs, never timing out again.
242+ threading .Thread (target = keepAliveThread ,daemon = True ).start ()
243+ capture (device )
244+ else :
245+ print ("Did not find a supported reader; Use the device manager to identify the COM port" )
246+ print ("and then enter the command" )
247+ print (" SerialToKbd COMn" )
248+ print ("where n is the number of the COM port you identified in the device manager" )
249+ sys .stdin .readline ()
250+ except KeyboardInterrupt :
251+ pass
220252
0 commit comments