1

I want to ask about reading string array from PLC Beckhoff.

I have some methods for reading Int16 - it works correctly

public Int16[] ReadArrFromPLC_Int16(string Mnemonic, int ArrLength)
{
    Int16[] TempVariable = null;

    try
    {
        ITcAdsSymbol itc = PLC3.adsClient.ReadSymbolInfo(Mnemonic);
        long indexGroup = itc.IndexGroup; ;
        long indexOffset = itc.IndexOffset;

        int[] args = { ArrLength }; 
        TempVariable = (Int16[])PLC3.adsClient.ReadAny(indexGroup, indexOffset, typeof(Int16[]) , args);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, Mnemonic);
    }
    return TempVariable;
}

but if I want to read array of string I get some exception: "Unable to marshal type. Parameter Name: type" in bold place: ...adsClient.ReadAny(indexGroup, indexOffset, typeof(string[]), args);

public string[] ReadArrFromPLC_String(string Mnemonic, int ArrLength)
{
    string[] TempVariable = null;

    try
    {
        ITcAdsSymbol itc = PLC3.adsClient.ReadSymbolInfo(Mnemonic);
        long indexGroup = itc.IndexGroup; ;
        long indexOffset = itc.IndexOffset;

        int[] args = { ArrLength };
        TempVariable = (string[])PLC3.adsClient.ReadAny(indexGroup, indexOffset, typeof(string[]), args);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, Mnemonic);
    }
    return TempVariable;
}

I can read the array in loop but it take long time.

I found similar topic: MarshalAsAttribute array of strings but I don't know does it help me? and I don't know how I can use it in my case.

3
  • Are you sure that it shouldn't be an array of characters? Commented Dec 2, 2014 at 12:22
  • 1
    ReadAny() demands that a string is declared with [MarshalAs] to give the string a fixed size. That prevents arrays of strings from working. Best you could possibly do is declare a struct with each member a string. Commented Dec 2, 2014 at 12:40
  • In PLC I declared an array "prg : ARRAY[0..200] OF STRING;" - with standard length string = 80 character. (I can declare the array "prg : ARRAY[0..200] OF STRING[20];" - with 20 characters in each string) The array have 201 elements. Could you write me how create this structure with [MarshalAs]? I don't know how use it. Commented Dec 2, 2014 at 13:08

1 Answer 1

2

Thank you Hans Passant for the tip.

Below I show method - it works correctly, but I had to use loop in order to rewrite text from struct to string[].

Is could be something to add?

    [StructLayout(LayoutKind.Sequential)]
    public struct MyString
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)] 
        //SizeConst = 21 because in my PLC I declared 
        //"prg : ARRAY[0..200] OF STRING[20];" 
        //(0 to 20 = 21 characters)
        public string Str;
    }
    public string[] ReadArrFromPLC_String(string Mnemonic, int ArrLength)
    {
        MyString[] StrArrFromPLC = null;
        try
        {
            ITcAdsSymbol itc = PLC3.adsClient.ReadSymbolInfo(Mnemonic);
            long indexGroup = itc.IndexGroup; ;
            long indexOffset = itc.IndexOffset;
            int[] args = { ArrLength };
            StrArrFromPLC = (MyString[])PLC3.adsClient.ReadAny(indexGroup, indexOffset, typeof(MyString[]), args);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Mnemonic);
        }
        string[] TempVariable = new string[StrArrFromPLC.Length];

        for(int i = 0; i < StrArrFromPLC.Length; i++)
        {
            TempVariable[i] = StrArrFromPLC[i].Str;
        }
        return TempVariable;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.