Question

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).

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top