How to Create virtual serial port using java

6.9k views Asked by At

I had tried with TelnetSerialPort and jSSC library to create virtual sertial port but wont work.it not create Virtual serial port, it use existing serial port for communication.

This one my programm using jSSc library

public class VirtualSerialPort 
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        SerialPort serialPort = new SerialPort("COM12");
        try 
        {
            System.out.println("Port opened: " + serialPort.openPort());
            System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0));
            System.out.println("\"Hello World!!!\" successfully writen to port: " + serialPort.writeBytes("Hello World!!!".getBytes()));
            System.out.println("Port closed: " + serialPort.closePort());
        }
        catch (SerialPortException ex)
        {
            System.out.println(ex);
        }
    }
}

it gives following output

Port opened: false
jssc.SerialPortException: Port name - EMPTY; Method name - setParams(); Exception type - Port not opened.
2

There are 2 answers

1
Preston On BEST ANSWER

I'm assuming you are using Windows based off the COM port identifier. In Windows you have to access COM ports that are 10 or higher with a \\.\COMn notation (where n is your COM port number). Try changing your code to this (note there are double the amount of \ characters to get it to show up instead of triggering an escape sequence):

SerialPort serialPort = new SerialPort("\\\\.\\COM12");
0
Nikhil On

As per my understanding JSSC will connect to existing Serial Port. It will not create Virtual Serial Port. If you want Virtual Serial Port then you should use some emulator and then use JSSC to connect that virtual port.

Ref: Sending and receiving strings from COM-port via jSSC in Java : http://www.codeproject.com/Tips/801262/Sending-and-receiving-strings-from-COM-port-via-jS