Hello Guest it is August 22, 2019, 07:00:25 PM

Author Topic: MACH4 - Modbus  (Read 50514 times)

0 Members and 1 Guest are viewing this topic.

Offline simpson36

*
  •  1,374 1,374
    • View Profile
Re: MACH4 - Modbus
« Reply #40 on: May 13, 2014, 08:19:52 AM »
Update on editing Register TYPE.

MACH4 will now not keep any CHANGES to the TYPE after the REG has been created.

It *seems* to me that I could go in and edit the TYPE, but that may have been on the first demo ver.  . .  -or- I dreamed it . .  :-\

In any case, at this point, MACH4 will not retain Register TYPE changes. It allows the Edit without complaint, but the change is not saved.

Deleting the Function line (in MACH4 Modbus Config) and re-creating a new one with the same Reg names allows and saves the new Reg TYPE

This may be by design to prevent system corruption by changing TYPE after the reg has been used in a TYPE specific screen or macro, but it would be good to know if this behavior (not saving TYPE edits to existing regs) is intentional.
Re: MACH4 - Modbus
« Reply #41 on: May 13, 2014, 10:41:40 AM »
modbus reading registers from the simulator

how do to write to the simulator is the next challenge.

Offline simpson36

*
  •  1,374 1,374
    • View Profile
Re: MACH4 - Modbus
« Reply #42 on: May 13, 2014, 12:23:37 PM »
modbus reading registers from the simulator

how do to write to the simulator is the next challenge.

Anyone who just wants to test the data xfer follow this procedure:

Set up a function to write registers, (looks like you already have that). Make sure the TYPE is register.

Get the sim running and set to show the registers.

Get your Modbus diag window running, expand the function so you can see the register values.

Go back into MACH4 Diags -> Regfile -> Modbus + box

Double click on any Reg and you should get a Dialog box where you can enter a value.  

Do so and click OK and the value should appear (rather quickly I might add) in the Modbus Reg as well as the Simulator.

I have not had a chance to play with the scripting yet.


Re: MACH4 - Modbus
« Reply #43 on: May 13, 2014, 02:08:04 PM »
 :-X ty
cool... now to get the scripting to work
something worked kind of in my messing around

Offline smurph

*
  • *
  •  1,117 1,117
  • "That there... that's an RV."
    • View Profile
Re: MACH4 - Modbus
« Reply #44 on: May 13, 2014, 05:56:43 PM »
Graig,

You are adding two handles that sum to a number larger than a 16 bit register will take.  The internal Mach register can store that number easily.  But the 16 bit register in the PLC cannot.  It also looks like the Rhr3 is also part of a read function.  The internal Mach register will not get updated unless the value ON the PLC changes.  Yet is it possible to SET the value internally from the Mach side.  So if you wrote a number in the simulator register 40003, it would have changed the value in the Register Diagnostics window.

The correct way to do the register read is with two API calls.

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

Rhr3 should == 24564 which is within the 16 bit range.


An enterprising LUA programmer can shorten the 2 API calls with something like this:

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

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

Those are hand written on-the-fly functions so they may not be totally correct.  But put something like them in the Screen load script and you can access them from any screen/object script. This will make things a bit easier in the code like:

local valRhr1 = ReadReg("modbud0/Rhr1");
local valRhr2 = ReadReg("modbud0/Rhr2");
WriteReg("modbud0/Rhr3" valRhr1 + valRhr2);

Steve
Re: MACH4 - Modbus
« Reply #45 on: May 13, 2014, 07:40:16 PM »
Thanks Steve

I knew that, but a bad example gets more attention  >:D

time to play and learn
thanks again
Re: MACH4 - Modbus
« Reply #46 on: May 13, 2014, 08:51:17 PM »
local inst= mc.mcGetInstance();

local hRhr1 = mc.mcRegGetHandle(inst, "modbud0/Rhr1");
local valRhr1 = mc.mcRegGetValue(hRhr1);
local hRhr2 = mc.mcRegGetHandle(inst, "modbud0/Rhr2");
local valRhr2 = mc.mcRegGetValue(hRhr2);
local hRhr3 = mc.mcRegGetHandle(inst, "modbud0/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/Wsr1", valRhr1 + valRhr2);


--works thanks

Offline simpson36

*
  •  1,374 1,374
    • View Profile
Re: MACH4 - Modbus
« Reply #47 on: May 14, 2014, 09:34:29 AM »
Graig,

The correct way to do the register read is with two API calls.

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


A response to your post said this was working, but I don't see how. Note the spelling is modbuD which looks like a typo.

Quote
An enterprising LUA programmer can shorten the 2 API calls  . . .
 
But put something like them in the Screen load script and you can access them from any screen/object script.


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.

Quote

local valRhr1 = ReadReg("modbud0/Rhr1");
local valRhr2 = ReadReg("modbud0/Rhr2");
WriteReg("modbud0/Rhr3" valRhr1 + valRhr2);


Is this just a 'cut and paste' typo, or should "modbud" be used?

Being unable to figure out how to add functions to the 'Screen load script', I tried making a macro. The macro runs in the debugger, but almost never from the MDI and never from within G-code.

Even more strange is that it does run on every MACH4 startup. I'm sure I have done something wrong, but I have tried a lot of stuff and no joy. Need a little help here. File name is m40. First and last line commented out because it won't run at all that way. Message boxes are for diagnostic purposes. It runs from the debugger and executes properly, reading and writing the regs as expected.

--function m40()
local inst
local hRhr1;
local valRhr1;
local hRhr2;
local valRhr2;
local hRhr3;
local valString;
  wx.wxMessageBox("Start");
inst=mc.mcGetInstance();
hRhr1 = mc.mcRegGetHandle(inst, "modbus0/Hreg1");
valString = tostring(hRhr1);
  wx.wxMessageBox(valString);
valRhr1 = mc.mcRegGetValue(hRhr1);
valString = tostring(valRhr1);
  wx.wxMessageBox(valString);
hRhr2 = mc.mcRegGetHandle(inst, "modbus0/Hreg2");
valRhr2 = mc.mcRegGetValue(hRhr2);
valString = tostring(valRhr2);
  wx.wxMessageBox(valString);
hRhr3 = mc.mcRegGetHandle(inst, "modbus0/Wreg3");
valString = tostring(valRhr2 + valRhr1);
  wx.wxMessageBox(valString);

mc.mcRegSetValue(hRhr3, valRhr1 + valRhr2);
  wx.wxMessageBox("Done");

--end

« Last Edit: May 14, 2014, 09:36:43 AM by simpson36 »
Re: MACH4 - Modbus
« Reply #48 on: May 14, 2014, 11:32:41 AM »

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
Re: MACH4 - Modbus
« Reply #49 on: May 14, 2014, 11:35:25 AM »
I just focused on the functions and made sure it found my register values, pulled, computed and passed back the right value.

all done under a button I created to test.

I'm just adding the first and the second and putting the new value back in the first, basically an additive loop.
just to prove out how to do a modbus scripting feature.
now to figure out how to use the data and make bit wise decision.



found they did not really make a difference in my setup, so did not address them.
but going back, now that you reminded me of it, could be very useful.  :)
if you change them to the correct modbus0 name, as you step thru the function you can see them pass the values

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);


far as a m40
you would have to put the script in a m40.mcs file
then call it as a mcode
remember that you have to restart Mach4 to compile the new m40.mcs file
everytime you edit it.
« Last Edit: May 14, 2014, 11:43:27 AM by Ya-Nvr-No »