Bonjour, je souhaiterai récupérer un numéro de code barre dans une variable NumCodeBarre mais je ne sais pas comment faire puisque la méthode serialEvent ne peut renvoyer qu'un void, pouvez-vous m'aider ?
Merci d'avance.
Voici mon code:
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
| package Test;
import javax.comm.*;
import com.sun.comm.Win32Driver;
import java.io.*;
import java.util.*;
public class GestionLecteur 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 GestionLecteur(String port) {
//initialisation du driver
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
//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) {
System.out.println(e.getMessage());
}
//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_8,
SerialPort.STOPBITS_1,
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 :
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 NumCodeBarre = new String();
try {
//lecture du buffer et affichage
NumCodeBarre = (String) fluxLecture.readLine();
} catch (IOException e) {
}
break;
}
}
/**
* Permet l'arrêt du thread
*/
public void stopThread() {
running = false;
}
} |
je fais appel dans mon IHM comme ceci:
new GestionLecteur("COM1");
Merci encore.
Partager