Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.35k stars 287 forks source link

Cleaning up com port buffer #255

Closed yaroslau1 closed 5 years ago

yaroslau1 commented 5 years ago

How can I cleaning up com port buffer with JSerialComm. Every time i call serialEvent(SerialPortEvent event) I get new and old data.

This is my code example: `public void serialEvent(SerialPortEvent event) {

    if (userPort.bytesAvailable() > 0) {
        System.out.println(userPort.bytesAvailable());
        byte[] readBuffer = new byte[userPort.bytesAvailable()];
        if (userPort.bytesAvailable() == 15) {
            int numRead = userPort.readBytes(readBuffer, readBuffer.length);
            for (int i = 0; i < readBuffer.length; i++) {
                bytesRiesived.add(readBuffer[i]);
            }
            System.out.println(bytesRiesived);
            control.addToList(bytesRiesived);
            System.out.println("end");
            bytesRiesived.clear();
        }
    }
}`

For example, I send 15 bytes from device and I see 15 bytes in the System.out.println(userPort.bytesAvailable());

Then after some time (about 5 minutes) I send another 15 bytes and I see 30 bytes in the System.out.println(userPort.bytesAvailable());

So tell me please: how to cleaning up com port buffer.

hedgecrw commented 5 years ago

Could you also show how you are setting up the port and registering your event listener? There are no persistent "port buffers" under the hood other than whatever is present in the device driver, so somehow the original 15 bytes aren't actually getting read until the next set of bytes come in.

yaroslau1 commented 5 years ago

Thaks for your reply. And this is my Constructor:

private SerialPort userPort;

private ComPortConnectivity(String comPortName){

    //userPort.addDataListener(this);
    if (comPortName != null) {
        userPort = SerialPort.getCommPort(comPortName);
    }
}

public static synchronized ComPortConnectivity getInstance(String comPortName) {
    if (comPortConnectivity == null) {
        comPortConnectivity = new ComPortConnectivity(comPortName);
    }
    return comPortConnectivity;
}`

And this is my openPort method:

`public void openPort() {

    userPort.setBaudRate(9600);
    userPort.setNumDataBits(8);
    userPort.setNumStopBits(SerialPort.ONE_STOP_BIT);
    userPort.setParity(SerialPort.NO_PARITY);
    userPort.addDataListener(this);
    userPort.openPort();
}`

And some more configuration: ` @Override

public int getListeningEvents() {
    return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
}`

so somehow the original 15 bytes aren't actually getting read until the next set of bytes come in.

No, I definitely get the first incoming message consisting of 15 bytes. And I get a second message, but these two and subsequent messages are stored in the port buffer on the PC.

hedgecrw commented 5 years ago

Is it possible that the device you are sending bytes from is not clearing its send buffer before sending (just appending 15 bytes to the send buffer every time)? With your configuration (event-based listening for LISTENING_EVENT_DATA_AVAILABLE), there are no buffers being used whatsoever, so the call to readBytes(buffer, length) translates to a read() call directly on the serial port, so there is something else going on here.

yaroslau1 commented 5 years ago

Thank you, the device i am sending bytes from is not clearing its send buffer before sending.

hedgecrw commented 5 years ago

Great! Closing as resolved.

omidziaee commented 3 years ago

How did you clear the output buffer of the sending device then? I have the same issue.

josephbriguet01 commented 3 years ago

Me too I have the same problem. How did you manage to empty the buffer ?

3bgdragon commented 1 year ago

I also had the same problem, so I cleared the value of the buffer as follows serialPort.getInputStream().skip(serialPort.bytesAvailable());

nadirvishun commented 8 months ago

Me too I have the same problem. How did you manage to empty the buffer ?

call com.fazecast.jSerialComm.SerialPort#flushDataListener method can flush already-received data.