J'utilise dans un programme java l'API javax.comm pour lire des infos venant d'un recepteur gps USB (détecté sur un port série virtuel).
Etant donné que le numéro du port COM attribué au recepteur GPS peut changer et que l'utilisateur du programme ne doit pas s'occuper de ça, je scanne tous les ports série et test si il y a des datas format NMEA (trames du recepteur GPS) qui arrivent du port COM en question. (Je n'ai pas trouver mieux!)
Donc j'essaye de faire une lecture du port série sur évènements comme dans ce tutoriel :
http://christophej.developpez.com/tutoriel/java/javacomm/#L2.3.3
J'ai juste un petit souci maintenant : j'arrive bien a trouver mon recepteur GPS mais mon prog plante une fois les trames lues! Je crois que le prog reste bloqué dans la structure switch qui gère les évènements du port série!
import javax.comm.*;
import java.util.*;
import java.math.*;
import java.net.*;
import com.sun.comm.Win32Driver;
import java.io.*;
public class testCOM implements SerialPortEventListener {
private String portCOM;
private CommPortIdentifier portID = null; //identifiant du port
private SerialPort serialPort; //le port série
private BufferedReader fluxLecture; //flux de lecture du port
/*
* Methode qui initialise le port série en evenementiel
*/
public void ModeEvenement(String portCOM) {
//récupération de l'identifiant du port
try {
portID = CommPortIdentifier.getPortIdentifier(portCOM);
} 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(
4800,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
System.out.println("port ouvert, attente de lecture");
}
/*
* Methode qui effectue la lecture de 7 trames sur le port série
* Une fois la lecture faite on ferme le flux de lecture et le port COM
*/
public void ReadSerialPort(){
int i=7;
String reponse = new String();
try {
System.out.println("i="+i);
while(i!=0){
System.out.println("On lit sur le port COM\n");
reponse = (String) fluxLecture.readLine();
System.out.println(reponse);
i--;
System.out.println("i="+i);
}
} catch (IOException e) {}
//fermeture du flux de lecture
try {
fluxLecture.close();
} catch (IOException e) {}
//fermeture du port COM
serialPort.close();
}
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.DATA_AVAILABLE :
this.ReadSerialPort();//si data dispo on lance la lecture
break;
default:
break;//on ne fait rien pour les autres evenements
}
}
/*
* Methode qui scanne tous les ports COM et test si des données viennent du port COm scanné
*/
public void listPort(){
Enumeration listePorts = CommPortIdentifier.getPortIdentifiers();
int typePort;
String GPSPortCOM;
while (listePorts.hasMoreElements()){
portID = (CommPortIdentifier) (CommPortIdentifier) listePorts.nextElement();
if(portID.getPortType()==CommPortIdentifier.PORT_SERIAL){
System.out.println("Nom du PORT :"+portID.getName());
System.out.println("User :"+portID.getCurrentOwner());
System.out.println("Use ? :"+portID.isCurrentlyOwned());
System.out.println("Type du PORT :"+portID.getPortType());
// On lance la gestion des évènements sur portID
this.ModeEvenement(portID.getName());
}
}
}
public static void main(String[] args) {
//initialisation du driver
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
testCOM test = new testCOM();
test.listPort();
}
}
Je ne sais pas si j'ai été assez clair! Mais si quelqu'un a une idée je suis preneur!!!
Merci d'avance.

