Hello Guest it is August 21, 2019, 10:26:25 AM

Author Topic: MACH4 - Modbus  (Read 50484 times)

0 Members and 1 Guest are viewing this topic.

Offline simpson36

*
  •  1,374 1,374
    • View Profile
Re: MACH4 - Modbus
« Reply #50 on: May 14, 2014, 01:05:21 PM »

What is the file name of the 'Screen load script' ? The file that scrolls when you select 'LUA sciprt' from the Operator dropdown looks like the right file, but it is compiled and cannot be edited. I tried some tricks, but nothing worked.

 

Cliick on wxmach in the tree, then below on events.
Russ

Thanks. I'll give that a try!

Offline simpson36

*
  •  1,374 1,374
    • View Profile
Re: MACH4 - Modbus
« Reply #51 on: May 14, 2014, 01:15:26 PM »

if you change them to the correct modbus0 name, as you step thru the function you can see them pass the values


Thanks. I had a feeling it was just a typo. I though the term might be different on purpose to eliminate a conflict somewhere , but nothing is working for me either way, so I could not confirm.


Quote
far as a m40
you would have to put the script in a m40.mcs file
then call it as a mcode


Yes, it is an M40 macro. I tried it both source and compiled. It does work via the debugger, just really unpredictable from MIDI and never runs from G-code. Another mystery.

Incidentally, there is a compile choice on one of the drop downs in the editor, so you *,ight* not need to restart MACXH4 every time.

I did not know MACH4 compiled the macros at startup, but a glitch in that might explain why M40 runs every time MACH starts. That is really weird.

There is a possibility that these spooky problems have more to do with my Win7 Pro 64 bit machine than with MACH4. I will run this on my CNC computer which is less complicated and runs 32 bit Win 7 pro.


Thanks for the help.

Offline smurph

*
  • *
  •  1,117 1,117
  • "That there... that's an RV."
    • View Profile
Re: MACH4 - Modbus
« Reply #52 on: May 14, 2014, 01:28:07 PM »
If the macro script is not wrapped in a function, then that is what you will get.

This is what a macro should look like.  Notice at the bottom it has code that detects the editor so that it will actually run the M40 function.

