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
| import javax.comm.*;
import java.io.*;
import java.util.*;
public class Flux extends Thread implements SerialPortEventListener {
private CommPortIdentifier portId;
private SerialPort serialPort;
private BufferedReader fluxLecture;
private boolean running;
/**
* Constructeur qui récupère l'identifiant du port et lance l'ouverture.
*/
public Flux(String port) {
//initialisation du driver
//récupération de l'identifiant du port
try {
portId = CommPortIdentifier.getPortIdentifier(port);
} catch (NoSuchPortException e) {
}
//ouverture du port
try {
serialPort = (SerialPort) portId.open("ModeEvenement", 2000);
} catch (PortInUseException e) {
}
//récupération du flux
try {
fluxLecture =
new BufferedReader(
new InputStreamReader(serialPort.getInputStream()));
} catch (IOException e) {
}
//ajout du listener
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
}
//paramétrage du port
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(
9600,
SerialPort.DATABITS_7,
SerialPort.STOPBITS_1,
SerialPort.PARITY_EVEN);
} 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 :
case SerialPortEvent.OE :
case SerialPortEvent.FE :
case SerialPortEvent.PE :
case SerialPortEvent.CD :
case SerialPortEvent.CTS :
case SerialPortEvent.DSR :
case SerialPortEvent.RI :
case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
break;
case SerialPortEvent.DATA_AVAILABLE :
String codeBarre = new String();
try {
//lecture du buffer et affichage
codeBarre = (String) fluxLecture.readLine();
System.out.println(codeBarre);
} catch (IOException e) {
}
break;
}
}
/**
* 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 = args[0];
//lancement de l'appli
Flux modeEve=new Flux(port);
modeEve.start();
//"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