Click here to Skip to main content
12,451,896 members (66,430 online)
Click here to Skip to main content
Add your own
alternative version

Stats

27.3K views
1.4K downloads
2 bookmarked
Posted

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

, 27 Jul 2014 CPOL
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 viaSerialPort 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...

Pro
Pro

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.

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