2

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.

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Alex Watts
  • 537
  • 8
  • 27
  • 2
    You can always use an enum and mark it as flags: http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c – Gusman Mar 10 '16 at 16:27
  • 1
    For 2. http://stackoverflow.com/questions/5283180/how-can-i-convert-bitarray-to-single-int – walpen Mar 10 '16 at 16:27
  • A colleague of mine is always oozing over enums and I never even thought to use them haha. These both look like some good ground to go on - I'll try it out and update the post when I find out what works best, thanks! – Alex Watts Mar 10 '16 at 16:34

1 Answers1

2

1) BitArry(16) takes up more space than 16 bits. Try a ushort for something that takes exactly 16 bits.

2) If you want speed I would recommend you used a ushort and performed bitwise operations on it. You could set it like this:

    bitContainer = (ushort)(result.Spare_7 << 15 | result.Spare_6 << 14...)

then you can set or reset each bit as you required.

Edit: The bitwise OR operator '|' is used here to chain each entry bit together into the final ushort variable. The '<<' operator shifts a value n number of bits to the left, so this is bringing in each 1 or 0 in result.xxxx to it's correct place in bitContainer.

Tony68000
  • 303
  • 1
  • 7
  • I'm a bit inexperienced with bitwise operations, but does this solution not just compare values more so than actually copy them into a solid data type I can send? I basically want to end up with a string (not 'String' necessarily) of 1100101101111010 inside of my, in this case, ushort. That way, the PLC-side can parse it and take it apart and know which bit belongs to what (mapping scheme is setup outside of my questions posted here) – Alex Watts Mar 10 '16 at 16:56
  • 1
    The bitwise OR operator '|' is used to 'chain' each of those bit values together. The << operator shifts the bit to it's correct place in the ushort. So this will actually give you 2 bytes of bits as you want. Assuming result.Spare_7,etc is 1 or 0. – Tony68000 Mar 10 '16 at 17:04
  • OK. Was doing some looking up on bitwise operations and the way MSDN words their explanations is sometimes... misleading. Thanks! – Alex Watts Mar 10 '16 at 17:15