Reading serial port in Java

前端 未结 3 1910
情歌与酒
情歌与酒 2020-12-06 02:36

I\'m beginner in Java. I\'m reading data from device through serial port. I\'m getting data for every one minute, but first reading is coming half, after that data is coming

3条回答
  •  眼角桃花
    2020-12-06 03:03

    Try flushing the input buffer of the port before doing your read. Otherwise, if the sending end has sent data during your program's startup (or closely before, that might be up to the operating system), you will get old buffered data.

    Also, if possible, consider adding message framing to the protocol, so you can detect when you have read something that is not, in fact, a complete message, and discard it. This is often very helpful with these kinds of issues.

提交回复
热议问题