Is there a function that puts (writes) a bit into an integer?
Thanks
Is there a function that puts (writes) a bit into an integer?
Thanks
Not sure where you are doing this, but you could just add them by converting the bit to an integer…
if by bit you mean binary…
int(‘bit’, 2)
ie: int(‘00000101’, 2)
Cheers,
Chris
Here is some additional information as to why I’m looking for this function.
I have 5 pumps at a water pump station that I need to start and stop (10 bits or binary values altogether). I want to write these bits to the local controller as a integer. The rest of the information that I’m reading and writing to the local controller are integers. This makes my communications easier to work with on the Master PLC.
Ok, then use Bit Maniputaion Operators.
I created a property to PumpNumber on the screen since you had 5 pumps.
This code assumes a couple things:
Put this on event handlers, action -> actionPerformed.
#Get Variables
pump = event.source.parent.PumpNumber
tempcommand = system.tag.read("Test/Pump_Command")
command = tempcommand.value
#Lets toggle the correct bits per pump.
if pump == 1:
newcommand = command^3 #Toggle both start / stop bits for Pump 1
elif pump == 2:
newcommand = command^12 #Toggle both start / stop bits for Pump 2
elif pump == 3:
newcommand = command^48 #Toggle both start / stop bits for Pump 3
elif pump == 4:
newcommand = command^192 #Toggle both start / stop bits for Pump 4
elif pump == 5:
newcommand = command^768 #Toggle both start / stop bits for Pump 5
else:
newcommand = command
system.gui.errorBox("No pump number selected")
#Lets write the new command.
system.tag.writeToTag('Test/Pump_Command', newcommand)
Since that code is too simple and I like checks…
I would use this code:
I did this as 1 button. This could be changed to two button system.
#Get Variables
pump = event.source.parent.PumpNumber
tempcommand = system.tag.read("Test/Pump_Command")
command = tempcommand.value
#This prevents errors if someone hits start/stop twice.
newcommand = command
#Lets map the bits per pump. (Start button)
if pump == 1:
checkstop = command&1
checkstart = command&2
if checkstop == 1 and checkstart == 2: #Pump is commanded to stop and start, toggle stop bit
newcommand = command^1
elif checkstop != 1 and checkstart != 2: #Pump is not commanded to start nor stop, toggle start bit
newcommand = command^2
else:
newcommand = command^3 #Toggle both Start / Stop bits.
elif pump == 2:
checkstop = command&4
checkstart = command&8
if checkstop == 4 and checkstart == 8: #Pump is commanded to stop and start, toggle stop bit
newcommand = command^4
elif checkstop != 4 and checkstart != 8: #Pump is not commanded to start nor stop, toggle start bit
newcommand = command^8
else:
newcommand = command^12 #Toggle both Start / Stop bits.
elif pump == 3:
checkstop = command&16
checkstart = command&32
if checkstop == 16 and checkstart == 32: #Pump is commanded to stop and start, toggle stop bit
newcommand = command^16
elif checkstop != 16 and checkstart != 32: #Pump is not commanded to start nor stop, toggle start bit
newcommand = command^32
else:
newcommand = command^48 #Toggle both Start / Stop bits.
elif pump == 4:
checkstop = command&64
checkstart = command&128
if checkstop == 64 and checkstart == 128: #Pump is commanded to stop and start, toggle stop bit
newcommand = command^64
elif checkstop != 64 and checkstart != 128: #Pump is not commanded to start nor stop, toggle start bit
newcommand = command^128
else:
newcommand = command^192 #Toggle both Start / Stop bits.
elif pump == 5:
checkstop = command&256
checkstart = command&512
if checkstop == 256 and checkstart == 512: #Pump is commanded to stop and start, toggle stop bit
newcommand = command^256
elif checkstop != 256 and checkstart != 512: #Pump is not commanded to start nor stop, toggle start bit
newcommand = command^512
else:
newcommand = command^768 #Toggle both Start / Stop bits.
else:
newcommand = command
system.gui.errorBox("No pump number selected")
#Lets write the command we need.
system.tag.writeToTag('Test/Pump_Command', newcommand)
I hope I explained what I am doing. Let me know if you have any questions.
Cheers,
Chris
Here is some cleaner code:
Start button:
#Get Variables
pump = event.source.parent.PumpNumber
command = system.tag.read("Test/Pump_Command").value
#Figure out the integer equivalent to the start / stop bits.
stop = 2**((pump*2)-2)
start = 2**((pump*2)-1)
#This prevents errors if someone hits start/stop twice.
newcommand = command
#Check if a pump is selected.
if pump > 0:
#Lets map the bits per pump. (Start button)
checkstop = command&stop
checkstart = command&start
if checkstop == stop and checkstart == start: #Pump is commanded to stop and start, toggle stop bit
newcommand = command^stop
elif checkstop != stop and checkstart != start: #Pump is not commanded to start nor stop, toggle start bit
newcommand = command^start
elif checkstop == stop and checkstart != start: #Pump is commanded to stop and not start. toggle both bits
newcommand = command^(stop+start)
else:
system.gui.errorBox("No pump number selected")
#Lets write the command we need.
system.tag.writeToTag('Test/Pump_Command', newcommand)
Stop Button:
#Get Variables
pump = event.source.parent.PumpNumber
command = system.tag.read("Test/Pump_Command").value
#Figure out the integer equivalent to the start / stop bits.
stop = 2**((pump*2)-2)
start = 2**((pump*2)-1)
#This prevents errors if someone hits start/stop twice.
newcommand = command
#Check if a pump is selected.
if pump > 0:
#Lets map the bits per pump. (Start button)
checkstop = command&stop
checkstart = command&start
if checkstop == stop and checkstart == start: #Pump is commanded to stop and start, toggle start bit
newcommand = command^start
elif checkstop != stop and checkstart != start: #Pump is not commanded to start nor stop, toggle stop bit
newcommand = command^stop
elif checkstop != stop and checkstart == start: #Pump is commanded to start and not stop. toggle both bits
newcommand = command^(stop+start)
else:
system.gui.errorBox("No pump number selected")
#Lets write the command we need.
system.tag.writeToTag('Test/Pump_Command', newcommand)
Cheers,
Chris