java 如何模拟 COM 端口,向其中写入数据并从中读取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1059451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I emulate a COM port, write data to it and read data from it?
提问by homebrew2k9
I'm trying to test my code that reads from a USB port (COM25 when the device is connected) that is created when a device is connected to my computer and to a boat. I cannot power the USB device when not on the boat so testing is difficult. Can someone let me know how to simulate a COM port and write data to it so my test program is able to connect to that simulated COM port and read that data?
我正在尝试测试从 USB 端口(连接设备时为 COM25)读取的代码,该端口是在设备连接到我的计算机和船上时创建的。我不在船上时无法为 USB 设备供电,因此测试很困难。有人可以让我知道如何模拟 COM 端口并向其写入数据,以便我的测试程序能够连接到该模拟的 COM 端口并读取该数据吗?
I'm reading this from a Java program but the simulation doesn't need to be in Java or any specific language. Just a program that will simulate the COM port and allow me to connect to it. I downloaded a COM port emulator from AGG Software and it appears that it's writing to what I deem COM25 but I'm not able to connect to it from my Java test.
我正在从 Java 程序中读取此内容,但模拟不需要使用 Java 或任何特定语言。只是一个可以模拟 COM 端口并允许我连接到它的程序。我从 AGG Software 下载了一个 COM 端口模拟器,它似乎正在写入我认为的 COM25,但我无法从我的 Java 测试中连接到它。
回答by Kathy Van Stone
The general answer for this kind of problem is to wrap the code that talks to the COM port in a class that implements an interface. If you do this as a Facade (pattern) then you can also make the COM methods you call sensible from your end.
此类问题的一般答案是将与 COM 端口通信的代码包装在实现接口的类中。如果您将其作为 Facade(模式)执行,那么您还可以使您调用的 COM 方法从您的末端变得合理。
The interface can then be mocked or faked for the test. (There is a great article on test objects, but I haven't been able to find it yet.) One advantage here is that you can create a fake version that throws exceptions or otherwise does things that are possible for the port to do but hard to get it to do in practice.
然后可以模拟或伪造接口以进行测试。(有一篇关于测试对象的很棒的文章,但我还没有找到。)这里的一个优点是您可以创建一个假版本来抛出异常或以其他方式执行端口可能执行的操作,但是在实践中很难做到。
回答by rtperson
Where I work, we solved a similar issue by having our emulator not spoof a COM port at all. Here's how you can do it:
在我工作的地方,我们通过让我们的模拟器根本不欺骗 COM 端口来解决类似的问题。您可以这样做:
- Define an interface for talking with your COM port, something like IUsbCommService
- Implement your real COM-communcation service, using the standard Java Comm API
- For your emulator, simply kick of a thread that spits out the same sort of data you can expect from your USB device at regular intervals.
- Use your IOC framework of choice (e.g., Spring) to wire up either the emulator or the real service.
- As long as you hide your implementation logic appropriately, and as long as you code to your interface, your service-consumer code won't care whether it's talking to the real USB device or to the emulator.
- 定义与 COM 端口通信的接口,例如 IUsbCommService
- 使用标准的 Java Comm API 实现您真正的 COM 通信服务
- 对于您的模拟器,只需启动一个线程,该线程就会定期从 USB 设备中输出相同类型的数据。
- 使用您选择的 IOC 框架(例如 Spring)来连接模拟器或实际服务。
- 只要你适当地隐藏你的实现逻辑,只要你对你的接口进行编码,你的服务消费者代码就不会关心它是在与真正的 USB 设备通信还是与仿真器通信。
For example:
例如:
import yourpackage.InaccessibleDeviceException;
import yourpackage.NoDataAvailableException;
public interface IUsbProviderService {
public void initDevice() throws InaccessibleDeviceException;
public UsbData getUsbData()
throws InaccessibleDeviceException, NoDataAvailableException;
}
// The real service
import javax.comm.SerialPort; //....and the rest of the java comm API
public class UsbService implements IUsbProviderService {
.
.
.
}
// The emulator
public class UsbServiceEmulator implements IUsbProviderService {
private Thread listenerThread;
private static final Long WAITTIMEMS = 10L;
private String usbData;
public UsbServiceEmulator(long maxWaitTime) throws InaccessibleDeviceException{
initialize();
boolean success = false;
long slept = 0;
while (!success && slept < maxWaitTime) {
Thread.sleep(WAITTIMEMS);
slept += WAITTIMEMS;
}
}
private void initialize() throws InaccessibleDeviceException{
listenerThread = new Thread();
listenerThread.start();
}
private class UsbRunner implements Runnable {
private String[] lines = {"Data line 1", "Data line 2", "Data line 3"};
public void run() {
int line = 0;
while(true) {
serialEvent(lines[line]);
if(line == 3) {
line = 0;
} else {
line++;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
//handle the error
}
}
private void serialEvent(String line) {
if(/*you have detected you have enough data */) {
synchronized(this) {
usbData = parser.getUsbData();
}
}
}
}
Hope this helps!
希望这可以帮助!
回答by homebrew2k9
Thanks to all the answers so far! Here's what I ended up doing as a result of recommendations from someone at work.
感谢到目前为止的所有答案!根据工作人员的建议,我最终做了以下工作。
- Downloaded the COM Port Data Emulator (CPDE) from AGG Software
- Downloaded the Virtual Serial Port Driver (VSPD) from Eltima Software
- 从 AGG Software 下载 COM Port Data Emulator (CPDE)
- 从 Eltima Software 下载 Virtual Serial Port Driver (VSPD)
(I just randomly picked a free data emulator and virtual serial port package. There are plenty of alternatives out there)
(我只是随机挑选了一个免费的数据模拟器和虚拟串口包。那里有很多替代品)
Using VSPD, created virtual serial ports 24 and 25 and connected them via a virtual null modem cable. This effectively creates a write port at 24 and a read port at 25.
Ran the CPDE, connected to 24 and started writing my test data.
Ran my test program, connected to 25 and was able to read the test data from it
使用 VSPD,创建了虚拟串行端口 24 和 25,并通过虚拟空调制解调器电缆连接它们。这有效地在 24 处创建了一个写端口,在 25 处创建了一个读端口。
运行 CPDE,连接到 24 并开始编写我的测试数据。
运行我的测试程序,连接到 25 并且能够从中读取测试数据
回答by F.McGregor
There are plenty of relevant answers in this section. But as for me, I personally use Virtual Serial Port Driver, which works perfect for me. But I must admit that there are plenty alternatives when it comes to creating virtual ports: freevirtualserialports.com; comOcom to name a few. But I haven't got a chance to use them, so my recommendation for solving this problem is Virtual Serial Port Driver.
本节中有很多相关的答案。但就我而言,我个人使用 Virtual Serial Port Driver,它非常适合我。但我必须承认,在创建虚拟端口方面有很多选择:freevirtualserialports.com;comOcom 仅举几例。但是我没有机会使用它们,所以我解决这个问题的建议是 Virtual Serial Port Driver。
回答by maytham-???????
In addition all others, I would like to added this nice, free emulator https://sites.google.com/site/terminalbpp/I do use. I do also use AGG Com port data emulator.
除了所有其他人之外,我想添加这个不错的免费模拟器https://sites.google.com/site/terminalbpp/我确实使用过。我也使用 AGG Com 端口数据模拟器。
回答by Danra
I recommend fabulatech's virtual modem. Get it at http://www.virtual-modem.com
我推荐 fabulatech 的虚拟调制解调器。在http://www.virtual-modem.com 上获取
You might also want to get a COM port monitor for your tests - You can find it at http://www.serial-port-monitor.com
您可能还想为您的测试获得一个 COM 端口监视器 - 您可以在http://www.serial-port-monitor.com 上找到它
Good luck with the boat! :)
祝你坐船好运!:)

