Skip to content

Cleaning up com port buffer #255

@yaroslau1

Description

@yaroslau1

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions