Click here to Skip to main content
13,000,323 members (53,665 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 27 Jul 2014

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

, 27 Jul 2014
Rate this:
Please Sign up or sign in to vote.
How to send data to device and receive data from device through COM-port


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:

import jssc.*;

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

// 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 {;
    } catch (IOException e) {
         // TODO Auto-generated catch block

for (int i = 0; i < portNames.length; i++){

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

serialPort = new SerialPort("COM1");
try {


    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | 

    serialPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);

catch (SerialPortException ex) {
    System.out.println("There are an error on writing string to port т: " + ex);
private static class PortReader implements SerialPortEventListener {

    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.


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


About the Author

You may also be interested in...

Comments and Discussions

QuestionSynchronous serial port communication Pin
Member 1264310420-Jul-16 19:06
memberMember 1264310420-Jul-16 19:06 
Questionhow to write data to com port Pin
Member 124275882-Apr-16 0:45
memberMember 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
memberMember 117659247-Jul-15 1:42 
QuestionAlternate to jssc Pin
victor29662127-Mar-15 2:12
membervictor29662127-Mar-15 2:12 
QuestionWriting string to commport Pin
Member 998757516-Feb-15 0:06
memberMember 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 27 Jul 2014
Article Copyright 2014 by Emiliarge
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid