C#SerialPort#Open()方法是否由于端口名称而引发ArgumentException? [英] C# SerialPort#Open() method throws ArgumentException because of port name?
本文介绍了C#SerialPort#Open()方法是否由于端口名称而引发ArgumentException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的本地计算机上,
- Windows XP
- 端口(COM和LPT)
- COM3
- RIM虚拟串行端口v2(COM4)
- RIM虚拟串行端口v2(COM5)
- Windows XP
- Ports (COM & LPT)
- COM3
- RIM Virtual Serial Port v2 (COM4)
- RIM Virtual Serial Port v2 (COM5)
以下代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO.Ports; using System.Threading; namespace OpenSerialPortTest { class Test { static void Main(string[] args) { foreach (String serialPortName in SerialPort.GetPortNames()) { SerialPort serialPort = new SerialPort(serialPortName); try { serialPort.Open(); // Line 19 Console.WriteLine(serialPort.PortName); } catch (Exception ex1) { Console.WriteLine(ex1); try { serialPort.Close(); } catch (Exception ex2) { Console.WriteLine(ex2); } } } Console.ReadLine(); } } }
将抛出以下
ArgumentException
,也就是说,
给定的端口名不是以COM/com开头或不能解析为 有效的串行端口.
The given port name does not start with COM/com or does not resolve to a valid serial port.
有人知道为什么会这样吗?
Does anybody know why this is happening?
推荐答案
似乎问题在于那些COM端口已映射到其他端口,并在响应中给出了这个奇怪的错误.
seems the problem was that those COM ports were mapped to something else and gives this strange error in response.
我遇到了完全相同的问题.我发现我有一个Windows打印机设置要使用相同的端口.一旦我在打印机设置中更改了端口,SerialPort.Open()就会起作用.
I had exactly the same problem. I found that I had a windows printer setup to use the same port. As soon as I changed the port in the printers settings, the SerialPort.Open() worked.
这篇关于C#SerialPort#Open()方法是否由于端口名称而引发ArgumentException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文