Jython bin() Not Working

in need to use scripting to do bit manipulation from a DINT
I cannot get the jython bin() to work. Is there another version of this in scripting?

from java.lang import Integer

print Integer.toBinaryString(28)

Are you actually using the string? If you are just testing or manipulating the bits, jython’s & and | operators will be much more efficient.

What I am doing is using Ignition to maintain and program ‘pin charts’ that will be downloaded to a PLC.
The pin charts contains an array of [150,3] DINTs stored in a SQL database table
I use a table component to display the SQL table, then select a row and column to edit.
I need to capture the DINT, display and manipulate bits and store the DINT back into the database table.

The expression getbit() works for displaying, but there is no way to write a bit into an OPC Tag that is internal.

Correct. But jython bitwise math can do it for you:

# Set bit 23 and reset bit 20 of a dint
tagpath = '[default]Some/Path/To/Dint'
i32 = system.tag.read(tagpath).value
i32 = i32 | 0x00800000
# or i32 = i32 | (1 << 23)
i32 = i32 & (~ 0x00100000)
system.tag.write(tagpath, i32)
1 Like

Fantastic, you gave me the right path to follow:
I have toggle script to a momentary button and custom properties set up to show status of bit:

1 Like

This is what I am trying to do, have it working so far, just need button and scripting to get manipulated bits back into data base table:

1 Like

That’s a sweet UI !

{ dummy text for min length }

Bits.proj (33.8 KB)

Create a custom property called OutValue with the expression binEnc(bit0, bit1, bit2, …)
Then add scripting to update the value in the table.

if event.propertyName == "OutValue":
	if event.newValue != event.source.MyValue:
		data = event.source.parent.getComponent('Power Table').data
		selectedRow = event.source.parent.getComponent('Power Table').selectedRow
		data = system.dataset.setValue(data,selectedRow,"outs",event.newValue)
		event.source.parent.getComponent('Power Table').data = data