c# - SerialPort.GetPortNames() 创建 System.StackOverflowException
问题描述
基于 Atmel 示例,我在 SAMD21J18A 单片机中实现了引导加载程序。我可以通过 USB CDC 连接到引导加载程序,它在 Windows 中被识别为名称为:的 COM 端口AT91 USB to Serial Converter
,没有显示任何问题。
COM 端口工作正常,我制作了一个工作 Windows Form C# 程序来将代码下载到引导加载程序。在工作程序中,我刚刚手动设置了PortName
.
现在,我想让程序对用户更友好,并希望ComboBox
使用户能够选择要使用的 COM 端口。因此我正在调用该GetPortNames
函数:
string[] Ports = null;
Ports = SerialPort.GetPortNames();
只要我不插入引导加载程序,这工作正常。
如果我附上例如。一个 FTDI 设备,它会检测到该设备并将其添加到列表中,但是如果我附加了Bootloader("AT91 USB to Serial Converter")
,那么我会System.StackOverflowException
在该SerialPort.GetPortNames()
功能上得到一个。
有谁知道GetPortNames
USB CDC驱动程序有什么问题?
感谢您的帮助 mjwills :-)
完整的程序太大,无法在这里展示,所以我决定制作一个简短的程序来展示这个问题。但是,新的短节目没有这个问题。因此,发现问题并非直接源自visual studio所引用的行。无论如何,这是造成问题的函数:
// Update comport pull-down list:
private void UpdateComPortList()
{
string[] Ports = null;
Ports = SerialPort.GetPortNames();
int index = -1;
string ComPortName = null;
comboBoxComPort.Items.Clear();
do
{
index += 1;
comboBoxComPort.Items.Add(Ports[index]);
//if(Properties.Settings.Default.comPort == Ports[index]) comboBoxComPort.SelectedIndex = index;
}
while (!((Ports[index] == ComPortName) || (index == Ports.GetUpperBound(0))));
comboBoxComPort.Items.Add("update list...");
}
如果我删除我现在评论的行,程序就可以工作。我认为问题在于,我尝试在添加项目的同时选择一个项目......
解决方案
推荐阅读
- c++ - 排序元素的 std::set 构造函数的复杂性
- javascript - 如何在谷歌地图上显示多个颜色图钉
- python - 如何使用 pytest-cov 生成覆盖率报告并打印到终端?
- javascript - 对反应角色不和谐机器人有问题
- strapi - strapi-girdsome 首发问题
- kubernetes - 在实时集群中添加 apiserver extraArgs/extraVolumes
- javascript - 未捕获的 ReferenceError:未定义水印
- elasticsearch - logstash 配置文件的 Grok 模式
- javascript - Javascript filter() 函数过滤父元素
- python - 使用变量中的值调用函数