I am working on driver that talks to a device via a serial port in C#. I do not always have the device available to do physical testing with. Is there a way I can simulate a device on a serial port so that it respond开发者_运维百科s in an ideal manner?
Get a second COM port and use a Null-modem cable to connect the COM ports to allow two C# programs to talk to each other.
I used Com0Com for a while and wrote some simulator/emulator code.
What kind of driver? If it is the serial interface driver, then that gets quite tricky.
However, if your driver is an application level above the Windows device driver, then it's fairly easy to replace the i/o behavior by altering the string passed to CreateFile
, or whatever layer on top of that C# uses.
== More ==
Since you use the .net library tools, this technique may be too yucky to bother. However the idea is to replace where, at some point, your code says open COM1:
or whatever. Change that to be a file which has the simulated data, say `c:/com1testdata.txt'. Additional emulation code which recognizes the contents of the file for pauses and/or responses might be useful for some protocols. Data which is written to the port can be logged or ignored, depending on your requirements.
When working in a team designing some custom hardware I used a great terminal emulator called ZTerm (only available for Mac, I believe). It allows you to script responsees which enabled me to complete the software to our pre-agreed specification while the custom controller chips were still in the design iteration stage.
If you're running in a VM, you may be able to attach things to the serial port. QEMU, for instance, allows you to attach the virtual serial port to a TTY, which you can then interact with either manually or with a program running on the host.
精彩评论