Closed yaroslau1 closed 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.
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.
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.
Thank you, the device i am sending bytes from is not clearing its send buffer before sending.
Great! Closing as resolved.
How did you clear the output buffer of the sending device then? I have the same issue.
Me too I have the same problem. How did you manage to empty the buffer ?
I also had the same problem, so I cleared the value of the buffer as follows serialPort.getInputStream().skip(serialPort.bytesAvailable());
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.
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) {
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.