Reusable function to toggle bit
Posted: Mon Jul 24, 2017 10:20 pm
Hello,
I know from previous posts that I can toggle a bit in a tag using:
Globals.Tags.myvar.Value ^= 1 << 5;
I would like to be able to do this using reusable code which can be called using the "Run Script" action when I click on a button. I have put the following into a Script Module:
public int ToggleBit(int CurrentValue, int BitNumber)
{
CurrentValue ^= 1<<BitNumber;
string msg = CurrentValue.ToString();
MessageBox.Show (msg);
return CurrentValue;
}
When I run this on the development PC it reads the tag, modifies its value and shows the modified value in the message box, but it does not modify the tag value in the PLC.
What am I doing wrong?
Thank you in advance for your assistance.
Matt
I know from previous posts that I can toggle a bit in a tag using:
Globals.Tags.myvar.Value ^= 1 << 5;
I would like to be able to do this using reusable code which can be called using the "Run Script" action when I click on a button. I have put the following into a Script Module:
public int ToggleBit(int CurrentValue, int BitNumber)
{
CurrentValue ^= 1<<BitNumber;
string msg = CurrentValue.ToString();
MessageBox.Show (msg);
return CurrentValue;
}
When I run this on the development PC it reads the tag, modifies its value and shows the modified value in the message box, but it does not modify the tag value in the PLC.
What am I doing wrong?
Thank you in advance for your assistance.
Matt