How to access the Serial Port of a computer in Java?

1

How do I access / control a Serial Port through Java? Currently I got access in C ++, however, in Java I do not know how to do it. Any suggestions?

    
asked by anonymous 15.07.2015 / 19:22

1 answer

4

There is the jSSC library that provides communication with the serial port link

Add the dependency in your pom.xml, if you do not use maven download the library and put it in the classpath.

<dependency>
    <groupId>org.scream3r</groupId>
    <artifactId>jssc</artifactId>
    <version>2.8.0</version>
</dependency>

Test Class

public class SerialTest {

public static void main(String[] args) {
    SerialPort serialPort = new SerialPort("COM1");

     try {
            System.out.println("Port opened: " + serialPort.openPort());
            System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0));
            System.out.println("\"Hello World!!!\" successfully writen to port: " + serialPort.writeBytes("Hello World!!!".getBytes()));
            System.out.println("Port closed: " + serialPort.closePort());
        }
        catch (SerialPortException ex){
            System.out.println(ex);
        }
}

}

I took this example from the API site

    
15.07.2015 / 19:29