Fazecast / jSerialComm

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

serialEvent( SerialPortEvent.LISTENING_EVENT_DATA_RECEIVED ) says there is data but nothing is available! #216

Closed evanzulli closed 5 years ago

evanzulli commented 5 years ago

Hi! My code receives the event but when i try to read from InputStream there is nothing to read. byteBuffer[0] = 0 says there is something but is.available() returns 0. What am I doing wrong? Thanks

hedgecrw commented 5 years ago

It looks like you are mixing two types of incompatible reading methodologies. If you are receiving SerialPortEvent.LISTENING_EVENT_DATA_RECEIVED, then all data is already read for you by the framework, and you simply access it using the getReceivedData() method of the SerialPortEvent object that is passed to your serialEvent() callback. If you want to use InputStreams, then you either need to not use event-based reading or only use event notifications for LISTENING_EVENT_DATA_AVAILABLE (not for LISTENING_EVENT_DATA_RECEIVED).

Meimurugan commented 5 years ago

could you give some example snippet about getReceivedData() @hedgecrw ?? Thank you

hedgecrw commented 5 years ago

Example snippets are all available in the Usage Wiki: https://github.com/Fazecast/jSerialComm/wiki/Usage-Examples

Specifically: https://github.com/Fazecast/jSerialComm/wiki/Event-Based-Reading-Usage-Example

hedgecrw commented 5 years ago

Closing due to no issue, wrong event type being used.