Hi,
i’am creating a C# function block which should read some values of a buffer created with the WORD_TO_BUF function block.
The output from the WORD_TO_BUF is an ARRAY OF BYTE and this ARRAY i want to connect with the input of my new C# function block.
So i tried to declare it as :
But how can I covert the Any-Variable to a IO.Stream or a byte-Array?
Or is there another way to get the single bytes out of the Any-Variable?
Thanks for helping me!
If you want a function block written in C# to accept an array as an input, you can follow this example in Github: CSharpExamples/UserArray.md at master · PLCnext/CSharpExamplesCollection of various C# sample code for PLCnext Technology controllers. - CSharpExamples/UserArray.md at master · PLCnext/CSharpExamples
Hallo Martin,
thanks for the fast answer. I found this solution, but if i define an array in the C# function block i can only connect a variable with exact this DataType. What i need is the following:
I have a MODBUS_TCP_SERVER and I want to connect the variable of the Holding Registers(which ist an ARRAY OF WORD) with my new function block:
but as you can see above => i can’t connect the variable of the modbus.
I’m sure the is a solution for my effort, but I can’t see it => would be nice if you could help me!
Thanks a lot!
That’s an interesting problem.
You’re right, PLCnext Engineer will not copy data between two arrays if their type names are different, even if those types have the same underlying data type. So even in pure IEC 61131 code (without C#), this does not work: image.pngimage.pngimage.pngOne option would be to use the MEMORY_COPY function to copy data from a1 to a2. This isn’t very efficient (especially for very large arrays like you are using), because if a2 is then used as an input variable on a function block instance, all the data will be copied again into the function block instance.
There is a quirk in PLCnext Engineer that might provide a solution in your case:
If a PLCnext Engineer project contains two data type definitions with the same name and the same underlying type, then PLCnext Engineer will not complain about this. So, if you change the name of the array type in your C# function block from CRUSH_ARRAY_OF_WORD to MB_TCP_ARR_W_0_65535, then you will be able to connect the variable MB_SERVER_A_HR_DATA directly to the input on your function block.
One of the side-effects of this “feature” is that the Type drop-down list in variable tables will contain duplicate entries, and it is impossible to know which one is the definition from the Modbus library, and which one is from your own library. But it seems to make no difference which one you choose. image.pngI hope that something here helps.