Hi Craig,
my reference was lua 5.3, so you are right, it is a version problem.
What I wanted to do:
12 input signals building 3 * 4 bit counters. The counters build the register variables JogXValue, JogYValue and JogZValue. Inputs 0 to 3 representing the JogXValue. Each input status change should add or remove its related bit (1, 2, 4, or
in the register variable.
Sample code for input 0 and 1:
[mc.ISIG_INPUT0] = function (state)
if (state == 1) then WriteiRegs0 ("JogXValue", GetiRegs0 ("JogXValue") | 0x1) -- add bit by or 0001
else WriteiRegs0 ("JogXValue", GetiRegs0 ("JogXValue") & 0xE) -- remove bit by and 1110
end
end,
[mc.ISIG_INPUT1] = function (state)
if (state == 1) then WriteiRegs0 ("JogXValue", GetiRegs0 ("JogXValue") | 0x2) -- add bit by or 0010
else WriteiRegs0 ("JogXValue", GetiRegs0 ("JogXValue") & 0xD) -- remove bit by and 1101
end
end,Without the bitwise operators I have to read all other 3 inputs of a set to get the actual state and build the value anew.
Sample code for input 0 and 1:
[mc.ISIG_INPUT0] = function (state)
local h_INPUT_Bit
local Val = 0
if (state == 1) then Val = 1 end -- Bit 1 = on
h_INPUT_Bit = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1)
Val = Val + (mc.mcSignalGetState(h_INPUT_Bit) * 2)
h_INPUT_Bit = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2)
Val = Val + (mc.mcSignalGetState(h_INPUT_Bit) * 4)
h_INPUT_Bit = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT3)
Val = Val + (mc.mcSignalGetState(h_INPUT_Bit) *
WriteiRegs0 ("JogXValue", Val)
end,
[mc.ISIG_INPUT1] = function (state)
local h_INPUT_Bit
local Val = 0
h_INPUT_Bit = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT0)
Val = Val + mc.mcSignalGetState(h_INPUT_Bit)
if (state == 1) then Val = Val + 2 end -- Bit 2 = on
h_INPUT_Bit = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2)
Val = Val + (mc.mcSignalGetState(h_INPUT_Bit) * 4)
h_INPUT_Bit = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT3)
Val = Val + (mc.mcSignalGetState(h_INPUT_Bit) *
WriteiRegs0 ("JogXValue", Val)
end,This is necessary for all 12 input signals. It works but doing it bitwise would save some CPU time and would be more readable.
You answered my question 1, I found one solution for my question 2 by myself. Maybe you have another suggestion.
CNCharly