I'm creating a software package that takes 16 boolean values at a time, puts them into an array of bits, then sends them off to a PLC to be unpacked and used by the PLC. My question is two-fold:
1) If I make a BitArray(16), will it only occupy 16 bits of data or does the BitArray object automatically include extra data in there as part of the object (I assume it's only 16 bits but can't find much real good confirmation on it so far) and
2) The PLCs are expecting a 16-bit integer (short) for the communication coupler to work as necessary, therefor I figured I could simply cast the BitArray into a short, but I need a list of 1's and 0's, not "true" and "false" - so what would be the best way to do this? Some sample code of what I have in mind below:
/* Pack bits into a container to send them as one item */
var bitContainer_1 = new BitArray(16);
bitContainer_1[0] = result.Toggle_Signal;
bitContainer_1[1] = result.Auto_RFO;
bitContainer_1[2] = result.Spare_1;
bitContainer_1[3] = result.Spare_2;
bitContainer_1[4] = result.HMI_Horn_Necessary;
bitContainer_1[5] = result.HMI_Horn_Urgent;
bitContainer_1[6] = result.HMI_Horn_Emergency;
bitContainer_1[7] = result.Spare_3;
bitContainer_1[8] = result.Sys_1_Auto_Ready;
bitContainer_1[9] = result.Sys_1_Auto_Active;
bitContainer_1[10] = result.Spare_4;
bitContainer_1[11] = result.Spare_5;
bitContainer_1[12] = result.Sys_2_Auto_Ready;
bitContainer_1[13] = result.Sys_2_Auto_Active;
bitContainer_1[14] = result.Spare_6;
bitContainer_1[15] = result.Spare_7;
short packagedBits_1 = Convert.ToInt16(bitContainer_1);
I assumed I could do something to the booleans before going into bitContainer
to cast them to 1 or 0 and found out that didn't seem to work, so I wanted to see what any/all my options may be while avoiding lots of if(true){var = 1} else{var = 0}
type scenarios since I'm rather unfamiliar with this type of task. As these communications will be in charge of machine behaviors, reducing overhead and time to package these & send them is vital.