Skip to content

Commit 5099641

Browse files
committed
More detail in README.md; better, but not great, handling of when the device goes away and the serial.read() call hangs
1 parent f60356f commit 5099641

File tree

4 files changed

+85
-32
lines changed

4 files changed

+85
-32
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ wheels/
2424
*.egg-info/
2525
.installed.cfg
2626
*.egg
27+
*.exe
28+
*.zip
2729

2830
# PyInstaller
2931
# Usually these files are written by a python script from a template

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# SerialToKbd
22

33
SerialToKbd is a simple helper program that allows MagTek USB check/credit card
4-
readers 22523003 and 22533007 to be used with software that doesn't specifically support check readers.
4+
readers 22533003 and 22533007 to be used with software that doesn't specifically support check readers.
55
It is intended as a replacement for the MicrSend program that MagTek used to provide but which no
66
longer works with recent versions of Windows.
77

88
You do NOT need this program if:
9-
* Your app supports the check reader directly (for example PowerChurch running locally)
10-
* You have a USB MiniMicr that does keyboard emulation (part number 22523009 and similar)
11-
* You have a keyboard "wedge" MiniMicr that plugs into the PS/2 keyboard port of your computer
9+
* Your app supports the check reader directly (for example PowerChurch versions 10, 11, and 11.5 running locally)
10+
* You have a USB MiniMicr that does keyboard emulation (part number 22523009 or 22523012 and similar)
11+
* You have a keyboard "wedge" MiniMicr (22520001) that plugs into the PS/2 keyboard port of your computer
1212

1313
This program MAY help if you have non-keyboard-emulation USB MiniMicr or a MiniMicr that plugs into
1414
the serial port on your computer AND your app doesn't support the MiniMicr directly (for example
15-
PowerChurch online) (tested with 22533007 readers).
15+
PowerChurch Online and PowerChurch versions 8.5 and 9 running locally) (tested with 22533003 and 22533007
16+
readers and PowerChurch Online).
1617

1718
Before using SerialToKbd you should install the MagTek drivers for your device.
1819

@@ -28,7 +29,7 @@ The USB Mini Micr should be listed as such. A MiniMicr attached with a serial ca
2829
try launching SerialToKbd from a command prompt and passing the COM port name (e.g. COM1) as a parameter:
2930
SerialToKbd COM1
3031

31-
You can test whether the MiniMicr, SerialToKbd, and other apps on your computer are working by
32+
You can test whether the MiniMicr and SerialToKbd are working by
3233
opening NotePad or WordPad and placing the cursor in it. Then scan a check: you should see the check
3334
information appear as characters in the document.
3435

@@ -48,13 +49,25 @@ License: see 'License.txt'
4849
Source code for the program may be found at the project github page:
4950
http://github.com/chauser/SerialToKbd
5051

51-
## Running and Building
52+
## Download
53+
Select the 'Releases' tab above and download the zip file for the latest release. Extract the contained files,
54+
then double-click 'SerialToKbd' to run the program. If you need to run from a command prompt in order to
55+
supply a 'COMn' parameter, open a command prompt, change directory to the folder where you extracted the files,
56+
and type
57+
SerialToKbd COMn
58+
where 'n' is the number of the COM port you want to monitor.
5259

53-
### Run directly from source:
60+
## Running from source and Building
61+
62+
### Run directly from source (assumes you have python installed):
5463
python SerialToKbd.py
64+
or
65+
python SerialToKbd.py COMn
5566

56-
### Package as .exe
67+
### Package as .exe (assumes you have pyinstaller installed)
5768
pyinstaller -F SerialToKbd.py
5869

5970
The created .exe is 'dist/SerialToKbd.exe'
71+
72+
6073

SerialToKbd.py

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,19 @@ def get_option_info(port):
168168
}
169169

170170
import 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+
197218
import serial.tools.list_ports as LP
198219
import 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

makeRelease.bat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pyinstaller -F SerialToKbd.py
2+
copy /Y dist\SerialToKbd.exe .
3+
del /Q SerialToKbd.zip
4+
zip SerialToKbd.zip SerialToKeybd.py keyboardlayout.py LICENSE README.md SerialToKbd.exe
5+
powershell Compress-Archive -Path SerialToKbd.py,keyboardlayout.py,LICENSE,README.md,SerialToKbd.exe,makeRelease.bat -DestinationPath SerialToKbd.zip
6+

0 commit comments

Comments
 (0)