I have two issues with a serial program on mobile device:
well any suggestion? code snippets maybe?
You can add a Listener to a SerialPort that gets notified whenever data is available on that port. Inside this callback, in.available()
should also return the number of bytes that can be read, but it is probably better to just consume bytes until read
return -1.
final CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM1");
final SerialPort com1 = (SerialPort)portId.open("Test", 1000);
final InputStream in = com1.getInputStream();
com1.notifyOnDataAvailable(true);
com1.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
com1.addEventListener(new SerialPortEventListener() {
public void serialEvent(SerialPortEvent event) {
if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
byte[] buffer = new byte[4096];
int len;
while (-1 != (len = in.read(buffer))) {
// do something with buffer[0..len]
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});