繁体   English   中英

如何在Java系统中运行comport代码而又不实际在Linux系统中运行

[英]How to run comport code in java without having it physically in linux system

我在Java中有一个项目,该项目使用comport进行通信。 并使用comport 1和2的数字。 但是我的linux系统不具备通讯功能。 我想运行代码,并想监听在comport上发送的数据。 但是当我运行代码时,它会引发错误。 我应该如何追求。

我的comport实用程序代码是这样的

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;
}

}

我在开发需要读取串行端口的软件时遇到了类似的问题。

我的工作站没有序列号(它是一台虚拟机),所以我诉诸于使用com0com创建虚拟串行端口。

它可以创建2个交叉链接的虚拟com端口(一个的输入是另一个的输出)。

我配置了一个供我的应用程序使用的程序,并使用另一个程序创建了一个小程序来发送输入以测试应用程序。

这是在应用程序侦听其交叉链接的COM时在虚拟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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM