I have a utility which connects to a device through a serial port. To test this application I'm writing a simulator for the device.
Both applications are intended to run on the same Windows 7 machine.
I'm writing the simulator in C#. I used com0com to create a pair of virtual ports (in this case - CNCA8 & CNCB8, since I already have several existing pairs).
I'm trying to open CNCB8 in the simulator (application is not running yet), and I get an exception:
An unhandled exception of type 'System.ArgumentException' occurred in System.dll
Additional information: The given port name does not start with COM/com or does not resolve to a valid serial port.
Sample code:
SerialPort port = new SerialPort("CNCB8", 9600, Parity.None, 8, StopBits.One);
port.Handshake = Handshake.None;
port.Open();
This question is not relevant since it's an unused virtual port (IsOpen returns false if that matters): C# SerialPort#Open() method throws ArgumentException because of port name?
EDIT: I also made sure that the port name appears in SerialPort.GetPortNames(), and it appears in WIndows' device manager under 'com0com serial port emulators'. I also tried using CNCA instead of B (with a few different existing virtual ports), tried to remove the 'HandShake' line and tried changing the baud rate to 115200, just in case (although ultimately I need 9600 there).
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Com0com does let you edit the port names. Does this still happen if you rename your pair to, say, COM10 and COM11?
Also, are the ports actually alive? On Windows Server 2008 R2 we had to issue the bcdedit command the Com0com README file mentions because the device driver is not trusted by the operating system.
As of .NET Framework 4.7.2, the SerialPort
class creates a SerialStream
instance which has the following code in the constructor:
if (portName == null || !portName.StartsWith("COM", StringComparison.OrdinalIgnoreCase))
throw new ArgumentException(...)
Sad, really... since calling the static method SerialPort.GetPortNames()
happily returns the string array {"COM1", "CNCA0", "CNCB0"}
.
My solution is as in Ian Millers answer to rename the virtual COM ports. For this, I am calling
setupc.exe change CNCA0 Portame=COM8
setupc.exe change CNCB0 Portame=COM9
after com0com is installed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With