我有一个处理串口通信的应用程序,我有一个"coms handler"类,它基本上处理所有数据的接收/发送,并将解析后的数据传递给我的表单.然而,虽然我能够传递double、string、int和serialport,但我无法传递string[]数组,因为某些原因,它总是显示为空.

这是coms handler类中使用BleuIO的扫描方法:

internal string[] scanBleu(int scanTime)
{
    //returns array of found devices.
    string[] devices = new string[] { };

    bleuInBuffer.Clear();
    //Scan
    bleuPort.WriteLine("AT+CENTRAL\r\n");
    Thread.Sleep(100);
    bleuPort.WriteLine("AT+GAPSCAN=" + scanTime + "\r\n"); //Scan for x-seconds
    Thread.Sleep(1000 * scanTime);//Sleep while scanning, disabling button for scan time 
    for (int i = 0; i < bleuInBuffer.Count; i++)
    {
        if (bleuInBuffer[i].Contains("Device")) 
        {
            devices.Append(bleuInBuffer[i].ToString());
            Console.WriteLine("Appending device: " + bleuInBuffer[i]);
        }
    }
    devices.Append("Hello this is a test"); //NOT EVEN THIS IS APPENDED!, RETURNED DEVICES IS ALWAYS SIZE 0
    Thread.Sleep(50); //BREAK POINT SET UP HERE!
    return devices;
}

I tried adding a break-point in the Thread.Sleep line, and AT THAT TIME (note this is AFTER I ran the line Append("hello...") so it should have at least 1 component, however according to the data shown devices has a value of {string[0]} type string[].
Also note that the line "Console.WriteLine("Appending device...") did print on my console 40x times (so 40 devices were found)

现在,在我的表格上,我有以下代码来调用它:

private void scanBleu_Click(object sender, EventArgs e)
{
    bleuComboBox.Enabled = false;
    connect2BleuBtn.Enabled = false;
    if (dataHandler.isBleuConnected)
    {
        string[] devicesFound = dataHandler.scanBleu(bLEScanTime);
        Thread.Sleep((1000 * bLEScanTime) + 25);
        if (devicesFound.Length != 0)
        {
            bleuComboBox.Items.Clear();
            foreach (string device in devicesFound)
            {
                if (device.Contains("CMT")) //filters out for only the device we want
                {
                    bleuComboBox.Items.Add(device);
                }
            }
            updateBleuScanBtn();
        }
        bLEScanTime++;
        bleuComboBox.Enabled = true;
        connect2BleuBtn.Enabled = true;
        return;
    }
    populateBleuCBox(dataHandler.getPortList());
    connect2BleuBtn.Enabled = true;
    bleuComboBox.Enabled = true;
}

这是我的控制台输出的(部分),只是为了让您可以看到附加了一些设备:

SCAN COMPLETE

Appending device: 
[01] Device: [1]6F:4C:F2:A8:FE:FA  RSSI: -74

Appending device: 
[02] Device: [1]3D:D1:56:D9:5A:60  RSSI: -75

Appending device: 
[03] Device: [1]23:C1:4A:71:07:FC  RSSI: -76

Appending device: 
[04] Device: [1]2F:EE:98:DA:7E:49  RSSI: -73

Appending device: 
[05] Device: [1]7A:3E:D9:6D:C0:C3  RSSI: -63

推荐答案

这里应该使用List<string>及其Add方法.List就像一个没有固定大小的数组,可以按照您的意愿扩展它1.

Append是一个所谓的LINQ方法,它将返回附加了该值的数组的"copy"2,而不是修改调用它的实例,这不是您想要做的.

如果你真的需要返回一个字符串数组,最终在列表中调用.ToArray().

internal string[] scanBleu(int scanTime)
{
    //returns array of found devices.
    List<string> devices = new();

    bleuInBuffer.Clear();
    //Scan
    bleuPort.WriteLine("AT+CENTRAL\r\n");
    Thread.Sleep(100);
    bleuPort.WriteLine("AT+GAPSCAN=" + scanTime + "\r\n"); //Scan for x-seconds
    Thread.Sleep(1000 * scanTime);//Sleep while scanning, disabling button for scan time 
    for (int i = 0; i < bleuInBuffer.Count; i++)
    {
        if (bleuInBuffer[i].Contains("Device")) 
        {
            devices.Add(bleuInBuffer[i].ToString());
            Console.WriteLine("Appending device: " + bleuInBuffer[i]);
        }
    }
    devices.Add("Hello this is a test"); 
    Thread.Sleep(50); //BREAK POINT SET UP HERE!
    return devices.ToArray();
}

1我想你来自Python背景,因为Python"列表"由于方括号的缘故看起来像C#arrays,并且没有固定的大小.然而,Python列表的C#companion有List个实例,它们的方法名略有不同,比如Add而不是Append.

2由于LINQ是延迟计算的,所以这条语句非常简单,只返回一个枚举数,在遍历它之后,它的效果会达到你想要的效果,而不会修改你调用它的实例,也不会自己创建新的实例.

Csharp相关问答推荐

我可以 suppress 规则CS 9035一次吗?

.NET框架4.7.2项目如何引用.NET Core 2.2库?

CsWin32如何创建PWSTR的实例,例如GetWindowText

如何使用C#和Graph API从Azure Directory获取用户详细信息

使用特定格式的JsonConvert序列化对象

如何使用XmlSerializer反序列化字符串数组?

如何注册类使用多级继承与接口

当前代码Cosmos DB 3.37.1:PartitionKey key key mismatch exception

取决于您的数据量的多个嵌套循环

如果设置了另一个属性,则Newtonsoft JSON忽略属性

DbContext-传递自定义配置选项

DateTime ToString()未以指定格式打印

在C#.NET项目中启动时,如何等待提升的PowerShell进程退出?

在等待OnGetAsync时打开Razor Page显示微调器

使用未赋值的、传递的局部变量

C#如何获取字符串中引号之间的文本?

如何在C#中从MongoDB IPipelineStageDefinition中获取聚合命令的字段/选项?

如何从原始图像到新创建的图像获得相同的特定 colored颜色 ,并且具有相同的 colored颜色 量和相同的宽度和高度?

嵌套Blazor组件内的验证

通过mini kube中的远程调试Pod与从emoteProcessPickerScript中解析错误输出的代码错误进行比较