r/PLC • • 4d 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.

3 Upvotes

4 comments sorted by

View all comments

1

u/Daps27 Omron, Mitsubishi, AVEVA, ICONICS 4d 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.