开发者

Mysterious error message "The port 'COM1' does not exist"

开发者 https://www.devze.com 2023-02-17 14:30 出处:网络
I am getting the following error message when trying to access devices connected by USB port. The port \'COM1\' does not exist

I am getting the following error message when trying to access devices connected by USB port.

The port 'COM1' does not exist

By looking into device manager, I am certain that the device has been assigne开发者_开发百科d COM1 and no other device is attached to COM1. Why would this behavior occur?

Is this environment dependent, because the same version of the App doesn't produce this message when executed in a different system.


My case is a very specific one.

What was happening is, there was a device that was previously connected. When it's removed, the Object making the connection was still holding on to the port. Now, when a new device was inserted, although a search for that device revealed that it was in COM1, but trying to open it caused this exception as the previous and still live object was holding on to the port.


Currently i don't know what exactly .Net wants, but back in the old C/C++ days you had to open COM1: (take care for the colon after the port name).


Source: MsdnLink

I figured out problem and answer. We need to get name of the port where drivers for Arduino is installed on your machine. Not all Arduino is getting installed on 'COM1' port. My Arduino Mega 2560 R3 was installed on port, 'COM3'

Paste the code in C# Main() function,

var serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
 try
 {
     while (true)
     {
         serialPort.ReadTimeout = 2000;
         if (!serialPort.IsOpen) serialPort.Open();
         int readData = serialPort.ReadByte();
         Console.Write((char)readData);
     }
 }
 catch (Exception ex)  // Press CTRL-C to exit.
 {
     Console.WriteLine(ex.Message);
     Console.ReadKey();
 }
 serialPort.Close();

Steps to find the port name:

  1. Start > Run > devmgmt.msc
  2. Expand the node called, 'Port (COM & LPT)'
  3. You find the name of the port for your Arduino device.
0

精彩评论

暂无评论...
验证码 换一张
取 消