Hello Guest it is July 17, 2025, 09:53:01 PM

Author Topic: mcSignalWait doesn't work but mcSignalGetState does?  (Read 9320 times)

0 Members and 1 Guest are viewing this topic.

mcSignalWait doesn't work but mcSignalGetState does?
« on: July 08, 2024, 01:21:27 PM »
Hi all:

I'm working on getting my toolchanger integrated and I'm having an issue with the mcSignalWait command:

I have a much larger body of code, but this is the minimal chunk that shows the issue.   I am trying to wait on an input signal that originates from a modbus coil.  All of the UI elements tied to the coil operate just fine (led status).  The mcSignalWait API call does not work, yet the mcSignalGetState does.   

To test the script,  I have a UI button linked to the following script that sends
Code: [Select]
local inst = mc.mcGetInstance()
-- Check to make sure that TC is ready (req complete)
--rc = mc.mcSignalWait(inst, mc.ISIG_INPUT43, mc.WAIT_MODE_HIGH, 5) --Wait 5 seconds for req complete to become active
rc = mc.mcSignalGetState(inst, mc.ISIG_INPUT43)
if (rc~= 0) then --Check our return call
mc.mcCntlSetLastError(inst, "rc = 0 There was an error: Request Complete Timeout")
else
mc.mcCntlSetLastError(inst, "rc = 1 Request Complete")

local hTcOutCMD, rc -- Command Out
hTcOutCMD, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT92) -- Get handle for output 92: outgoing command
rc = mc.mcSignalSetState(hTcOutCMD, 1) -- Outgoing Command
if (rc~= 0) then --Check our return call
mc.mcCntlSetLastError(inst, "There was an error: Set Outgoing Command")
end
end

The script runs just fine, reports "request complete" and triggers the output coil when I use "mc.mcSignalGetState(inst, mc.ISIG_INPUT43)", but when I try "mc.mcSignalWait(inst, mc.ISIG_INPUT43, mc.WAIT_MODE_HIGH, 5)" it only gives me the "mc.mcCntlSetLastError(inst, "rc = 0 There was an error: Request Complete Timeout")" error.

I have tried many permutations of this command, all to the same effect:
mc.mcSignalWait(inst, mc.ISIG_INPUT43, WAIT_MODE_HIGH, 5)
mc.mcSignalWait(inst, mc.ISIG_INPUT43, 1, 5)

I have also tried the mcSignalHandleWait command, but it also behaves the same.

Am I missing something?

Ultimately, the final code will go into an m6 macro, so if there is a workaround that could work there instead (G-code?) I'm open to suggestions.

FWIW, I'm running M4 ver 4.2.0.5255
Re: mcSignalWait doesn't work but mcSignalGetState does?
« Reply #1 on: July 09, 2024, 02:51:14 PM »
It is working for me.. it is best to check the actual value of the return code. This will help to identify what is going wrong. At first I was getting -31 return code which is mc.MERROR_NOT_ENABLED.. so make sure you have Mach4 enable before testing this otherwise you will get the same error.

Just a note, you have your debug messages incorrect. When rc is not equal to 0 you are writing "rc = 0......"
Re: mcSignalWait doesn't work but mcSignalGetState does?
« Reply #2 on: July 09, 2024, 04:20:03 PM »
Oh interesting! I was definitely not testing with the machine enabled.  I’ll have to try that.

I totally missed the ‘~’ in that expression.  You’re right!  But the logic is still correct though i think.

I’ll mod the debug message to show the actual rc value so i can see what is happening as well.

Thanks!
Re: mcSignalWait doesn't work but mcSignalGetState does?
« Reply #3 on: July 10, 2024, 12:23:49 PM »
Perfect.  the enable was the issue all along.

Incidentally, I can now get the "MERROR_NOT_ENABLED" message.  Kinda clunky the way I did it (no switch case in LUA?), but I'll post it to help anyone else who comes across this

Code: [Select]
local inst, rc
inst = mc.mcGetInstance()

-- Check to make sure that TC is ready (req complete)
rc = mc.mcSignalWait(inst, mc.ISIG_INPUT43, mc.WAIT_MODE_HIGH, 5) --Wait 5 seconds for req complete to become active
--rc = mc.mcSignalGetState(inst, mc.ISIG_INPUT43) --To directly test the input signal
if (rc == 0) then
mc.mcCntlSetLastError(inst, "There was no error: ")--// Signal changed state from low to high
end

if (rc == -40) then
mc.mcCntlSetLastError(inst, "MERROR_TIMED_OUT")--// The signal didn't change state in the alotted time.
end

if (rc == -31) then
mc.mcCntlSetLastError(inst, "MERROR_NOT_ENABLED")--// The control was not enabled at the time of the
end

if (rc == -2) then
mc.mcCntlSetLastError(inst, "MERROR_INVALID_ARG")--// There was an invalid argument
end



if (rc== 0) then --Check our return call
-- if (rc~= 0) then --Check our return call
-- mc.mcCntlSetLastError(inst, "There was an error: rc= ") mc.mcCntlSetLastError(inst, rc)
-- else
mc.mcCntlSetLastError(inst, "rc = 0 Request Complete")

local hTcOutCMD, rc -- Command Out
hTcOutCMD, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT92) -- Get handle for output 92: outgoing command
rc = mc.mcSignalSetState(hTcOutCMD, 1) -- Outgoing Command
if (rc~= 0) then --Check our return call
mc.mcCntlSetLastError(inst, "There was an error: Set Outgoing Command")
end
end

Thanks again!
« Last Edit: July 10, 2024, 12:26:15 PM by brianthechemist »