如何使用C#创建虚拟COM端口 [英] How to create a virtual com port using C#

查看:610
本文介绍了如何使用C#创建虚拟COM端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#(不使用第三方工具)创建虚拟Com端口,以便在不连接原始设备的情况下连接手持扫描仪。请帮助我提供有效的输入。希望你们中的任何一个人以前都会尝试过。寻求你的帮助。



我的尝试:



我我不知道为这个虚拟的COM端口模拟器继续进一步。

How to create a virtual Com port using C# (without using third party tools) for connecting a hand scanner without connecting with the original device. Please do help me with your valid inputs. Hope any one of you would have tried this before. Hence seeking your help.

What I have tried:

I don't have any idea to proceed further for this virtual com port simulator.

推荐答案

基本上,你不能 - 不在实践中。你需要创建的是一个设备驱动程序假装是一个com端口,这些并不简单,根本不是。

如果你想要测试你的软件,那么我强烈建议你去第三方,或者通过零调制解调器电缆在你的机器上使用第二个端口来提供设备模拟。

对于第三方:com0com [ ^ ]似乎很受欢迎,但我自己没有使用它。

对于电缆:使用虚拟端口测试串行应用程序 [ ^ ]
Basically, you can't - not in practice. What you need to create is a device driver which pretends to be a com port, and those aren't simple, not at all.
If you want this to test your software, then I'd strongly suggest that you do go third party, or, use a second port on your machine via a "null modem cable" to provide the device emulation.
For third party: com0com[^] seems popular, but I haven't used it myself.
For cable: Testing Serial Application with Virtual Ports[^]


您需要模拟什么?与测试期间不存在的外部设备的通信?

- 首先找出你真正需要的SerialPort的哪些功能和属性(在我们的例子中,我们只需要其中一些)。 br />
- 创建一个定义它们的接口。

- 为实现接口的真实端口创建一个包装器。这将是生产中使用的实例。

- 让你的其他代码依赖于你的界面而不是真正的端口。

- 创建一个实现你的虚拟端口接口,并添加用于模拟设备的逻辑。这是用于测试的实例。

- 您可以创建更多模拟通信问题的假人 - 现在您也可以测试您的代码是否也能应对。
What do you need to simulate? The communication with an external device which is not present during the tests?
- First find out which functions and properties of SerialPort you really need (in our case, we just needed a few of them).
- Create an interface defining them.
- Create a wrapper for the "real" port which implements your interface. That will be the instance used in production.
- Make your other code depend on your interface instead of the "real" port.
- Create a dummy port which implements your interface, and add the logic for emulating the device. That's the instance used for testing.
- You may create further dummies which simulate communication problems - now you can test that your code copes with that, too.


这篇关于如何使用C#创建虚拟COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