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
| package com.atosworldline.rd.wodapa;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
public class SendToSPort implements SerialPortEventListener, , SendToLPP {
private byte[] reponse;
private int reponceLenght;
SerialPort sPort;
public void setSPort(SerialPort port) {
sPort = port;
try {
this.sPort.addEventListener(this);
} catch (TooManyListenersException ex) {
System.err.println("[TooManyListenersException] " + ex.getMessage());
}
}
public byte[] send(char[] apdu) {
//pour lire et écrire avec des streams:
reponse = new byte[256];
reponceLenght = 0;
try {
DataOutputStream output = new DataOutputStream(sPort.getOutputStream());
//Teste de lecture/ecriture sur le port série
System.out.println("Ecriture");
byte[] apdu2 = toByteArray(apdu);
output.write(apdu2);
//A modifier peut etre ajout d'un listener
while (reponceLenght == 0) {
//la reponce n'est toujours pas arriver
try {
//j'attend donc un peut
Thread.sleep(50);
} catch (InterruptedException ex) {
System.err.println("[InterruptedException] " + ex.getMessage());
}
}
//ma reponce est arrivé
byte[] out = new byte[reponceLenght];
// on copie notre reponce dans un tableau de bonne longueur
for (int i = 0; i < reponceLenght; i++) {
out[i] = reponse[i];
}
return out;
// pas top de catch Exception il vaut mieux catch plus finement je trouve
} catch (IOException ex){
System.err.println(ex.getMessage());
}
return null;
}
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:
System.out.println("DATA_AVAILABLE.");
try {
// ici tu est sur d'avoir un message complet
DataInputStream input = new DataInputStream(sPort.getInputStream());
reponceLenght = input.read(reponse);
} catch (IOException ex) {
Logger.getLogger(SendToSPort.class.getName()).log(Level.SEVERE, null, ex);
}
break;
}
}
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();
}
} |