What benefits do Java serial port libraries have over plain IO to the device?

1.8k views Asked by At

There is a lot of talk about Java serial port libraries like RXTX and JSSC, but what do they really provide? In both linux and windows you can simply open the serial port like a file for reading and writing, can't you? What is the advantage of using the libraries over just reading and writing from the device with file IO?

I understand that the libraries allow you to configure the ports, which would normally need to be done by commandline calls. But assuming the ports are already configured, is there any reason to use the libraries?

3

There are 3 answers

0
rm5248 On BEST ANSWER

In both linux and windows you can simply open the serial port like a file for reading and writing, can't you?

While this is possible to do, it's not really a recommended way to do it. To go to your second point:

But assuming the ports are already configured, is there any reason to use the libraries?

Assuming the ports are configured and configured properly, then it's perfectly possible to simply open up the serial port and read and write it like a normal file. This does come with another caveat though: that if you depend on the control signals at all, you won't be able to get that data from the port. Most serial devices that I have worked with don't do anything with the control lines at all, but that's not something that you can always be sure of.

The point behind using a library is so that you can get and set the exact settings that you need to in order to talk appropriately over the port.

As for JSSC/RXTX not having an InputStream/OutputStream for them, I didn't like that about those libraries either, so I wrote my own.

7
jurez On

Serial ports, historically, have been designed for slow communication lines such as modems. They have additional signals for "clear to send", "request to send", "data terminal ready", "hang up", "ring", etc. Some serial equipment still uses those. That stuff still exists in hardware, so a serial library should provide an API to access it.

Another thing are the interrupts. You might not want to poll the connection all the time to see if there is data available. Serial APIs usually provide a callback or an event handler for that.

Opening and closing port is best done in-application. Strictly speaking, it's not neccessary, but it's good practice not to expect a particular port being open at start or leave it locked on exit.

0
Hercules dd On

Following is a quick list of why we may consider a library but largely I feel that if we are making a commercial product than ready made libraries may be good choice otherwise as a learner we can write our own.

  1. Uniformity across various operating systems. We have to write code for all supported OS which is a task in itself.
  2. The library authors may have more experience in that particular topic so quality might be better than us.
  3. Time to finish project (time to market) is another factor.
  4. Comprehensive test suite
  5. Beyond simple read/write can be extra features especially in case of GUI driven products like hot plugging and visual indications etc.
  6. Library might implement protocols or specifications means simple read/write may come wrapped in stream where the serial port specific things are hidden and a uniform layer is exposed to app.
  7. Support for wider range of hardware devices esp USB-UART Take a look at these 2 open source libraries 1st library and 2nd library
  8. Support for both Embedded and Desktop OS