Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / Java
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.86/5 (6 votes)
27 Jul 2014CPOL 102.4K   4K   2   5
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)



Comments and Discussions

 
QuestionSynchronous serial port communication Pin
Member 1264310420-Jul-16 19:06
Member 1264310420-Jul-16 19:06 
Questionhow to write data to com port Pin
Member 124275882-Apr-16 0:45
Member 124275882-Apr-16 0:45 
QuestionCan anyone please tell me How to write the same data which is being read? In the above example Pin
Member 117659247-Jul-15 1:42
Member 117659247-Jul-15 1:42 
QuestionAlternate to jssc Pin
victor29662127-Mar-15 2:12
victor29662127-Mar-15 2:12 
QuestionWriting string to commport Pin
Member 998757516-Feb-15 0:06
Member 998757516-Feb-15 0:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.