[英]How to run comport code in java without having it physically in linux system
I have a project in java, which is using comport for communication. 我在Java中有一个项目,该项目使用comport进行通信。 And using comport 1 and 2 number.
并使用comport 1和2的数字。 But my linux system is not capable to have com-port.
但是我的linux系统不具备通讯功能。 I want to run the code and want to listen data send on comport.
我想运行代码,并想监听在comport上发送的数据。 but when i am running code, it throws error.
但是当我运行代码时,它会引发错误。 How should i pursue.
我应该如何追求。
My comport utility code is something like this 我的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;
}
} }
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 . 我的工作站没有序列号(它是一台虚拟机),所以我诉诸于使用com0com创建虚拟串行端口。
It can create 2 virtual com port cross-linked (the input of one is the output of the other). 它可以创建2个交叉链接的虚拟com端口(一个的输入是另一个的输出)。
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. 这是在应用程序侦听其交叉链接的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.