Hello Guest it is May 12, 2021, 06:55:48 PM

Author Topic: Lua Script and reading register's Help  (Read 671 times)

0 Members and 1 Guest are viewing this topic.

Offline gorf23

*
  •  165 165
    • View Profile
Lua Script and reading register's Help
« on: January 06, 2021, 12:04:47 PM »
Not sure if this is allowed in a lua script, but if i run this code i always get a NUL value returned for hreg value
If i run the code with no if statement it runs fine.. i have placed both reggethandles with no if statement and worked fine.
So question is why is the if statement messing up the value retuned for the hreg value inside the if ?

Code: [Select]
local inst = mc.mcGetInstance()
local PoKeysreg = mc.mcRegGetHandle(inst,"PoKeys_40548/DeviceConnected") -- The Pokeys Probe register value's
local PoKeys = mc.mcRegGetValue(PoKeysreg)
if ( PoKeys == 1 ) then
local hreg = mc.mcRegGetHandle(inst,"PoKeys_40548/ProbeStatus") -- The Pokeys Probe register value's
        mc.mcCntlSetLastError(inst, "Connected to Pokeys") 
end

Thanks gary
Re: Lua Script and reading register's Help
« Reply #1 on: January 06, 2021, 06:45:10 PM »
Do a tonumber() on the Pokeys variable in your if statement. Lua will not guess the data type correctly every time .
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com

Offline gorf23

*
  •  165 165
    • View Profile
Re: Lua Script and reading register's Help
« Reply #2 on: January 07, 2021, 01:07:10 PM »
Brian

Thanks that worked, its funny how i make something so easy, so hard...

Gary
Re: Lua Script and reading register's Help
« Reply #3 on: January 07, 2021, 01:15:02 PM »
I hear you brother! How do you think I knew what to do! LOL We are all in this together.
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com

Offline gorf23

*
  •  165 165
    • View Profile
Re: Lua Script and reading register's Help
« Reply #4 on: January 07, 2021, 01:16:15 PM »
Brian

Before i forget, is there any major changes to mach4 that would crash every time i exit the screen editor, i created a screen a while back, and now if i go to edit it when i exit the editor mach4 crashes and shuts down to desktop i have to reboot mach4 every time.. it started 4 or 5 versions ago... if i install mach4 a few versions ago then it works fine again.

Thanks gary
Re: Lua Script and reading register's Help
« Reply #5 on: January 07, 2021, 01:23:31 PM »
Well I don't know. You may be doing something we are not thinking about. I can look at it if you like . brianb at machsupport.com is where you can send it. If I need any dependents please send them as well.

Thanks
Brian
Fixing problems one post at a time ;)

www.newfangledsolutions.com
www.machsupport.com
Re: Lua Script and reading register's Help
« Reply #6 on: January 07, 2021, 04:24:22 PM »
Hi,
one of the things I like about Lua is that it is largely type free.....one of the things I hate about Lua is that its largely type free, and you can
never be sure what its going to do next!

Craig
My wife left with my best friend...
     and I miss him!

Offline gorf23

*
  •  165 165
    • View Profile
Re: Lua Script and reading register's Help
« Reply #7 on: January 21, 2021, 11:36:50 AM »
I just got back to the pokeys and smoothstepper register's and i guess i was wrong before, i am still getting a Null value when i use the if then statement..
Here is the code if i remove the if i get no null error.. all messagebox's print ok its the last one after the if then that error's with a null value..
it's being run in a modular. and called with a button press..
 
Code: [Select]
local Pokeysreg  = mc.mcRegGetHandle(inst,"PoKeys_40548/DeviceConnected") -- The Pokeys57CNC is Connected
local Phreg = mc.mcRegGetValue(Pokeysreg)
tonumber(Phreg)
wx.wxMessageBox("Pokeys Connected =  "..Phreg)

local Esshreg  = mc.mcRegGetHandle(inst,"ESS/Connected") -- The Ess Smoothstepper is Connected
local Ehreg = mc.mcRegGetValue(Esshreg)
tonumber(Ehreg)
wx.wxMessageBox("ESS Connected =  "..Ehreg)

if (Phreg == 1) then
local hreg  = mc.mcRegGetHandle(inst,"PoKeys_40548/ProbeStatus") -- The Pokeys57CNC Probing register
elseif (Ehreg == 1) then
local hreg  = mc.mcRegGetHandle(inst,"ESS/Probing_State") -- The ESS Smoothstepper Probing register
    tonumber(hreg)
    end

wx.wxMessageBox("Pokeys ProbeStatus =  "..hreg)

Thanks gary
Re: Lua Script and reading register's Help
« Reply #8 on: January 21, 2021, 12:44:51 PM »
The local variables scope is limited to within that if statement.

You could do something like this...
Code: [Select]
local hreg = 0
if (Phreg == 1) then
    hreg  = mc.mcRegGetHandle(inst,"PoKeys_40548/ProbeStatus") -- The Pokeys57CNC Probing register
elseif (Ehreg == 1) then
    hreg  = mc.mcRegGetHandle(inst,"ESS/Probing_State")
end

Offline gorf23

*
  •  165 165
    • View Profile
Re: Lua Script and reading register's Help
« Reply #9 on: January 21, 2021, 03:38:57 PM »
Thanks

That seems to stop the null error, But if i run a G31 after the if statement and then do a mc.mcRegGetValue(hreg) to see if the probe was not hit, the  pokeys should return a -4 and the ESS should return a -1 not hit, it always returns a 0.0 if probe not hit, if i remove the if statement and other code, and only have hreg = mc.mcRegGetHandle(inst,"PoKeys_40548/ProbeStatus"). then G31, and then get the value from hreg, it works fine returns the correct values..

Not really sure where the problem is, a lua bug?

Thanks gary