1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| package com.atosworldline.rd.wodapa;
import javax.comm.*;
import com.sun.comm.Win32Driver;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.*;
public class ModeEvenement extends Thread implements SerialPortEventListener {
private CommPortIdentifier portId;
private static SerialPort serialPort;
private BufferedReader fluxLecture;
private boolean running;
static char[] sCREnable={0x3E, 0x33, 0x00, 0x00, 0x04, 0x01, 0x00, 0x05, 0x00, 0x0D};
static char[] checkForCard={0x3E, 0x33, 0x00, 0x00, 0x02, 0x04, 0x00, 0x0B};
static char[] powerOn={0x3E, 0x33, 0x00, 0x00, 0x02, 0x02, 0x00, 0x0D};
static char[] apduGetChallenge={0x00,0x84,0x00,0x00,0x08};
// static byte[] exchangeAPDU[]={0x3E, 0x33, 0x00, 0x00, 0x09, 0x05, 0x00, 0x00,0x84,0x00,0x00,0x08, 0x8D}; // 4 byte APDU
// static byte[] exchangeAPDU[]={0x3E, 0x33, 0x00, 0x00, 0x0F, 0x05, 0x00, 0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x00, 0x03}; // 13 byte APDU
static char[] exchangeAPDU={0x3E, 0x33, 0x00, 0x00, 0x16, 0x05, 0x00, 0x00, 0xA4, 0x04, 0x00, 0x07, 0x31, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31, 0x00, 0xDE}; // 20 byte APDU
static char[] powerOff={0x3E, 0x33, 0x00, 0x00, 0x02, 0x03, 0x00, 0x0C};
static char[] disableSCR={0x3E,0x33,0x00,0x00,0x02,0x06,0x00,0x09};
/**
* Constructeur qui récupère l'identifiant du port et lance l'ouverture.
*/
public ModeEvenement(String port) {
//initialisation du driver
System.out.println("initialisation du driver");
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
//récupération de l'identifiant du port
try {
System.out.println("récupération de l'identifiant du port");
portId = CommPortIdentifier.getPortIdentifier(port);
} catch (NoSuchPortException e) {
}
//ouverture du port
try {
System.out.println("ouverture du port");
serialPort = (SerialPort) portId.open("ModeEvenement", 2000);
} catch (PortInUseException e) {
}
//récupération du flux
try {
System.out.println("récupération du flux");
fluxLecture =
new BufferedReader(
new InputStreamReader(serialPort.getInputStream()));
} catch (IOException e) {
}
//ajout du listener
try {
System.out.println("ajout du listener");
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}
//paramétrage du port
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setRTS(false);
serialPort.setDTR(false);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
System.out.println("port ouvert, attente de lecture");
}
public void run() {
running = true;
while (running) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
//fermeture du flux et port
try {
fluxLecture.close();
} catch (IOException e) {
}
serialPort.close();
}
/**
* Méthode de gestion des événements.
*/
public void serialEvent(SerialPortEvent event) {
//gestion des événements sur le port :
//on ne fait rien sauf quand les données sont disponibles
switch (event.getEventType()) {
case SerialPortEvent.BI :
System.out.println("Break interrupt.");
break;
case SerialPortEvent.OE :
System.out.println(" Overrun error.");
break;
case SerialPortEvent.FE :
System.out.println("Framing error.");
break;
case SerialPortEvent.PE :
System.out.println("Parity error.");
break;
case SerialPortEvent.CD :
System.out.println("Carrier detect.");
break;
case SerialPortEvent.CTS :
System.out.println("Clear to send.");
break;
case SerialPortEvent.DSR :
System.out.println("Data set ready.");
break;
case SerialPortEvent.RI :
System.out.println("Ring indicator.");
break;
case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
System.out.println("Output buffer is empty.");
break;
case SerialPortEvent.DATA_AVAILABLE :
String retour = new String();
try {
//lecture du buffer et affichage
retour = (String) fluxLecture.readLine();
System.out.println(retour);
} catch (IOException e) {
}
break;
}
}
// table to convert a nibble to a hex char.
private static char[] hexChar = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'};
public static String hex2String(byte[] b) {
StringBuffer sb = new StringBuffer(b.length * 2);
for (int i = 0; i < b.length; i++) {
// look up high nibble char
sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
// look up low nibble char
sb.append(hexChar[b[i] & 0x0f]);
}
return sb.toString().toUpperCase();
}
public static String hex2String(byte b) {
StringBuffer sb = new StringBuffer(2);
// look up high nibble char
sb.append(hexChar[(b & 0xf0) >>> 4]);
// look up low nibble char
sb.append(hexChar[b & 0x0f]);
return sb.toString().toUpperCase();
}
public static byte[] toByteArray(char[] array) {
return toByteArray(array, Charset.defaultCharset());
}
public static byte[] toByteArray(char[] array, Charset charset) {
CharBuffer cbuf = CharBuffer.wrap(array);
ByteBuffer bbuf = charset.encode(cbuf);
return bbuf.array();
}
/**
* Permet l'arrêt du thread
*/
public void stopThread() {
running = false;
}
/**
* Méthode principale de l'exemple.
*/
public static void main(String[] args) {
//Récuperation du port en argument
String port = "COM1";
//lancement de l'appli
ModeEvenement modeEve=new ModeEvenement(port);
modeEve.start();
SimpleWrite beep=new SimpleWrite();
beep.ecrit(serialPort);
//"interface utilisateur"
// System.out.println("taper q pour quitter");
// //construction flux lecture
// BufferedReader clavier =
// new BufferedReader(new InputStreamReader(System.in));
// //lecture sur le flux entrée.
// try {
// String lu = clavier.readLine();
// while (!lu.equals("q")) {
// }
// } catch (IOException e) {
// }
modeEve.stopThread();
}
} |
Partager