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 210 211 212 213 214 215 216 217 218 219 220 221 222 223
| 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 BasicReadWrite {
/*
* Cette classe permet de tester les ports séries disponibles
* en les listant puis en créant un stream d'entrée et de sortie
*
* // TODO Implémenter un listener pour enlever le sleep
*
*/
*/
/* variables locales */
static char[] byte2={0x3E,0x33,0x00,0x00,0x02,0x06,0x00,0x09};
static Enumeration portList;
static CommPortIdentifier portId; //identifiant du port
static char[] beep={0x3E,0x36, 0x00, 0x00, 0x06, 0x03, 0x00, 0x10, 0x00, 0x01, 0x32, 0x2E};
static SerialPort serialPort; //le port série
static OutputStream outputStream; //flux d'écriture du port
static InputStream inputStream;//flux de lecture du port
static boolean outputBufferEmptyFlag = false;
private SerialPort sPort;
/*
Constructeur de la classe
qui prend en paramètre le port série à utiliser.
Dans un premier temps on liste les ports disponibles sur le pc
*/
public BasicReadWrite ()
{
//initialisation du driver
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
}//fin constructeur
public static void listePortsDispo()
{
System.err.println("recherche...");
Enumeration<CommPortIdentifier> portList=CommPortIdentifier.getPortIdentifiers();
if (portList==null)
{
System.err.println("Aucun port de communication détecté");
return;
}
while (portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
System.out.println("* "+portId.getName());
}
}
public void initialisation(String port){
//récupération de l'identifiant du port
try {
portId = CommPortIdentifier.getPortIdentifier(port);
} catch (NoSuchPortException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*ouverture du port avec le nom de l'application qui demande le port
* le délai d'attente pour l'obtention du port en millisecondes
*/
try {
System.out.println("Ouverture du port "+port);
sPort = (SerialPort) portId.open("UtilisationFlux", 10000);
} catch (PortInUseException e) {
}
//règle les paramètres de la connexion
/*
* le contrôle de flux * le débit de la connexion (en Bauds) * le nombre de bits de données
* le ou les bits stop * la présence d'un bit de parité
* */
try{
System.out.println("Chargement des paramètres");
sPort.setRTS(false);
sPort.setDTR(false);
sPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
sPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
}catch (UnsupportedCommOperationException ex){
//gestion de l'exception
}
}
public int communique(char[]apdu,byte[]reponse) {
//pour lire et écrire avec des streams:
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
Thread.sleep(200);
DataInputStream input=new DataInputStream(sPort.getInputStream());
if (input.available()>0){
int readByte=input.read(reponse);
System.out.println("Lecture de "+readByte+" byte");
output.flush();
input.close();
output.close();
return readByte;
}
else {
System.out.println("Pas de lecture : "+input.available()+" données..");
output.flush();
input.close();
output.close();
return 0;
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Erreur de communication !");
return 0;
}
}
/**
* Méthode de fermeture des flux et port.
*/
public void close(){
sPort.close();
}
// 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();
}
/*
Methode main qui permet de tester notre classe de teste en ouvrant une connexion sur le port COM1.
*/
public static void main(String args[])
{
System.out.println("listage des ports série disponibles:");
listePortsDispo();
byte[] reponse = new byte[256];
byte []reponse2;
BasicReadWrite test=new BasicReadWrite ();
System.out.println("Initialisation");
test.initialisation("COM1");
int readByte = 0;
readByte=test.communique(beep, reponse);
reponse2 = new byte[readByte];
for (int i=0;i<readByte;i++){
reponse2[i]=reponse[i];
}
if (readByte!=0){
System.out.println("Reponse beep: "+ hex2String(reponse2));
}
readByte=test.communique(byte2, reponse);
reponse2 = new byte[readByte];
for (int i=0;i<readByte;i++){
reponse2[i]=reponse[i];
}
if (readByte!=0){
System.out.println("Reponse byte2: "+ hex2String(reponse2));
}
System.out.println("Fermeture");
test.close();
}//fin du main
}//fin de la classe |
Partager