Nicola Rigoni
Nicola Rigoni

Reputation: 241

C# snap7 reading array of char null character

I'm reading an array of char from the plc but I have to add the character to the string if it's not empty but as in the picture all the character in the plc are empty but when run the code it add all the character anyway. I can't understand where the problem is

enter image description here

Code:

String sealNumber = "";

for (int i = 90; i < 108; i++)
{
    String c = S7.GetCharsAt(bufferLineToRead, i, 1);

    if (c != "" && c != "?")
    {
        Console.WriteLine("STRINGA " + c);
        sealNumber += c;
    }
}

Upvotes: 0

Views: 496

Answers (1)

nvoigt
nvoigt

Reputation: 77364

A string can be "empty" (means no characters are in it), but a character can never be "empty". A character is always exactly one character.

That means your comparison of c != "" is most likely being true because the string has exactly one character in it.

I have no idea what the plc sends there. Maybe it's zero characters (characters with the value 0, as opposed to the UNICODE numbers that make readable letters).

You may have better luck with using !string.IsNullOrWhiteSpace(c) because that will check for all characters that aren't considered "something".

But in the end, you will need to fire up your debugger and find out what exactly happens there. We cannot do that remotely.

Upvotes: 1

Related Questions