📜 ⬆️ ⬇️

We work with a COM port from Java using jSSC

Already a lot of water has flowed since we used mice on com-mode, modems, played “on it” in games for lack of a network, threw files and ... that we just didn’t do with it.

Everything, in the 21st century, USB 3.0 crushed everything and everyone, the coms have become extinct, and now the “newborn” programmers look at this connector with perplexity and reproach. But not everyone died, some people still remained ... These brave guys send bytes on three wires and with a condescending smile look at the "newborns." And about what these guys enjoy doing their job, and I want to tell in this short article. It will talk about a small library named jSSC.

Judging by the way people at Habré speak of com-e, apparently many really believe that it is no longer needed and do not understand why someone still uses it, why they write libraries and software, produce equipment with this port on board. From myself I can only say one thing, in order not to breed holivar, as it seems to me, it disappeared only for home use, and in factories and in various laboratories it exists and feels just fine. There are many reasons for this, but the main one seems to me to be simplicity and convenience in work.

But the conversation is not about that, so we will not be distracted. So, jSSC is a Java library for easy work with a COM port (Java Simple Serial Connector). She takes her official, public start in 2010. It was then that it was decided to share it with Java developers (distributed under the LGPL license). The sad fact led to the writing of the library - the lack of adequate tools for working with this port. Many will say, and have already said, they say there is javax.comm, rxtx and there is still a giovynet (but it cannot be seriously considered under any circumstances), but unfortunately everything is not that simple. Our main OS is Windows, and javax.comm 3.0 will not work, rxtx is not suitable because of its instability. As a result, there was nothing to do, I had to write my own library.
')
When developing, simplicity of use was put at the forefront, because every day you have to work with the equipment, and you want your work to be enjoyable. But don't let this be misleading, simplicity is not a silver bullet and not a “make pi * dato” button, you need to understand what you want to get from the device, how it works and how com-u interacts in general. When developing, a greater bias was made in the direction of the Windows style of development for the com port, this is mainly expressed in the naming of constants for setting the event mask, flow control mode, error parsing, etc.

jSSC can be divided into several main parts:

Now you can sketch a small example. Suppose that we have some kind of device that uses hardware flow control, and we want to receive answers to requests from it. Here is an example implementation:

import jssc.SerialPort; import jssc.SerialPortEvent; import jssc.SerialPortEventListener; import jssc.SerialPortException; public class Test { private static SerialPort serialPort; public static void main(String[] args) { //     serialPort = new SerialPort("COM1"); try { //  serialPort.openPort(); //  serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT); //     serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR); //   serialPort.writeString("Get data"); } catch (SerialPortException ex) { System.out.println(ex); } } private static class PortReader implements SerialPortEventListener { public void serialEvent(SerialPortEvent event) { if(event.isRXCHAR() && event.getEventValue() > 0){ try { //   ,    .. String data = serialPort.readString(event.getEventValue()); //    serialPort.writeString("Get data"); } catch (SerialPortException ex) { System.out.println(ex); } } } } } 


I think it's pretty simple. In addition to read / write operations, jSSC provides the following options:

It is worth noting one important point: USB-> COM adapters can sometimes behave differently than you expect, this is understandably dependent on their driver, so if you are going to use such adapters, test them properly in Linux and Windows, if Of course, you need support for different operating systems.

A year after the publication of jSSC there were quite a few letters from developers and firms, and the scope was quite extensive. Here is a small list:

Well, I send a separate hello to the guys from India, for whom I made the jSSC-CE version (Windows CE). If my memory serves me correctly, they ordered some equipment on WinCE without checking whether there is a SUN / Oracle JVM for it, and were very surprised when they learned that it was not there. I used CreMe JVM for development, I hope that everything worked out for them.

Well, let me leave, I apologize for a bit of a messy narration, and I hope that jSSC will help you among you. I will be glad to answer your questions.

And here is the link to the project page: http://code.google.com/p/java-simple-serial-connector

Source: https://habr.com/ru/post/133766/


All Articles