0

im trying to read this data block from Siemens S300 PLC using s7netplus libary

and my mapped code struct looks like this

       [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
       public struct CIP_B
        {

           public bool Start_Req;
           public bool Start_Ack;
           public bool End_Req;
           public bool End_Ack;
           public bool Ended_OK;
           public bool Step_Interupted;
           public bool Value_Changed;
           public Byte CIP_OBJ;
           public Byte CIP_TYPE;
           [MarshalAs(UnmanagedType.U2, SizeConst = 16)]
           public ushort[] VCM_Setpoint;
           public Int16 Step_Num;
           public Int16 changedValue;
            [MarshalAs(UnmanagedType.U1, SizeConst = 10)]
           public Byte[] Spare;
}

I'm using this command to read the struct

  var res = (CIP_B)_plcClient.ReadStruct(typeof(CIP_B), 71, 0);

I read all other values besides the two arrays with no problem with the above command, but the two arrays always result as null

I also tried to create a constructor and initialize all the field there, but still, I'm getting the same result.

Is it even possible, how can I read the two arrays?

1 Answer 1

1

I haven't used C# with Siemens, but with Bechoff the array has to be marshalled as ByValArray.

Now you are saying that the VCM_Setpoint is 2 byte unsigned interger that is a size of 16, which sounds strange. Does the following work (array of ushort(WORD) of 16 values, am I right?)

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public ushort[] VCM_Setpoint;

//If you need to marshal the array subtype too, use the ArraySubtype
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.SET_TYPE_HERE, SizeConst = 16)]
public ushort[] VCM_Setpoint;

In IEC 61131-3 environment it would be the following

//C#
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public ushort[] VCM_Setpoint;

//PLC
VCM_Setpoint : ARRAY[0..15] OF WORD;
Sign up to request clarification or add additional context in comments.

5 Comments

VCM_Setpoint is an array of WORDS(2 BYTES) as you can see, i tried all the options with above with` UnmanagedType.I2` and UnmanagedType.H2 and still the same result. VCM_Setpoint returns as null, not an array of zero just null
Did you try that ByValArray? And if you did and it didn't work, did you try to set the ArraySubType to UnmanagedType.I2 or UnmanagedType.H2? What is the size of that array in PLC, 16?
I tried ByValArray with and without UnmanagedType.I2 or UnmanagedType.H2 and still returning null . and yes the size is 16(you can see that in the picture )
Oh yes, I see, didn't notice the picture. From this source I can see that the STRING is not supported inside struct. Perhaps arrays aren't supported either, since string works like a byte array. Sorry I couldn't help.
i looked at bit into the source code at it looks that you are right, there's no support for arrays. Thanks for the help anyway

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.