Code: [Select]
function m40()
    inst=mc.mcGetInstance()
    mc.mcCntlSetLastError(inst, 'I'm in M40!!!!')
end

if (mc.mcInEditor() == 1) then
    m40()
end

Steve
Re: MACH4 - Modbus
« Reply #53 on: May 14, 2014, 01:39:41 PM »
I just created a m40.mcs and called it up with a mdi and It works for my address's
 change address to suit your named ones.


function m40()

local inst= mc.mcGetInstance();

local hRhr1 = mc.mcRegGetHandle(inst, "modbus0/Rhr1");
local valRhr1 = mc.mcRegGetValue(hRhr1);
local hRhr2 = mc.mcRegGetHandle(inst, "modbus0/Rhr2");
local valRhr2 = mc.mcRegGetValue(hRhr2);
local hRhr3 = mc.mcRegGetHandle(inst, "modbus0/Rhr3");
mc.mcRegSetValue(hRhr3, valRhr1 + valRhr2);

function ReadReg(regName)
    local hReg = mc.mcRegGetHandle(inst, regName);
    local val = mc.mcRegGetValue(hReg);
    return(val);
end

function WriteReg(regName, val)
    local hReg = mc.mcRegGetHandle(inst, regName);
    mc.mcRegSetValue(hReg, val);
end

valRhr1 = ReadReg("modbus0/Rhr1");
valRhr2 = ReadReg("modbus0/Rhr2");
WriteReg("modbus0/Wsr3", valRhr1 + valRhr2);

end

if (mc.mcInEditor() == 1) then -- as Steve said this works in the editor for debugging the m40 function only
    m40()
end

Offline simpson36

*
  •  1,374 1,374
    • View Profile
Re: MACH4 - Modbus
« Reply #54 on: May 14, 2014, 02:48:52 PM »
Code: [Select]
[quote author=smurph link=topic=27158.msg192373#msg192373 date=1400088487]
If the macro script is not wrapped in a function, then that is what you will get.

if (mc.mcInEditor() == 1) then
    m40()
end

My Macro was modeled after the existing M6 which does not have the editor detector at the bottom. I was blissfully unaware of the bit of the puzzle.

The macro was wrapped in a function, but would not run in the editor UNLESS it was unwrapped and stepped thru. Catch 22.

Now, with the 'In Editor' statements, I see that I can step thru AND it will also run.

I have your read and write functions added to the proper file (much thanks to Overloaded) but I am getting an error message when  I try to use them in a macro. Something about a chunk.

So, again thanks to all for the help. The macro is running now and I'll tinker about with calling the functions and see f I can get something going with that rout or by attaching the calls to a button . . . which I do not know how to do either . . LOL!!

Anyway, one step at a time, it is coming together. Thanks to everyone for their patience.


Offline simpson36

*
  •  1,374 1,374
    • View Profile
Re: MACH4 - Modbus
« Reply #55 on: May 14, 2014, 04:28:23 PM »
Trying to call the new functions. Stepping thru the script and getting error message that indicates MACH4 is attempting to get the Registers off the computer's NIC address.

The script I am attempting to run and the added functions are shown below.  The IP in the error msg is the computer.

The script is attached to a screen button. I have tried it wrapped and unwrapped. 22 was substituted for the added vars to eliminate one possible cause.
« Last Edit: May 14, 2014, 04:33:41 PM by simpson36 »
Re: MACH4 - Modbus
« Reply #56 on: May 14, 2014, 04:37:55 PM »
Is regName case sensitive ?
Re: MACH4 - Modbus
« Reply #57 on: May 14, 2014, 04:50:54 PM »
id say by the look of it, you cannot local inst mc.mcGetInstance() more than once in the same over all button script
the functions would have to be in the load script area and you call them from there
if they are in the same overall function you call it at the top once

please post as code this is a pita to follow
« Last Edit: May 14, 2014, 04:58:16 PM by Ya-Nvr-No »

Offline simpson36

*
  •  1,374 1,374
    • View Profile
Re: MACH4 - Modbus
« Reply #58 on: May 15, 2014, 07:40:31 AM »
attempt to call global 'ReadReg'  (a nil value)

I don't get why you cannot call a 'global' anything, but I've run out of things to try, so I post here FWIW.

My schedule s slammed and I probably won't find much time to indulge my new MACH4 addiction, but I wanted to take time to leave some initial comments on the 'MACH4 experience' since I suspect that's what they are looking for.

GENERAL COMMENTS:

As far as the 'global call' issue; while it is typically easier to maintain an application that is composed of centralized functions, but I find that developing under a 'mothership' can dictate different priorities. From a support standpoint, sometimes it is better to have all of the code for a particular action self contained as that isolates a problem and prevents my stuff from going south if a user modifies the screen script and messes something up, or tries out a new slick feature that comes in the form of a new screen script which of course would not have all of my routines. It might also prevent a bug injected in a new release from thrashing everything. The bottom line of all that is that it's not particularly important for my purposes that the 'global' function calls work perfectly.  I probably will not use them unless they are contained in a separate file that would be immune to changes in the main screen script.

Certainly I do not know nearly enough about MACH4 to make a decision on that, but I think I have enough stuff working to evaluate the new methodology and perhaps come up with a plan for porting my stuff over. I have a bunch of new features to add also, but MACH3 is just too convoluted to be a good host for them. Lack if individually accessible registers prevented me from taking advantage of the much better Serial Plug-in structure. Broken Bit Wise operations, etc.

Kudos to the developers for making one structure for all of the Modbus interfaces in MACH4. The different setups for TCP vs Serial vs Serial plug-in are . . . .  a challenge. I've only scratched the surface thus far, but it appears to me that MACH4 is going to be a far superior platform to develop under than MACH3.

It has been challenging to get stuff to work. I purchased a LUA programming guide, but that doesn't help with the MACH specific stuff. When the MACH4 docs catch up, I expect it will not be such a chore to figure out how to do stuff. I know this demo was not released with the intent of users hacking away at the Modbus, so I appreciate the responses from everyone, particularly from the developers.

OVERALL IMPRESSION:

I think the developers are creating a support nightmare for everyone with the confusing Modbus terminology and a support nightmare for themselves with the packaging of the commercial/industrial version,  but neither of those directly effect me at this point, so my opinion, plus about six bucks, will get you a coffee drink at Starbucks.

Primarily, I build prototypes and therefor use the MDI a LOT so as I said previously, the Multi-Line MDI by itself easily justifies the $200 for MACH4 and I found that feature almost immediately. My focus here was to see what improvements had been made to the Modbus interface because that effects what can be developed on top of the new faster, mo' better core. The parts that I need to work, do work, and at first blush, it seems that the limitations have been moved from the software itself to the creativity and skill of the programmer, so that is a very good thing.


 

« Last Edit: May 15, 2014, 07:48:10 AM by simpson36 »
Re: MACH4 - Modbus
« Reply #59 on: May 15, 2014, 08:44:27 AM »
--i put this in the screen load script and I don't see any issues calling it from any place   ::)

function ReadReg(regName)
    local inst= mc.mcGetInstance();
    mc.mcCntlSetLastError(0, 'ReadReg = ' .. tostring(ReadReg));
    local hReg = mc.mcRegGetHandle(inst, regName);
    local val = mc.mcRegGetValue(hReg);
    return(val);
end

function WriteReg(regName, val)
    local inst= mc.mcGetInstance();
    mc.mcCntlSetLastError(0, 'WriteReg = ' .. tostring(WriteReg));
    local hReg = mc.mcRegGetHandle(inst, regName);
    mc.mcRegSetValue(hReg, val);
end