lorsque j'envois ces commandes rien ne se passe (pas de résultat)
mais lorsque je ferme ou je j'ouvre le clavier des caractéres bizaares et des carreaux s'affiches et mot locked ou un locked détirées. je croix que mon portable ne recoit pas ces commandes .
si vous avez recontre ce prob contacte moi sur msn sabron01@hotmail.com j e vraiment besoin de ton aide,je suis mnt de réaliser ce truc com mon pfe et il y on ad'autres proble......
voici ce code et j'attends vos idées sur mon hotmail :
/*Most projects that deal with hardware and devices, needs to communicate with them using the COM port of the PC or Server. For example if there is a modem that is connected to a server via its COM port and the Java program has to read the output of the modem then the Java program has to read the COM port for any incoming data.
This sample Java program can be used to Read from a COM port for incoming data and process it. Note that you will need to change the Port number to COM1 or COM2 or any other ports as required.
Also if you are using unix based machines then you will have to uncomment the /dev/term/a instead of COM.*/
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
import javax.comm.*;
import com.sun.comm.Win32Driver;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
static InputStream inputStream;
static SerialPort serialPort;
static Thread readThread;
static OutputStream outputStream;
static SerialPortEvent event;
private static BufferedReader in;
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("Envoi", 20000);
}
catch (PortInUseException e) {System.out.println(e);}
/* try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {System.out.println(e);}*/
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
public static void EcritureSurRS232(String data ) {
try
{
outputStream = serialPort.getOutputStream();
outputStream.write(data.getBytes());
}
catch (IOException e)
{
System.out.println("erreur dans l'écriture sur le port série\n" +
e.getMessage());
}
}
public static void lecturePortRS232() throws IOException{
//byte[] readBuffer = new byte[2];
char[] charbuffer=new char[100];
//inputStream= serialPort.getInputStream();
try {
//while (inputStream.available() > 0) {
//int numBytes = inputStream.read(readBuffer);
//}
//System.out.print(new String(readBuffer));
in=new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
String codeBarre = new String();
try {
//lecture du buffer et affichage
codeBarre = (String) in.readLine();
System.out.println(codeBarre);
} catch (IOException e) {}
//in.close();
}
catch (IOException ex)
{
System.out.println("erreur dans la lecture du port\n"+ex.getMessage());
}
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
public void serialEvent(SerialPortEvent event) {
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:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
public static void main(String[] args) throws IOException {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM4"))
{
SimpleRead reader = new SimpleRead();
System.out.println("saisir la commande :") ;
BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
String data=stdin.readLine();
EcritureSurRS232(data);
lecturePortRS232();
}
}
}
}
}
lorsque j'envois ces commandes rien ne se passe (pas de résultat)
mais lorsque je ferme ou je j'ouvre le clavier des caractéres bizaares et des carreaux s'affiches et mot locked ou un locked détirées. je croix que mon portable ne recoit pas ces commandes .
si vous avez recontre ce prob contacte moi sur msn sabron01@hotmail.com j e vraiment besoin de ton aide,je suis mnt de réaliser ce truc com mon pfe et il y on ad'autres proble......
voici ce code et j'attends vos idées sur mon hotmail :
/*Most projects that deal with hardware and devices, needs to communicate with them using the COM port of the PC or Server. For example if there is a modem that is connected to a server via its COM port and the Java program has to read the output of the modem then the Java program has to read the COM port for any incoming data.
This sample Java program can be used to Read from a COM port for incoming data and process it. Note that you will need to change the Port number to COM1 or COM2 or any other ports as required.
Also if you are using unix based machines then you will have to uncomment the /dev/term/a instead of COM.*/
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
import javax.comm.*;
import com.sun.comm.Win32Driver;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
static InputStream inputStream;
static SerialPort serialPort;
static Thread readThread;
static OutputStream outputStream;
static SerialPortEvent event;
private static BufferedReader in;
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("Envoi", 20000);
}
catch (PortInUseException e) {System.out.println(e);}
/* try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {System.out.println(e);}*/
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
public static void EcritureSurRS232(String data ) {
try
{
outputStream = serialPort.getOutputStream();
outputStream.write(data.getBytes());
}
catch (IOException e)
{
System.out.println("erreur dans l'écriture sur le port série\n" +
e.getMessage());
}
}
public static void lecturePortRS232() throws IOException{
//byte[] readBuffer = new byte[2];
char[] charbuffer=new char[100];
//inputStream= serialPort.getInputStream();
try {
//while (inputStream.available() > 0) {
//int numBytes = inputStream.read(readBuffer);
//}
//System.out.print(new String(readBuffer));
in=new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
String codeBarre = new String();
try {
//lecture du buffer et affichage
codeBarre = (String) in.readLine();
System.out.println(codeBarre);
} catch (IOException e) {}
//in.close();
}
catch (IOException ex)
{
System.out.println("erreur dans la lecture du port\n"+ex.getMessage());
}
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
public void serialEvent(SerialPortEvent event) {
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:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
public static void main(String[] args) throws IOException {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM4"))
{
SimpleRead reader = new SimpleRead();
System.out.println("saisir la commande :") ;
BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
String data=stdin.readLine();
EcritureSurRS232(data);
lecturePortRS232();
}
}
}
}
}