Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Sending and receiving strings from COM-port via jSSC in Java

4.86/5 (6 votes)
27 Jul 2014CPOL 104.2K   4K  
How to send data to device and receive data from device through COM-port

Introduction

Program gets list of available serial ports on the PC and prints it to console. 

If there are no available serial ports, it prints an error message.

Else, it requests an input from user. After user typed a serial port which he wanted to use, program sends string "Hurrah!" to this serial port.

Then, program waits for response from serial port and prints it to console.

This program must properly work in Win32/64 (98..8.1), Linux (x86, ARM, x86-64), Solaris (x86, x86-64) and MacOS 10.5+ (x86, x86-64, PPC, PPC64).

Code snippets

At first, import jssc package to code:

Java
import jssc.*;

How to get a list of available serial ports via SerialPortList class:

Java
// getting serial ports list into the array
String[] portNames = SerialPortList.getPortNames();
        
if (portNames.length == 0) {
    System.out.println("There are no serial-ports :( You can use an emulator, such ad VSPE, to create a virtual serial port.");
    System.out.println("Press Enter to exit...");
    try {
        System.in.read();
    } catch (IOException e) {
         // TODO Auto-generated catch block
          e.printStackTrace();
    }
    return;
}

for (int i = 0; i < portNames.length; i++){
    System.out.println(portNames[i]);

How to send data through COM-port via SerialPort class and receive data through COM-port via SerialPortEventListener class:

Java
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("Hurrah!");
}
catch (SerialPortException ex) {
    System.out.println("There are an error on writing string to port т: " + ex);
}
...
...
...
private static class PortReader implements SerialPortEventListener {

    @Override
    public void serialEvent(SerialPortEvent event) {
        if(event.isRXCHAR() && event.getEventValue() > 0) {
            try {
                String receivedData = serialPort.readString(event.getEventValue());
                System.out.println("Received response: " + receivedData);
            }
            catch (SerialPortException ex) {
                System.out.println("Error in receiving string from COM-port: " + ex);
            }
        }
    }

}

How to test it

You may use VSPE emulator to emulate COM-ports and Advanced Serial Port Monitor to listen data sent through COM-port ("Hurrah!") and send it to the COM-port.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)