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)
以下代码,
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?
推荐答案
看看http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/35954173-2eeb-46af-bb3e-86840c6b5484
似乎问题在于那些 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# SerialPort#Open() 方法由于端口名称而引发 ArgumentException?
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30