package ClassUti; import java.io.*; import java.util.*; import javax.comm.*; public class SimpleWrite { public SimpleWrite(){} Enumeration portList; CommPortIdentifier portId; SerialPort serialPort; OutputStream outputStream; boolean outputBufferEmptyFlag = false; private String messageString; private String Port; public String getMessageString() { return messageString; } public void setMessageString(String messageString) { this.messageString = messageString; } public String getPort() { return Port; } public void setPort(String Port) { this.Port = Port; } public void Envoyer() { boolean portFound = false; int i = 0; String defaultPort = getPort(); //= "/dev/term/a"; portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if(i==0){ if (portId.getName().equals(defaultPort)) { //System.out.println("Found port " + defaultPort); portFound = true; try { serialPort = (SerialPort) portId.open("SimpleWrite", 2000); } catch (PortInUseException e) { //System.out.println("Port in use."); continue; } try { outputStream = serialPort.getOutputStream(); } catch (IOException e) {} try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {} try { serialPort.notifyOnOutputEmpty(true); } catch (Exception e) { System.out.println("Error setting event notification"); System.out.println(e.toString()); //System.exit(-1); } //System.out.println("Writing \""+getMessageString()+"\" to "+serialPort.getName()); try { outputStream.write(getMessageString().getBytes()); } catch (IOException e) {} try { Thread.sleep(2000); } catch (Exception e) {} serialPort.close(); //System.exit(1); i = 1; } } // fin boucle i cette condition c'est pour ne pa envoyer 2 foie le message } } if (!portFound) { System.out.println("port " + defaultPort + " not found."); } } }