Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: gorf23 on January 06, 2021, 12:04:47 PM

Title: Lua Script and reading register's Help
Post by: gorf23 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
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker 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 .
Title: Re: Lua Script and reading register's Help
Post by: gorf23 on January 07, 2021, 01:07:10 PM
Brian

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

Gary
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker 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.
Title: Re: Lua Script and reading register's Help
Post by: gorf23 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
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker 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
Title: Re: Lua Script and reading register's Help
Post by: joeaverage 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
Title: Re: Lua Script and reading register's Help
Post by: gorf23 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
Title: Re: Lua Script and reading register's Help
Post by: jbuehn 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
Title: Re: Lua Script and reading register's Help
Post by: gorf23 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
Title: Re: Lua Script and reading register's Help
Post by: jbuehn on January 21, 2021, 05:14:59 PM
What values are you getting for Phreg and Ehreg (from your message boxes)? My guess is you're not getting a valid reg handle (hreg) in your if statement, which causes mc.mcRegGetValue(hreg) to return 0.0.

Also, tonumber() returns the string converted to a number, so you'd need to do something like:
Code: [Select]
Phreg = tonumber(Phreg)
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker on January 23, 2021, 08:53:19 AM
Man I had a hard time finding this thread!
Anyway, Gary  Good news your problem with your crashing after edit was found! It was an error in the wxTranslation code so you could localization on your screen. Not that your ever going to do that for yourself :) . Because you didn't have the file it was causing a pointer to go out in memory and stomp on memory! On my PC it will messing with my OpenGL and crashing the machine. Steve found the issue (took days and he is REALLY good) and he patched it up and rebuilt the libs. So next version you should be all good. The latest Dev version has what you need if you want to try it. Wanted everyone to know it was fixed and that you helped to delay release LOL . We have started release testing over AGAIN because of this. The QA part of getting software out takes longer than making the code I think! Mach4 is a MASSIVE project with so many ways to use / customize it that it takes a lot of pokes to see if everything is working as it should.

Okay I have rambled enough, long story longer , check out the new version.
Title: Re: Lua Script and reading register's Help
Post by: gorf23 on January 23, 2021, 02:11:14 PM
Thanks Brian

Glad you found it, I know how had it is to fine crap in the code, i'm always having problems with simple scripts...

And to jbuehn

I think i have found a way to get it working I changed the if,  to something like this below  and put this code in a function, that returns the hreg value and it seems to working so far at least with the pokeys57CNC, i am going to try it on the ESS tomorrow and see how that go's.
   
if  (mc.mcRegGetHandle(inst,"PoKeys_40548/PoKeysConnected") == 1)  then               -- The Pokeys57CNC Connected register   

Thanks gary
Title: Re: Lua Script and reading register's Help
Post by: gorf23 on January 24, 2021, 05:36:09 PM
Just an update

It does work with both the pokeys and ESS so looks good now...

I tested a new plugin last week for the pokeys and looks like the register values are working for probing like the ESS now..
Don't know when it will be posted for download, but looks good anyway...

Gary
Title: Re: Lua Script and reading register's Help
Post by: gorf23 on January 31, 2021, 02:56:11 PM
Brian Think this one you maybe to answer.?

So i am reading the register for the value 's to see if the ESS or pokeys are running. Works fine but i have to do it in all my button calls or macros..

Can i read the register once and store the value in a global variable from the PLC or the screen load script?. and use it everywhere.
Do the plugins load before the PLC and screen load script? and are the registers set first.

I tried it in the PLC but wasn't getting the correct global value if i then checked it in a button script.

Thanks Gary
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker on February 01, 2021, 06:14:59 AM
The registers are your global variables and can be seen from all instance of Lua and they are also thread safe.
Title: Re: Lua Script and reading register's Help
Post by: gorf23 on February 01, 2021, 07:28:02 PM
Brian

I think i understand that part, what i am trying to do is set a flag once of my own var, and just use that var everywhere in in my buttons and macros
so i don't have to read the registers in all my buttons and macros, i just set the var once in the screen load script or the plc and then i don't have to write the code to read the same register in all my scripts, once the register is set when Pokeys or ESS plugin is loaded it will not change endless you swap controllers..

Hope it makes more sense i do have a hard time explaining my thoughts !, most likely old age

Thanks gary   
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker on February 01, 2021, 08:33:26 PM
The screen script is global, you will have to check when you open. New chunk. I don’t think I understand :( sorry I don’t think I am helping ..
Title: Re: Lua Script and reading register's Help
Post by: gorf23 on February 02, 2021, 12:36:48 PM
Thanks Brian

I don't think its you but more likely my explanation...

Here is a sample of the code it only needs to run once, and the hreg and probestate values global. so i can use anytime..
of course there will be no return's needed

Code: [Select]
function myZprobe.ConnectedDevice()
local hreg = 0
local ProbeState = 0
local Ereg  = mc.mcRegGetHandle(inst,"ESS/Connected")
local Preg = mc.mcRegGetHandle(inst,"PoKeys_40548/DeviceConnected")
if (mc.mcRegGetValue(Preg) == 1) then
     hreg  = mc.mcRegGetHandle(inst,"PoKeys_40548/ProbeStatus")
ProbeState = -4
return hreg, ProbeState
      elseif (mc.mcRegGetValue(Ereg) == 1) then
       hreg  = mc.mcRegGetHandle(inst,"ESS/Probing_State")
   ProbeState = -1
   return hreg, ProbeState
end
end


Thanks gary
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker on February 02, 2021, 01:31:47 PM
You want to have something that will run one time at startup ... We can do that many ways. A global screen var saying that you have doneInit = false and set it to true when you have done it. Then you will never run it again. Also you can do a scr.IsLoaded() to make sure that the screen set is fully up.
Title: Re: Lua Script and reading register's Help
Post by: gorf23 on February 02, 2021, 04:49:34 PM
Thanks Brian

Would i use the PLC script or the screen load script I'm assuming the PLC

Gary
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker on February 02, 2021, 05:18:41 PM
You got it! 8)
Title: Re: Lua Script and reading register's Help
Post by: gorf23 on February 02, 2021, 05:32:06 PM
Thanks Brian

This seems to work look ok to you.. no problems down the road?
its in the PLC script

Code: [Select]
if (pluginState == 0) and (scr.IsLoaded()) then
PluginReg = 0
ProbeState = 0
local Ereg  = mc.mcRegGetHandle(inst,"ESS/Connected")
local Preg = mc.mcRegGetHandle(inst,"PoKeys_40548/DeviceConnected")
if (mc.mcRegGetValue(Preg) == 1) then
     pluginReg  = mc.mcRegGetHandle(inst,"PoKeys_40548/ProbeStatus")
ProbeState = -4
      elseif (mc.mcRegGetValue(Ereg) == 1) then
       pluginReg  = mc.mcRegGetHandle(inst,"ESS/Probing_State")
   ProbeState = -1
end
pluginState = 1
end

Thanks gary
Title: Re: Lua Script and reading register's Help
Post by: Brian Barker on February 02, 2021, 06:20:23 PM
yup that will be fine