我有一个处理串口通信的应用程序,我有一个"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