Hello Guest it is April 26, 2024, 08:50:48 PM

Author Topic: lua script help have a problem with converting to number's  (Read 802 times)

0 Members and 1 Guest are viewing this topic.

Offline gorf23

*
  •  189 189
    • View Profile
lua script help have a problem with converting to number's
« on: September 16, 2021, 07:56:36 AM »
I am read A DRO valve and want to store it in a register i read it alright, but if i try to format it so i loose the values after the decimal point
if i don't convert it to a number i get the error can't use string number expected, so i convert it to a number i get the, error nul value. it is always when i am trying to write the value to the register..

any suggestions?


Code: [Select]
local inst = mc.mcGetInstance()
local val = scr.GetProperty("Tool1DRO", "Value")
--wx.wxMessageBox("Val = "..val)
--val = tonumber(val)
--x = string.format("%.1f"..tonumber(val))
mc.mcCntlSetLastError(inst, "val = "..val)
local Pos1reg = mc.mcRegGetHandle(inst,"gRegs0/ToolInPos1")
rc = mc.mcRegSetValueString(Pos1reg,string.format("%.0f",3))
rc = mc.mcRegSetValueString(Pos1reg,string.format(val))

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: lua script help have a problem with converting to number's
« Reply #1 on: September 16, 2021, 12:53:09 PM »
It is hard to tell what is what in that script because some of it was remarked out and the last two lines are confusing.  But anyway, without even trying to determine what is wrong with the script, you can fix this easily but just pointing your DRO to that register with the DRO's Register property and use whatever format you want with the DRO's Format Property.  :)

Steve

Offline gorf23

*
  •  189 189
    • View Profile
Re: lua script help have a problem with converting to number's
« Reply #2 on: September 16, 2021, 01:33:32 PM »
thanks not really sure how you mean to point to dro register I'm still in an early lua learning process..
not sure why i either get the error its a nul value after i try to use tonumber(val) and if i don't convert to number i get the error its a string needs to be a number...

This code here works but its not writing to the register it going to the machine.ini file not sure why the conversions work here but not when i am using the registers...

Code: [Select]
local inst = mc.mcGetInstance()
local val = scr.GetProperty("Tool2DRO", "Value")
val = tonumber(val)
val = (math.floor(val+0.5))
local Tool2= mc.mcProfileGetString(inst, 'ToolChangeSettings', 'ToolPos2', '0.0000')

if (Tool2 ~= string.format (val)) then
mc.mcProfileWriteString(inst, "ToolChangeSettings", "ToolPos2", string.format (val))
mc.mcCntlSetLastError(inst, "Tool Position 2: updated to Tool: "..string.format (val))
end


thanks

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: lua script help have a problem with converting to number's
« Reply #3 on: September 16, 2021, 03:24:51 PM »
thanks not really sure how you mean to point to dro register I'm still in an early lua learning process..

What I mean is edit your screen, click on the DRO that is named "Tool2DRO" and edit the Rregister property to point to your "gRegs0/ToolInPos1" register.  Done.  Use whatever format in the Format you wish.  No LUA code required. 

Steve

Offline gorf23

*
  •  189 189
    • View Profile
Re: lua script help have a problem with converting to number's
« Reply #4 on: September 16, 2021, 04:15:57 PM »
Thanks Steve,

That was way to easy, i always seem to go down the hard road first..

Gary

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: lua script help have a problem with converting to number's
« Reply #5 on: September 16, 2021, 05:05:57 PM »
Glad I could help!  :)  And yeah, I'm a hard road traveler myself.  So I know...  I know.  LOL

Steve