Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: django013 on May 30, 2017, 09:01:23 AM

Title: mcRegGetValue returns unexpected value
Post by: django013 on May 30, 2017, 09:01:23 AM
Hi,

I'm trying to work with registers. First I only want to read from registers and display that values ...
The register file configuration shows the defined registers with their values.
The screenshot from debugger shows, what mcRegGetValue read from register.

probe has value of 2, so expected value is 12, but retrieved register value is shown in the flyover help as 0.033

I already searched for typos, but can't find them. What am I doing wrong?
Title: Re: mcRegGetValue returns unexpected value
Post by: smurph on May 31, 2017, 04:54:08 PM
I would first start using the return code on the API functions to discover if there is an error or not.

local h0, rc = mc.mcRegGetHandle()
if (rc ~= mc.MERROR_NOERROR) then
    --- There is an error condition.
end

Steve
Title: Re: mcRegGetValue returns unexpected value
Post by: django013 on May 31, 2017, 11:48:04 PM
Hi Steve,

meanwhile I read, that there exists an API call for integers separately - but I don't like the idea of registers in that usecase any more.
The data is a table and I don't like the idea of having to read a complete table value by value ...
So I read a bit about lua and serialization and found a sample to store/load tables as single API-calls. The price is a separate file, but I'm willing to pay that price :)
Now the code is as clean as expected and works fine so far.

What I don't understand is the way of module loading. I got the hint, that each lua-panel is like a sandbox, where I have to reload the module.
Thats nasty :(
I checked the module internals and it shows, that the loaded code for a lua panel stays loaded the whole runtime of mach. That's quite ok, but there's no way of interaction with that panels elements. Neither exported variables can be accessed.
At least I found none.

So what do you think: would it be resonable to load a module and put the modules handle in a register and load that register inside the lua panel code?
That way the already loaded (and exported) variables could be accessed from different panels and the sandbox should work as before?!?
I like the idea, that a module could be treaten like a singleton :)

cheers Reinhard

Title: Re: mcRegGetValue returns unexpected value
Post by: smurph on June 01, 2017, 01:46:18 AM
Reinhard,

LUA is not multi-threaded as there is nothing protecting the global data tables.  So each instance runs in its' own space (with its' own global data and message loop, etc...).  Each LUA panel has one instance of LUA associated with it.  You can share code between the instances by requiring a module.  But the global data of one instance cannot be accesses from one instance to the next, etc...  So storing the address of a module table variable from one instance in a register and trying to use it in another instance will lead to "bad news".  The only really safe way to share data across LUA instances is with the Mach registers (because these registers are thread safe and mutexed).

In Mach, these are the LUA instances:
1. One instance for the GUI (runs the screen scripts)
2. One instance for the mcLua macro script plugin. (runs M codes)
3. One instance for the PMC (runs the ladder code)
4. And one instance per LUA panel in the GUI.

All of these instances have access to the MachAPI functions.
Only the GUI has access to the ScreenAPI functions. 

Steve
Title: Re: mcRegGetValue returns unexpected value
Post by: django013 on June 01, 2017, 02:02:24 AM
Ok, that makes sense :)
Thank you for the explainings!

Didn't expect, that each lua panel runs in its own thread. So ok.
Good to know, that registers are guarded by mutexes :)

Quote
All of these instances have access to the MachAPI functions.
Only the GUI has access to the ScreenAPI functions.
Sure about the latter?
Currently I use a hidden dro for synchronization and it works fairly well.
I use scr.GetProperty and scr.SetProperty from lua-panel-module-code
Ok user interaction is pretty slow respect to thread timings, but I use onUpdate and onModify triggers on dro controls which work too.

What about extending registers to table handling?
The serialization code is able to serialize tables to strings and read from strings ...
If you're interested, I could post the code ...

cheers Reinhard
Title: Re: mcRegGetValue returns unexpected value
Post by: django013 on June 01, 2017, 11:35:01 PM
based on the new informations, I rearranged my module code ...

- I extracted lua panel code so that each panel has now its own source file
- I removed all but one register
- bounded the dro to the remaining register
- changed lua-panel code to change register instead of dro value directly
- dro now fires update events in GUI-thread when changing register value in lua-panel-thread
- update-handler runs in GUI-thread and can use module code

so no more usage of screen API in lua panel code. Hope, the new arrangement is more mach4-specs like :)

cheers Reinhard