Fazecast / jSerialComm

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

Output buffer identical to the previous one #346

Closed josephbriguet01 closed 3 years ago

josephbriguet01 commented 3 years ago

Good evening,

I am writing this post with reference to issue #255 which is now closed. Unfortunately, the solution was not given. The person who opened this issue found a solution. But did not share it. Suddenly it seems important to me to reopen the issue. How do you empty the input buffer? I receive messages well. However, when I receive the following messages, they are identical to the very first one.

Niko-O commented 3 years ago

The user in that issue had this code:

int numRead = userPort.readBytes(readBuffer, readBuffer.length);
for (int i = 0; i < readBuffer.length; i++) {

readBytes returns the number of bytes that were actually read from the port, which micht be smaller than the buffer. But the user iterates over the entire buffer anyways. Maybe you do something similar? If your buffer is 64 bytes but readBytes only reads 1 byte, only the first byte of the buffer will be valid.

hedgecrw commented 3 years ago

I think in the original issue, there was a problem with the way the user was sending serial data (not using jSerialComm that I can tell). They were essentially adding contents to a send buffer before transmission, but never clearing that buffer or overwriting contents...just appending new data. I suspect anyone that has this problem is doing something similar, since there are no persistent buffers used in jSerialComm. Closing this issue again as it's not a jSerialComm bug, but rather a Java (or other language) coding bug.