Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: dude1 on May 18, 2015, 02:55:20 AM
-
I have run into a problem with signal tables from the scripting manual in it it has the sig table like this
SignalTable = { -------sig table number goes here I think without {
[mc.ISIG_INPUT0] = function (on_off)
if (on_off == 1) then
mc.mcCntlCycleStart(inst)
end
end
I keep get a error this
Compilation error on line number: 54
Lua: Syntax error during pre-compilation
[string "C:\Users\Daniel\AppData\Local\Temp\le6115.mcc..."]:54: unexpected symbol near '['
I have searched but cant find anything to help I used this and it work`s
if (sig == mc.ISIG_INPUT1) and (state == 1) then
local inst = mc.mcGetInstance()
mc.mcCntlCycleStart(inst)
end
-
I have run into a problem with signal tables from the scripting manual in it it has the sig table like this
You got a link to this manual..
DazTheGas
-
http://www.machsupport.com/help-learning/product-manuals/
-
changed to this seems to work
SignalTable = {001, --sig table number goes here I think
[mc.ISIG_INPUT0] = function (on_off)
if (on_off == 1) then
mc.mcCntlCycleStart(inst)
end
end
then for this bit
if (SignalTable[001] ~= nil) then
SignalTable[001](on_off)
end
it cant find the input
also the editing is a bit out in the manual
-
in your first post, you are missing the closing "}" for the sig table.
Scott
-
yep found that the call not working now
if (SignalTable[001] ~= nil) then
SignalTable[001](on_off)
end
it cant find the table
-
right it goes like this.
SignalTable = {001, --your sig table number goes here it compiles and runs like this
[mc.ISIG_INPUT0] = function (on_off)
if (on_off == 1) then
mc.mcCntlCycleStart(inst) --name of button command
end
end
}
I think this is now correct if I am wrong someone comment please.
the only problem now when I run the other part of the code it cant find the signal table.
this is the other part
if (SignalTable[sig] ~= nil) then --I think sig is the number of the table
SignalTable[sig](state) --I think state is (on_off) or on_off == 1) like I have it below
end
if (SignalTable[001] ~= nil) then
SignalTable[001](on_off)
end
-
Could someone who understands Lua explain what is going on here? This reference (http://www.lua.org/pil/3.6.html) describes Lua table constructors. Based on that, it looks like this:
SignalTable = {001,
[mc.ISIG_INPUT0] = function (on_off)
if (on_off == 1) then
mc.mcCntlCycleStart(inst)
end
end
}
would construct a new table stored in SignalTable with [1] = 001 and [mc.ISIG_INPUT0] = function...
1. Why is the 001 needed?
2. If I want to add additional signals and functions, it seems as though I would need to add them to this SignalTable constructor inside the "{}", but in another post it said that the way to do that was to add additional constructors of the form:
SignalTable = {00n,
[mc.ISIG_INPUTn] = function (on_off)
if (on_off == 1) then
....
end
end
}
Wouldn't this overwrite the previous SignalTable declarations?
-
If you want to use the table version of this then your best place to add it is within the startup script, it is declared only once there and there are examples already there, this creates a table of signal check and what to do with them, this is then activated from the signal script the same as a function would be in the PLC.
DazTheGas
-
I followed your example and have F5 working for feed hold using the PLC script, which is delightful - thanks.
I would also like to see how it works using the signal table. What I have tried doesn't work and I don't see what is wrong.
At the end of the Screen Load script, I have:
SignalTable = {
[mc.ISIG_INPUT63] = function (on_off)
if (on_off == 1) then
local inst = mc.mcGetInstance();
mc.mcCntlFeedHold(inst);
end
end,
}
At the end of the Signal Script, I have:
if (SignalTable[mc.ISIG_INPUT63] ~= nil) then
SignalTable[mc.ISIG_INPUT63](state);
end
-
your second end
-
your second end
No doubt I've been looking at it too long, but I'm not seeing the problem with any of the end's.
-
end, should be end
-
According to http://www.lua.org/pil/3.6.html, trailing commas are valid in Lua. I tried deleting it anyway, but it didn't make a difference.
-
go back to how daz says to do it that example I posted was working when I tried it, they may have done some changes I can't get it to work properly.
-
You are trying to do it the old way, in your table your using the on_off but in your signal script your using state ,
Put your signal script back to how it was and use the already created table in the startup script and a signal like this
[mc.ISIG_INPUT63] = function (state)
if (state == 1) then
mc.mcCntlFeedHold(inst)
else
-- do something else or not
end
end,
DazTheGas
-
The name of the variable doesn't matter. It's a function parameter and can be called anything. But I tried changing it to "state" anyway. No difference.