Click here to Skip to main content
15,896,502 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 8-Dec-20 22:07pm

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[^]
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900