Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Windows Forms C#, Return string Array always empty

I have an app that deals with serial port communications, I have a "coms-handler" class that basically handles all the data receiving/sending and just passes the parsed data to my form, However, while I am able to pass doubles, strings, ints and SerialPorts, I cannot pass a string[] array for some reason it always shows empty.

This is the scan-method in the coms-handler class, using 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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now, on my form, I have the following code to call it:

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;
}

This is (some) of my console output, just so you can see that some devices were appended:

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

>Solution :

You should be using List<string> and its Add method here.

Append is a so called LINQ method, which would return a "copy"1 of the array with that value appended, not modifying the instance you called it on, which is not what you intended to do.

If you really need to return a string array, call .ToArray() on the list eventually.

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 This statement is extremely simplified since LINQ is lazy-evaluated, only returning an enumerator with the effects you want it to have once you iterated through it, never modifying the instance you call it on or creating new instances by itself.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading