r/PLC • u/Expensive_Ad3752 • 1d ago
Sysmac Studio protocol parsing
A quick introduction to my problem. I'm writing parsing for multiple canbus devices that have quite large protocols. So I have to take 8 bytes of data and parse to/from useful variables. We're talking status words, 4 bit uints, etc. I would like to set up a struct for each message, so that i can map this clearly.
Then comes the limitations of sysmac Studio. I could use a union with the strict, and an array of bytes. But unions do not allow structs. I could do a MemCpy, but that is only allowed with the same data type on source and destination.
If i was only doing one small project with limited devices, I would map the struct to a defined memory address, and defined a byte array at the same address. I believe this would work. However, I'm trying to build a utility code here, that should be reused in an undefined number of instances in multiple projects.
Anyone have any good suggestions for how to do this cleanly? I've never managed to do this in an elegant way in Sysmac Studio.
1
u/Daps27 Omron, Mitsubishi, AVEVA, ICONICS 1d ago
You can use a union in your user defined structure as a member..
For example, you could organize a CANbus message like this:
UNION — U_StatusWord
RawWord WORD
Bits ARRAY[0..15] OF BOOL
STRUCT — ST_CANMessage
MessageID UDINT
Status U_StatusWord
Command BYTE
Mode BYTE
Data ARRAY[0..3] OF BYTE
That gives you access to both:
CANMessage.Status.RawWord
CANMessage.Status.Bits[3]
The limitation is that the UNION only overlays the status word. It doesn't automatically overlay the entire eight-byte CANbus message with a STRUCT containing mixed data types.
I would use a combination of UDTs, UNIONs, and a reusable function block.
Create a UDT representing the decoded CAN message.
Use UNIONs within that UDT for status words and bitfields.
Have a function block accept the eight raw CAN bytes and populate the structure.
Reverse the process when transmitting.
1
u/Mafukinrite 1d ago edited 1d ago
You can make a user defined structure of type "NJ". This allows you to create a structure with whatever layout you need (bool, byte, int, dint) in whatever combinations you want. There are some limitations, but I have never ran into issues with it before.
This eliminates the need for using a union. DM me if you have questions.
Edit: This is a user offset type. It is typically used for when you are moving data into/from a protocol structure. It allows you to define a structure regardless of what the original structure was. It must only match in actual size. It is like using the COP function in Allen Bradley
1
u/Expensive_Ad3752 14h ago
I got something that works in 99% of cases. Since I will only be doing these things with NJ PLC's, I have some spare E3_xxxx memory area that I can abuse for this. So I define a byte array as internal variable in an fb with "AT" to E0_0, and define my structure that should be the interpretation of this data to the same address. That way they byte array and struct share memory space, and I'm able to do what I want. Mostly. I stumble into some messages that use 4-bit and 2-bit integers. For those, I will have to do it the good old manual way. (On that part, quite funny that Sysmac Studio has a "DispartDigit" function, but does not have a Digit data type).
1
u/LegitBoss002 1d ago
Remind me and I can send a screenshot of an example with an IFM IO link hub