Beginner in the whole PLC stuff, so corrections are welcome.
I am trying to tidy up my project and current situation is thus: I receive 16 byte arrays from modbus. These act as buttons, lights, conveyors what have you in Factory IO.
GAB_FactoryIO_Inputs AT %I* : ARRAY [0..15] OF BYTE;
GAB_FactoryIO_Outputs AT %Q* : ARRAY [0..15] OF BYTE;
So instead of referring to "Start button" with its bit "IO.GAB_FactoryIO_Inputs[0].0" I made a clumsy conversion POU and GVL to go through each bit and give them a new name. So it currently it looks like 200 lines of this:
IO.iSensor10_Capa := IO.GAB_FactoryIO_Inputs[7].3;
IO.iSensor9_Capa := IO.GAB_FactoryIO_Inputs[7].4;
IO.iPositioner_Limit := IO.GAB_FactoryIO_Inputs[7].5;
IO.iPositioner_Clamped := IO.GAB_FactoryIO_Inputs[7].6;
IO.iPick2_Detected := IO.GAB_FactoryIO_Inputs[7].7;
IO.iPick2_MovX := IO.GAB_FactoryIO_Inputs[8].0;
IO.iPick2_MovZ := IO.GAB_FactoryIO_Inputs[8].1;
IO.iPick2_Rot := IO.GAB_FactoryIO_Inputs[8].2;
IO.iPick2_GripRot := IO.GAB_FactoryIO_Inputs[8].3;
And this
iPositioner_Limit : BOOL;
iPositioner_Clamped : BOOL;
iPick2_Detected : BOOL;
iPick2_MovX : BOOL;
iPick2_MovZ : BOOL;
iPick2_Rot : BOOL;
iPick2_GripRot : BOOL;
It all works as it should, but I cant help to feel its amateurish, unwieldy and slows things down.
I've read about structures, enumeration and alias, and thought structures would be my savior by handily arranging them inside "cabinets", like so:
stCNC.Button1
stCNC.Button3
stCNC.Sensor1
And hidden inside structures would be the conversion between stCNC Sensor1 = IO.GAB_FactoryIO_Inputs[9].4;
But for some reason that doesnt work at all. I'm most likely going at it plain wrong angle, but have no idea what to look for next.
EDIT work in progress thus far. Seems like I got the hang of the basics. @kolyur had simple enough example to follow, so I started from there, and progressed towards @Steve and @YAVA examples:
//sending inputs to GVL FactoryIO_Inputs AT %I* : ARRAY [0..15] OF BYTE;
fbMBReadInputs(pDestAddr := ADR(IO.FactoryIO_Inputs),
//in GVL IO
FactoryIO_Inputs AT %I* : U_UNION2;
//inside S_LIGHTS:
TYPE S_LIGHTS :
STRUCT
LIGHT0 : BIT;
LIGHT1 : BIT;
LIGHT2 : BIT;
LIGHT3 : BIT;
LIGHT4 : BIT;
LIGHT5 : BIT;
LIGHT6 : BIT;
LIGHT7 : BIT;
END_STRUCT
END_TYPE
//inside U_UNION1
TYPE U_UNION1 :
UNION
nArray : ARRAY[0..15] OF BYTE;
sName : S_NAME;
//Then instantiating in POU
VAR
sLights : S_LIGHTS;
---
sLights.LIGHT1 := TRUE;
You can try to use a UNION. It is basically an "overlay" variable you can place over another variable.
TYPE Test :
UNION
nARRAY : ARRAY[0..15] OF BYTE;
sHumanReadable : sStruct;
END_UNION
END_TYPE