0

I have a project in java, which is using comport for communication. And using comport 1 and 2 number. But my linux system is not capable to have com-port. I want to run the code and want to listen data send on comport. but when i am running code, it throws error. How should i pursue.

My comport utility code is something like this

import com.fazecast.jSerialComm.SerialPort;

public class ComPortUtil {
private static SerialPort comPort;
private static SerialPort relayPort;

static {
    SerialPort[] serialPorts = SerialPort.getCommPorts();
    comPort = serialPorts[3];
    comPort.setBaudRate(115200);
    comPort.setParity(SerialPort.NO_PARITY);
    comPort.setNumDataBits(8);
    comPort.setNumStopBits(SerialPort.ONE_STOP_BIT);
    relayPort = serialPorts[1];
    relayPort.setBaudRate(115200);
    relayPort.setParity(SerialPort.NO_PARITY);
    relayPort.setNumDataBits(8);
    relayPort.setNumStopBits(SerialPort.ONE_STOP_BIT);
}

public static SerialPort getPOSPort() {
    return comPort;
}

public static SerialPort getRelayPort() {
    return relayPort;
}

}

Rishabh
  • 89
  • 1
  • 4
  • 11
  • *But my linux system is not capable to have com-port. I want to run the code and want to listen data send on comport* **What** data? Your linux system doesn't have a com port, so what data are you trying to listen for on the port it doesn't have? – Elliott Frisch Nov 27 '18 at 11:22
  • My java code is writing hex code data on port which is consumed by third party product attached using comport but unfortunately all developer can't have that third party product to develop things. so to run project i need this. – Rishabh Nov 27 '18 at 11:28
  • You can't write data on a port which isn't there. Nor can something read data from a port which isn't there. Electricity doesn't flow across an air gap. – Elliott Frisch Nov 27 '18 at 11:38

1 Answers1

0

I had a similar problem, developing a software which needed to read a serial port.

My workstation did not have serials (it was a virtualized machine) so I resorted to create virtual serial ports with com0com.

It can create 2 virtual com port cross-linked (the input of one is the output of the other).

I configured one to be used by my app, and I created a small program using the other to send input for testing the app.

This is an example of a test writing on a virtual COM port, while the application is listening on its crosslinked COM.

static SerialPort commPort = null;

@BeforeClass
public static void setUpClass(){
    commPort = SerialPort.getCommPort("CNCB0");
    commPort.openPort();
}


@Test
public void showControl(){
    shortWait();
    send(commPort, "S421803171");
    // ...
    // A delay of few millis ...
    shortWait();
    String value = lookup("#fldFicheBarcode").queryAs(TextField.class).getText();
    // ...
    assertEquals("S421803171", value);
}

protected static void send(final SerialPort commPort, final String _txt) {
    ForkJoinPool.commonPool().submit(()->{
        final String txt = !_txt.endsWith(CRLF)?_txt+CRLF:_txt;
        byte[] bytes = txt.getBytes();
        int writeBytes = commPort.writeBytes(bytes, bytes.length);
        logger.debug("Written bytes:"+writeBytes);
    }).join();
}
minus
  • 2,646
  • 15
  • 18
  • Will it work on linux machine, i guess com0com support for windows machine – Rishabh Nov 27 '18 at 13:31
  • Working on a win machine I used that sw, I guess on linux you can do the same with other tools, but the basic idea is the same. Check https://stackoverflow.com/questions/23867143/null-modem-emulator-com0com-for-linux – minus Nov 27 '18 at 13:40
  • I have tried above experiment and able to create virtual connection using command "socat -d -d pty,raw,echo=0 pty,raw,echo=0" I got stuck at SerialPort[] serialPorts = SerialPort.getCommPorts(); relayPort = serialPorts[2]; throwing error: Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 at in.smartbox.terminal.pos.util.ComPortUtil.(ComPortUtil.java:17) ~[classes/:na] ... 25 common frames omitted – Rishabh Nov 29 '18 at 11:40