首页 > 解决方案 > 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()功能上得到一个。

有谁知道GetPortNamesUSB 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#

解决方案


推荐阅读