grazie della risposta vado al sodo se aggiungo a SimpleWrite nell'ultimo thread dopo la riga
outputStream.write(messageString.getBytes()); la riga:
SimpleRead reader =new SimpleRead(); che richiama questa classe:
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public SimpleRead() {
try {
inputStream = serialPort.getInputStream();
}
catch (NullPointerException e) {}
catch (IOException e) {}
try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
}
catch (InterruptedException 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) {}
break;
}
}
}
scusa la lunghezza ma per chiarezza eclipse lanciando SimpleWrite mi dà questo errore:
java.lang.NullPointerException
at SimpleRead.<init>(SimpleRead.java:66)
at SimpleWrite.main(SimpleWrite.java:69)
Exception in thread "main" perchè? una volta inviato il comando non faccio altro che leggere o m sbaglio? Se puoi darmi consigli su come correggere il codice o delle dritte te ne sarei grato ciao a presto