Click here to Skip to main content
11,570,906 members (51,760 online)
Click here to Skip to main content

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

, 27 Jul 2014 CPOL 10.2K 506 1
Rate this:
Please Sign up or sign in to vote.
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:

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 {
        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:

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)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
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    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 27 Jul 2014
Article Copyright 2014 by Emiliarge
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid