Handle TCP Modbus Arrays in C#

I use the Modbus TCP Server 4 (library Modbus TCP 7) and use the array arrModbusData also for another block.

In this block I read from the array and write to it. But I cannot access elements of the array as usual in C#. The data type of the array is MB_TCP_ARR_W_0_7167.
To my knowledge this corresponds to 16 bit → WORD in PLCNext and a ushort in C# The array ModBusData is defined for the other block in C# as follows
[InOut, DataType(“ANY”)]
public ushort ModBusData; The access with
ushort temp = ModBusData[0]
and writing with
ModBusData[1] = 0x0000
does not work and gives an error when writing the project.
The error messages are:
- Array boundaries out of valid range
- Runtime exception occurred on device How can this be done reading and writing to the ModBusData?

RegToken.PNG

Hello,

unfortunatly you can not connect arrays in the manner you are atempting.
Due to the nature of IECs static arrays getting C# and Eclr arrays to connect is a bit more complicated to setup.

Have a look here for one method:
https://github.com/PLCnext/CSharpExamples/blob/master/PLCnext_CSharpExamples/04_UserArray/UserArray.md
https://github.com/PLCnext/CSharpExamples/blob/master/PLCnext_CSharpExamples/04_UserArray/FBWithUserArray.cs
The out of Bounds happens because IEC can not be directly cast to ushort You can however also pass by reference i will add a howto later.

Also just browse around a little at https://github.com/PLCnext/CSharpExamples/blob/master/PLCnext_CSharp
You can find many more usefull examples there. kind regards,
Oliver

Hello, Thanks a lot. My given array arrModbusData has the default data type MB_TCP_ARR_W_0_7167.

How can I use this data type as well in the new function block? It is still the same array as from the TCP Modbus