1 | #! /usr/bin/python |
---|
2 | # |
---|
3 | # itccardtest.py |
---|
4 | # (c) 2007 George Farris <farrisg@shaw.ca> |
---|
5 | # |
---|
6 | # This program is free software; you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation; either version 2 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # This program is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with this program; if not, write to the Free Software |
---|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
19 | # |
---|
20 | |
---|
21 | |
---|
22 | # This documents and provides a demo of the protocol used to maniplulate |
---|
23 | # the ITC Print 2015 smart card reader, http://www.itcsystems.com/smart-card-stored.html |
---|
24 | # The 2015 is connected to the serial port of the PC to charge for things like |
---|
25 | # computer time usage, pay-for-print, cash registers sales, etc. |
---|
26 | # |
---|
27 | # The following description assumes that "Host" is the PC and Reader is the 2015 |
---|
28 | # |
---|
29 | # ----------------------------------------------------------------------------------------------------- |
---|
30 | # Poll card reader for indication of card insertion |
---|
31 | # ----------------------------------------------------------------------------------------------------- |
---|
32 | # Transmit from Host |
---|
33 | # Char line : <STX><NUL><SOH><SOH><ETX><NUL><BEL><EOT> |
---|
34 | # Hex translation: 0x02 0x00 0x01 0x01 0x03 0x00 0x07 0x04 |
---|
35 | # Receive from Reader |
---|
36 | # No card inserted |
---|
37 | # Char line : <STX><NUL><SOH>@<ETX><NUL>F<EOT> |
---|
38 | # Hex translation: 0x02 0x00 0x01 0x40 0x03 0x00 0x46 0x04 |
---|
39 | # Card Inserted |
---|
40 | # Char line : <STX><NUL><SOH>@<ETX><NUL>F<EOT> |
---|
41 | # Hex translation: 0x02 0x00 0x01 0x40 0x03 0x00 0x46 0x04 |
---|
42 | # ===================================================================================================== |
---|
43 | |
---|
44 | |
---|
45 | # ----------------------------------------------------------------------------------------------------- |
---|
46 | # Request current dollar(1) value stored on card |
---|
47 | # ----------------------------------------------------------------------------------------------------- |
---|
48 | # Transmit from Host |
---|
49 | # Char line : <STX><NUL><SOH>!<ETX><NUL>'<EOT> |
---|
50 | # Hex translation: 0x02 0x00 0x01 0x21 0x03 0x00 0x27 0x04 |
---|
51 | # Receive from Reader |
---|
52 | # Char line : <STX><NUL><SOH><NUL><NUL><NUL><NUL><NUL><NUL><DLE>h<SOH><ETX><NUL><DEL><EOT> |
---|
53 | # Hex translation: 0x02 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x10 0x68 0x01 0x03 0x00 0x7F 0x04 |
---|
54 | # [ Dollar value in this case 0x10 0x68 ] |
---|
55 | # [ 0x1068 = 4200 = $4.20 ] |
---|
56 | # [______________________________________] |
---|
57 | # ===================================================================================================== |
---|
58 | |
---|
59 | |
---|
60 | # ----------------------------------------------------------------------------------------------------- |
---|
61 | # Update Reader with new dollar value - must be less than what is stored on card |
---|
62 | # ----------------------------------------------------------------------------------------------------- |
---|
63 | # Transmit from Host |
---|
64 | # Char line : <STX><NUL><SOH>$<NUL><NUL><NUL><NUL><NUL><NUL><DLE><EOT><SOH><ETX><NUL>?<FF> |
---|
65 | # Hex translation: 0x02 0x00 0x01 0x24 0x00 0x00 0x00 0x00 0x00 0x00 0x10 0x04 0x01 0x03 0x00 0x3F 0x0C |
---|
66 | # [ Dollar value in this case 0x10 0x04 ] |
---|
67 | # [ 0x1004 = 4100 = $4.10 ] |
---|
68 | # [______________________________________] |
---|
69 | # |
---|
70 | # Receive from successful transaction from Reader |
---|
71 | # Char line : <STX><NUL><SOH><SYN><ETX><NUL><FS><BS> |
---|
72 | # Hex translation: 0x02 0x00 0x01 0x16 0x03 0x00 0x1C 0x08 |
---|
73 | # ===================================================================================================== |
---|
74 | |
---|
75 | |
---|
76 | # ----------------------------------------------------------------------------------------------------- |
---|
77 | # Eject card from Reader |
---|
78 | # ----------------------------------------------------------------------------------------------------- |
---|
79 | # Transmit from Host |
---|
80 | # Char line : <STX><NUL><SOH><SPACE><ETX><NUL>&<EOT> |
---|
81 | # Hex translation: 0x02 0x00 0x01 0x20 0x03 0x00 0x26 0x04 |
---|
82 | # Receive from Reader |
---|
83 | # Char line : <STX><NUL><SOH><SYN><ETX><NUL><FS><BS> |
---|
84 | # Hex translation: 0x02 0x00 0x01 0x16 0x03 0x00 0x1C 0x08 |
---|
85 | # ===================================================================================================== |
---|
86 | |
---|
87 | # (1) Currency can be set from the card reader keypad |
---|
88 | |
---|
89 | import sys, os, serial, string, binascii, time, tty, termios |
---|
90 | |
---|
91 | ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) |
---|
92 | #x = ser.read() #read one byte |
---|
93 | |
---|
94 | print "Main loop..." |
---|
95 | print " Waiting for card to be inserted..." |
---|
96 | ser.write(binascii.a2b_hex("0200010103000704")) |
---|
97 | s = ser.read(15) |
---|
98 | |
---|
99 | |
---|
100 | while binascii.b2a_hex(s) == "0200014003004604": |
---|
101 | time.sleep(2) |
---|
102 | #print binascii.b2a_hex(s) |
---|
103 | ser.write(binascii.a2b_hex("0200010103000704")) |
---|
104 | #print "Tx -> 0200010103000704" |
---|
105 | s = ser.read(15) |
---|
106 | #print "Rx -> %s" % binascii.b2a_hex(s) |
---|
107 | |
---|
108 | if binascii.b2a_hex(s) == "0200016c0300721c": |
---|
109 | print "" |
---|
110 | print " Card is inserted..." |
---|
111 | else: |
---|
112 | print " Card Error..." |
---|
113 | exit(0) |
---|
114 | |
---|
115 | # Get current value from card |
---|
116 | ser.write(binascii.a2b_hex("0200012103002704")) |
---|
117 | s1 = ser.read(20) |
---|
118 | |
---|
119 | #020001000000000000 125c 010300 7538 = $4.700 |
---|
120 | # ^^^^ 4700 in decimal |
---|
121 | #020001000000000000 0000 010300 0704 = $0.000 |
---|
122 | #020001000000000000 1068 010300 7F04 = $4.200 |
---|
123 | #020001000000000000 1004 010300 7F04 = $4.100 |
---|
124 | #print binascii.b2a_hex(s1[3:11]) |
---|
125 | print " %s%.2f" % ("Card valued at -> $",float(string.atoi(binascii.b2a_hex(s1[3:11]), 16))/1000) |
---|
126 | |
---|
127 | print " Press 'e' to eject the card..." |
---|
128 | # Need comms to contiune to keep card in machine. |
---|
129 | # This loop keeps the card in until it stops so basically the print |
---|
130 | # job can release the card after it is finished |
---|
131 | # The next three lines set the terminal up so one can poll the keyboard |
---|
132 | fd = sys.stdin.fileno() |
---|
133 | old_settings = termios.tcgetattr(fd) |
---|
134 | tty.setraw(sys.stdin.fileno()) |
---|
135 | |
---|
136 | while binascii.b2a_hex(s) == "0200016c0300721c": |
---|
137 | if sys.stdin.read(1) == 'e': |
---|
138 | break |
---|
139 | time.sleep(1) |
---|
140 | #print binascii.b2a_hex(s) |
---|
141 | ser.write(binascii.a2b_hex("0200010103000704")) |
---|
142 | #print "Tx -> 0200010103000704" |
---|
143 | s = ser.read(15) |
---|
144 | #print "Rx -> %s" % binascii.b2a_hex(s) |
---|
145 | # Reset the terminal after |
---|
146 | termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) |
---|
147 | |
---|
148 | # Eject card |
---|
149 | ser.write(binascii.a2b_hex("0200012003002604")) |
---|
150 | s2 = ser.read(20) |
---|
151 | #print "Rx -> %s" % binascii.b2a_hex(s2) |
---|
152 | print " Ejecting card ..." |
---|
153 | |
---|
154 | # okay so we have a card with $4.70 on it, decrement it to $4.10 |
---|
155 | #ser.write(binascii.a2b_hex("0200012400000000000010040103003F0C")) |
---|
156 | #s2 = ser.read(20) |
---|
157 | #print binascii.b2a_hex(s2) |
---|
158 | ser.close() |
---|
159 | print "Goodbye..." |
---|