ho risolto questo codice mi funziona legge e scrive ma non si ferma cioè non termina l'esecuzione come posso elegantemente forzarlo a uscire dopo che ho ricevuto la risposta? riporto il codice nella sua interezza(così puoi farne copia&incolla):


import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.TooManyListenersException;

import javax.comm.*;
public class SimpleReadWrite implements Runnable,
SerialPortEventListener {

static String DEFAULT_PORT = "COM4";

InputStream in;
OutputStream out;
private String message = "AT\r";
public void writeToCommPort(String message) {
this.message = message;
}


/**
* Main method.
*/
public static void main(String[] args) {
String portname = DEFAULT_PORT;
if (args.length > 1) {
System.err.println("Usage: java SimpleReadWrite [port_name]");
System.exit(1);
}
else if (args.length == 1) {
portname = args[0];
}

try {
CommPortIdentifier portId =

CommPortIdentifier.getPortIdentifier(portname);
if (portId.getPortType() ==
CommPortIdentifier.PORT_SERIAL) {
SimpleReadWrite srw = new SimpleReadWrite(portId);
}
else {
System.err.println(portname + " is not a serial port");
System.exit(1);
}
}
catch (NoSuchPortException e) { //Port nicht vorhanden
System.err.println("No such port: " + portname);
System.exit(1);
}
catch (PortInUseException e) { //Port ist gesperrt/inGebrauch
System.err.println("Port in use: " + portname);
System.exit(1);
}
}

/**
* Constructor.
*/
public SimpleReadWrite(CommPortIdentifier portId) throws
PortInUseException {
try {
SerialPort serialPort = (SerialPort)
portId.open("SimpleReadWrite",
2000);
//Port mit
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(9600, //Schnittstelleneinstellungen initialisieren
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
Thread readerThread = new Thread(this);

readerThread.start();

} //Exception-Handling
catch (IOException e) {
System.err.println("Problem opening streams");
System.exit(1);
}
catch (TooManyListenersException e) {
System.err.println("Too Many Listeners");
System.exit(1);
}
catch (UnsupportedCommOperationException e) {
System.err.println("Problem setting port parameters");
System.exit(1);
}
}

/**
* This method loops forever, reading data from the
* command line and writing it directly to the serial port.
* No buffering is performed.
*/
public void run() {

try {
while (true) {

out.write(message.getBytes());

}
}
catch (IOException e) {
// Terminate thread
}

}

/**
* Process serial port events. This method is required
* when implementing SerialPortEventListener
*
* @param event SerialPortEvent object to process
*/
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:
// If there is data available on serial port,
// read it in in chunks <=20 bytes
//
byte[] readBuffer = new byte[1];
try {
while (in.available() > 0) {
int numBytes = in.read(readBuffer);

}

System.out.print(new String(readBuffer));

}
catch (IOException e) {
// Terminate handling this event on read error
}
break;
default:
System.out.print("Unknown SerialPortEvent type = " +
event.getEventType());
break;
}
}
}
Questi i threads che eclipse in fase di debug mi elenca:
Thread [Win32SerialPort Notification thread] (in esecuzione)
Thread [DestroyJavaVM] (in esecuzione)
Thread [Thread-0] (in esecuzione)
A presto e grazie!