Hello Guest it is March 28, 2024, 10:03:49 AM

Author Topic: reading and writing to registers  (Read 857 times)

0 Members and 1 Guest are viewing this topic.

Offline Bill_O

*
  •  562 562
    • View Profile
reading and writing to registers
« on: February 25, 2019, 05:57:11 PM »
Does anyone have an example of reading and writing to registers?

Thanks,
Bill
Re: reading and writing to registers
« Reply #1 on: February 25, 2019, 06:23:26 PM »
Hi,
I will go through the basics first.

I assume you want a register to hold a value or string for your use? There are already a goodly number of registers defined in Mach4,
some for core variables, some will be for your motion controller variables, or Modbus variables, or Serial Comms variables......etc.

In the first instance you need to define a new register.

1)Ensure the regfile plugin is enabled  ( Configure/Control/Plugins) picture 1 attached.
2)Open the regfile plugin (Configure/Plugins/Regfile) picture 2 attached
3)Define a new Instance Register by clicking on the green icon, name the new register, assign an initial value if necessary
and decide whether it is to be persistant, that is to say saved to the .ini file on shutdown and repopulated when Mach
restarts. Picture 3 attached.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: reading and writing to registers
« Reply #2 on: February 25, 2019, 06:47:59 PM »
Hi,
now that you have a new register defined you can inspect its contents....
Diagnostics/Regfile. Picture 1 attached

Expand  iRegs0 to see the currently defined registers and their current contents. Note that in my example that the
register iRegs0/jewelHeightReg has a value of 105.22 rather than its initial value as I assigned to it in my previous post.
This is because the last time I used this register its value was changed to 105.22 and because it is persitent it is saved
between Mach sessions. Picture 2 attached. Note that a useful feature is to 'pin' the regfile window to the top which allows
you to monitor the contents while a process or debug session is in progress.

You can manually edit the contents of a register by double clicking on it. Picture 3 attached.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: reading and writing to registers
« Reply #3 on: February 25, 2019, 07:14:57 PM »
Hi,
now you have a new register/registers and you can inspect/edit manually. Note all this and more is covered in the
Mach4 Scripting Manual chapter 4, in Mach4Hobby/Docs folder.

Note that section 4.4 of the manual is somewhat out of date. It used to be necessary to explicitly flush the contents of
a register to the .ini file and reload it at the start of a new session. This is no longer required. None the less it is still
useful to be able to send the contents of a register to the .ini file under programmatic control.

Per Chapter 4 the main APIs are:

Quote
 hreg = mc.mcRegGetHandle(inst, path)
 val = mc.mcRegGetValue(hreg)
 string = mc.mcRegGetValueString(hreg)
 mc.mcRegSetValue(hreg)
 mc.mcRegSetValueString(hreg)

Thus to retrieve the value of jewelHeightReg in my example:

Code: [Select]
local hreg=mc.mcRegGetHandle(inst,'iRegs0/jewelHeightReg')
local ValuejewelHeightReg=mc.mcRegGetValue(inst,hreg)

As so often is the case reading a variable in Lua is a two step process, the first step is to find the variables 'handle'
or current address. Note that as the Mach session progresses that location can change....don't be duped.....get the
handle THEN get the value.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'

Offline Bill_O

*
  •  562 562
    • View Profile
Re: reading and writing to registers
« Reply #4 on: February 26, 2019, 08:09:54 AM »
Craig,

I had the registers taken care of but thanks for making sure I had it all.
I will work on the rest in a little while.

Thanks,
Bill