3

Please note: Although the library I'm using is called the Java Simple Serial Connector, this question is really more about Java coding against serial ports in general, and any strategies associated with doing so.

I'm using a Java library (JSSC as mentioned above) to read data from a serial port. The library requires you to poll the port for all available bytes. But this has me worried, because in between 2 different poll attempts, data could be streamed to the port (from the serial device) and therefore perhaps "lost".

Unless there is some kind of buffering/caching mechanism at the hardware layer that buffers data coming in to the serial port. In that case, the library's API makes sense, as it probably consults the buffer and reads anything thats been queueing up inside it. So I ask:

  1. Is there such a "serial port buffer"? If so what is it? If not, then are there any strategies to "lossless" serial port reads?
  2. If there is such a buffer, how does it work? What happens when it fills up?
  3. The Java lib I'm using reads serial port data as byte[]'s; does it make sense to then construct a ByteArrayInputStream from these byte[]? What benefits would one gain from doing so?
DirtyMikeAndTheBoys
  • 1,077
  • 3
  • 15
  • 29
  • 1
    With RXTX, there is an observable for data avialable on a serial port object. A flag needs to be set to raise those events on the serial port. I do, and I subscribe. Perhaps JSSC has something similar? I tried to check for you but the java doc page on their site renders a blank page :/ – Mark W Sep 03 '14 at 18:20
  • I dug in a little more and it looks like RXTX works in a very similar way with a listener object, as the ones provided by JSSC. See the Event mask & SerialPortEventListener interface section of their examples page. – Mark W Sep 03 '14 at 18:22
  • Thanks @MarkW (+1) - to access the Javadocs you need to download the source JAR and then extract it. But I'll save you the time: they don't mention anything similar. **Most importantly**: I would think this would be a hardware/OS feature that would be Java lib-agnostic, no? Thanks again! – DirtyMikeAndTheBoys Sep 03 '14 at 18:23
  • I would assume so to, but they do in fact provide an event observable type construct. The field for data available seems to be event.isRXCHAR(), check out that section of their examples page. Something like serialPort.setEventsMask(SerialPort.MASK_RXCHAR); will cause the data available event to be raised, and event.getEventValue() tells you the number of bytes available. – Mark W Sep 03 '14 at 18:24
  • Since this is a wrapper to access the serial ports as exposed by the underlying OS you can be sure that there is at least as much buffering as the OS provides. The hardware usually also provides some buffering (for at least 1 byte, possibly more). Failing to read data before the buffers fill up will be detected by the driver and some kind of error should bubble up to your code. Surely the javadocs will provide some details on that. – Durandal Sep 03 '14 at 18:40

0 Answers0