birkenfeld / ads-rs

Rust crate to access PLCs via the Beckhoff ADS protocol
https://crates.io/crates/ads
Apache License 2.0
45 stars 8 forks source link

Decoding and flattening of struct array - question #10

Closed alex-boring closed 1 year ago

alex-boring commented 2 years ago

Hey, the library is working great so far!

One question I have when using it that you might be able to answer: do you have an example on how to decode (and flatten) arrays, especially struct arrays using the library?

Example Struct:

TYPE ST_Test:
STRUCT
    IN  : ST_Test_IO_IN;
    OUT : ST_Test_IO_OUT;
        Test    : LREAL;
END_STRUCT
END_TYPE

Example Usage in MAIN Program:

Test    : ARRAY[0..9] OF ST_Test;

Getting the nested symbol information for non-array symbols works like a charm with your recursive function in the examples. Using an adjusted version of your function I am getting down to the Array level like this, but not further. Example print:

Name: GVL_MainGlobals.TestParams Level: 1 Typ: ARRAY [1..6] OF ST_Test Size: 2650

Now when I am using the Rust app to get all the information on the symbols in a struct array it would be great to get a flattened array like this that I can work with and forward. How the print could ideally look like:

Name: GVL_MainGlobals.TestParams[0].IN.whatever  Level: 3  Typ: T_MaxString  Size: 256
Name: GVL_MainGlobals.TestParams[0].OUT.someLightOn  Level: 3  Typ: BOOL Size: 1
Name: GVL_MainGlobals.TestParams[0].Test  Level: 2  Typ: LREAL Size: 8
.
.
.
Name: GVL_MainGlobals.TestParams[9].IN.whatever  Level: 3  Typ: T_MaxString  Size: 256
Name: GVL_MainGlobals.TestParams[9].OUT.someLightOn  Level: 3  Typ: BOOL Size: 1
Name: GVL_MainGlobals.TestParams[9].Test  Level: 2  Typ: LREAL Size: 8

This becomes even trickier for multi dimensional arrays I assume.

Thanks in advance, Alex

birkenfeld commented 2 years ago

If I understand correctly, you need to look at the array member of the type info struct to get the lower and upper bounds. Then you can go through each index and recurse through the members.

birkenfeld commented 1 year ago

I'm closing this for now, feel free to reopen if you are not satisfied.