import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.Enumeration; import java.io.InputStream; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import gnu.io.UnsupportedCommOperationException; public class Test1 { CommPortIdentifier portId; SerialPort serialPort; public Test1(String com) { System.out.println("listage des ports série disponibles:"); listePortsDispo(); try { portId=CommPortIdentifier.getPortIdentifier(com); serialPort=(SerialPort)portId.open("Envoi",2000); serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); System.out.println("Ouverture du port "+com); try { //Trame trame =new Trame(); //String tr=trame.getTrame(); serialPort.enableReceiveTimeout(1000); serialPort.enableReceiveThreshold(0); OutputStream out=serialPort.getOutputStream(); out.write(0x05); BufferedReader in=new BufferedReader (new InputStreamReader(serialPort.getInputStream())); String line=in.readLine(); while (line!=null){ try{ System.out.println(line); line = in.readLine(); }catch(IOException e){ System.out.println("No more characters received"); break; } //Print the line read if (line.length() != 0) System.out.println(line); /*InputStream in =serialPort.getInputStream(); int x=in.read(); System.out.println(x); //x=in.read();**/ }} catch (UnsupportedCommOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } //OutputStream out = (OutputStream) serialPort.getOutputStream(); //out.write(0x05); } //} /*BufferedReader in=new BufferedReader(new InputStreamReader(serialPort.getInputStream())); String line; while (true){ try{ line = in.readLine(); System.out.println(line); }catch(IOException e){ System.out.println("No more characters received"); break; } //Print the line read if (line.length() != 0) System.out.println(line); }**/ /* byte ENQ = 5; byte ACK = 6; byte NAK = 21; byte EOT = 4; byte ETX = 3; byte ETB = 23; byte STX = 2; byte NEWLINE = 10; byte[] ACK_BUFF = { ACK }; byte[] ENQ_BUFF = { ENQ }; byte[] NAK_BUFF = { NAK }; byte[] EOT_BUFF = { EOT };**/ //byte seperator =7c; //String trame="";//Suite à votre trame //byte[] outputArray = trame.getBytes( ); //out.write ( 0x05 ); //int b = in.read(); //String reponse=in.readLine(); //System.out.println("ligne: "+b+"\n Réponse: "+reponse); catch (Exception e) { e.printStackTrace(); } serialPort.close(); }//fin constructeur public void listePortsDispo() { System.err.println("recherche..."); Enumeration 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()); } // while } //fin de la methode listePortsDispo() public static void main(String args[]) { new Test1("COM8"); }//fin du main }//fin de la classe