Help Pushing Binary Into tag bits

I am working on a device that is hooked up to our PLC through ethernet/IP and an EDS file. The device works by taking the pressure you want and converting it to binary (through an formula in the manual). Once you have the binary value, you need to input the value into two different tags bits to get the desired output.
I have everything set up to get the number switched into binary but now I don’t know how to input that into the tag bits.. any ideas?

Need “toBinary” value above to be insert into tag bits like picture below:

Let me know if there is anything more I can clarify to help.

Share the details. This looks like basic shift/mask operations.

Which details are you looking for me to share? I’d be happy to share anything you’d need me to.

How did you compute the binary? And know what bits to target?

(Share the manual, perhaps.)

1 Like

There should be no need to write to individual bits in the PLC if you're writing what amounts to a value.

Yes, I'm going on a limb here but... Is this calculation right? It's looks like Byte 1 is supposed to be the integer and byte 2 is the decimal? If so, you are missing bit 5 on Byte1.

Also if so:
Option 1: make two integer expressions and write them separately to each SINT:

floor({[.]Desired Pressure)

({[.]Desired Pressure)-floor({[.]Desired Pressure))*10

Option 2: Write the full calculated integer to a UINT on the PLC and COPy it to Reg1:O and let the PLC handle the distribution.
({[.]Desired Pressure)-floor({[.]Desired Pressure))*2560 + floor({[.]Desired Pressure)

Here is the equation from the manual as well as the portion that points out what goes to what bit:

I can get you the full manual if you wanted to read through it as well.

Tell us what brand and model of PLC it is. Don't be embarrassed about it!

Probably this: https://content2.smcetech.com/pdf/manuals/IN19856.pdf

Assuming that the toBinary conversion is correct, then in the Tag valueChanged event you can use code similar to the following:

revBin = int(bin(currentValue.value)[2:][::-1].ljust(16,'0'),2)
byte2 = revBin & 0xFF
byte1 = revBin >> 8 & 0xFF

system.tag.writeAsync(['paths to each byte'],[byte1,byte2])

I am using an Allen Bradley CompactLogix 5069-L0306ERM

Yes, that is the full manual, thanks!

Rather than trying to write individual bits from Ignition I'd just write the integer value across into a PLC temp tag first and then have the PLC logic assign the bitwise logic into your final tag.

Concur. Just arrange to copy an INT into the first two bytes. The manual implies the high bits are ignored. Use Ignition's linear tag scaling to do the conversion to 0-4095 for you, and enable the clamping option.

1 Like

I was able to get the two INTs to copy into the first two bytes by converting the results of the formula (({[.]Desired Pressure}/130.534)*4095)
into binary using this expression on a tag:
(replace(stringFormat("%12s", toBinary({[.]CountFormula})), " ", "0"))

Then I did a little more magic to break the binary up in the two INTs. Then I convert the broken up binary back into a number to put into the each of the SINTs, but it seems that the number I am getting is out of range for the SINT..

Desired Pressure = User Input
Count Formula = formula from manual
toBinary = Converts value from CountFormula in Binary
ReverseBinary = Reverses order to have it line up with correct bits in PLC
Output1Binary/Output2Binary = The broken up binary for each SINT
fromBinary1/fromBinary2 = Converts the broken up binary into a number

The number I am getting on fromBinary1 is out of range for the SINT:

8 bit SINT can only be 127 to -128 because you use one bit for the sign.

10000100 would be -120 rather than 136 so you need to check something along the lines.
Edit: misplaced a zero. 10000100 would be -124 rather than 132.

I am using the fromBinary() function so it should be converting it to the correct binary number.. I am just struggling to get it to convert that SINT within that range.

It's because they're 8-bit SINT's.

132 as a UInt8 is 10000100
132 as a SInt8 cannot be expressed because a Sint8 can only be 127 to -128
132 as a Sint16 is 00000000 10000100

The from Binary function is using 16 bit values which is why it's giving you 132. But the PLC is using an 8-bit SInt 8. If you want 10000100 as a SInt8 then it's value in decimal is -124