Hello everyone, I'm using PI4J at my projects for sometime now and the serial listener always made it.
But now I have to use Serial.read(), and when I do it the program get stuck.
When I write to the serialcomm, the device send me an ACK. After the device finishes the process it sends the status, but the listener don't show it. Only shows after I user the serial.read(), but then it stucks
Using it inside a thread solves the problem of been stuck, the first time it's ok. The problem is when I start it again, the serial read get all confused and sometimes doesn't show anything.
Can anybody help me?
public void DEP_COMP_PI4J() throws IllegalStateException, IOException{
new Thread() {
@Override
public void run() {
final Serial serial = SerialFactory.createInstance();
serial.addListener(new SerialDataEventListener() {
@Override
public void dataReceived(SerialDataEvent event) {
try {
System.out.println("[HEX DATA] " + event.getHexByteString());
Recebido +=","+event.getHexByteString();
} catch (IOException e) {
e.printStackTrace();
}
}
});
try{
System.out.print("create buffer\n");
byte[] buffAbre = new byte[4];
buffAbre[0] = 0x02;
buffAbre[1] = 0x63;
buffAbre[2] = 0x03;
buffAbre[3] = 0x62;
serial.open("/dev/serial0", 9600,8,1,1,0);
System.out.println("serial open ?"+ serial.isOpen()+"\n");
System.out.println("will send buffer\n");
serial.write(buffAbre);
System.out.println("sent buffer:\n ");
Thread.sleep(7000); //Time to the device finish the process
System.out.println("Sleep:\n ");
serial.read();
}
catch(Exception Ex){
System.out.println(Ex);
System.out.println("Remove Listener");
serial.removeListener();
}
System.out.println(Ex);
}
}.start();
}