Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: maxspongebob on November 23, 2020, 11:51:01 AM

Title: Modbus Programming guidance needed
Post by: maxspongebob on November 23, 2020, 11:51:01 AM
I am in need of information about programming the MODBUS.  I am unable to find documentation or examples that would allow me to setup a separate DRO on the machine that is connected to my MACH 4 computer via MODBUS.  The scripting manual ends at section 5.7 called MODBUS with no detail.  I have found some info but most is in German and I don't speak German.

I just want to update a Write parameter on the MODBUS DRO display whenever the XYZ and RPM values change on the MACH 4 screen.

If anyone could point me in the right direction, it would be very much appreciated.

Bob
Title: Re: Modbus Programming guidance needed
Post by: Bill_O on November 25, 2020, 11:37:12 AM
I am interested in a little better info on modbus also.
I am only going to be bringing info into Mach4 but it is hard to find much in the way of how.
Title: Re: Modbus Programming guidance needed
Post by: KatzYaakov on November 25, 2020, 06:47:02 PM
"me too"
Title: Re: Modbus Programming guidance needed
Post by: smurph on November 26, 2020, 04:52:34 PM
MODBUS is going to be VERY device specific.  All of the device manufacturers use different terminologies and even give register information differently.  MODBUS, for the record, is always base 1 and decimal.  THAT is the standard.  But some device manufacturers give register information in base 0 and hex!!!  So yeah, it can be very confusing and there is no manual besides the ones from the manufacturers.  However, here is a nice writeup on the MODBUS protocol itself.  Having a good understanding of what MODBUS is supposed to be can help you untangle the mess of device documentation out there. 

https://www.modbusdriver.com/doc/libmbusmaster/modbus.html (https://www.modbusdriver.com/doc/libmbusmaster/modbus.html)

The Mach 4 MODBUS plugin just maps I/O or registers to the device.  That is basically ALL it does.  There really isn't any MODBUS "programming" as in some special MODBUS script.  The MODBUS plugin just maps Mach resources (registers and I/O) to the device.  Then you can use any LUA scripting as you would with any other Mach resources from any other plugin. 

Steve

Title: Re: Modbus Programming guidance needed
Post by: smurph on November 26, 2020, 04:56:34 PM
Oh, there is a nice MODBUS PLC simulator that is a great learning/test tool. 

https://sourceforge.net/projects/modrssim2/ (https://sourceforge.net/projects/modrssim2/)

Steve
Title: Re: Modbus Programming guidance needed
Post by: maxspongebob on November 29, 2020, 03:27:03 PM
I know MODBUS and how it works.  What I am asking for is more information about how MACH uses MODBUS and how to take the DRO value and send it to a register in the MODBUS.  Take a look at page 33 of the Scripting Manual in your Documents folder.    The document ENDS with the heading for section "5.7 Modbus". 

It's like AttSoft just said: "WTF, this is too hard, lets just let the user figure it out."  Is it really too much to ask for the document to be finished?  Is it a quality product if the documentation just ends mid sentence? 
Title: Re: Modbus Programming guidance needed
Post by: smurph on November 30, 2020, 12:10:36 AM
The Mach 4 MODBUS plugin just maps I/O or registers to the device.  That is basically ALL it does.  There really isn't any MODBUS "programming" as in some special MODBUS script.  The MODBUS plugin just maps Mach resources (registers and I/O) to the device.  Then you can use any LUA scripting as you would with any other Mach resources from any other plugin. 

I have no idea who wrote that scripting manual and why a MODBUS header was even included.  Maybe the author thought there was some MODBUS specific scripting, included the header, and then forgot to remove it after they found out there wasn't any MODBUS specific scripting?  I didn't write the scripting manual.  But I did write the MODBUS plugin so believe me when I tell you that there is no MODBUS specific scripting.  :)

In fact, you can do what you want with no scripting at all.

1. Setup the MODBUS plugin to have a function that writes to a holding (most likely) register.  (Function 6, write single register).  You will need to know the address of this register on your MODBUS device.  You will also have to give a name for the Mach register that will be mapped to this MODBUS function.  Writing a value of 123456 to this Mach register will use the Function 6 to write 123456 to the holding register you specified. 

2. Drop a DRO on the screen.  Then scroll to the "Register" property and find your MODBUS plugin register that you created. 

3. When you type a value in the DRO, your holding register will be updated. 

ALL Mach registers can be used in this manner.  The only thing different here is that there is a MODBUS device baking the register.

Now, if you want something more fancy that this, like preloading the register with a value, you will have to do some non MODBUS specific scripting that the scripting manual most likely covers (sections 3.2.1 and 4.3 look pertinent).

I believe the rest of the manual documents things VERY well.  I think that section 5.7 was left in there by mistake.  We all make mistakes. 

Steve
Title: Re: Modbus Programming guidance needed
Post by: Cbyrdtopper on December 09, 2020, 11:07:50 AM
Just to throw my 2 cents in.  I use modbus communications with a Click PLC from automation direct on something like 6 or 7 machines in our shop.  It is extremely easy to use.  I send information to and form Mach4 and the Click PLC quite often.  Like Steve said, sometimes you will have to add some script to update certain registers but a lot of it is plug and play. 
For example, I send my Spindle RPM from an encoder to the PLC to make sure the operators CANNOT press the tool release button unless the spindle is under 15rpm.  I don't allow the Tool Drawbar to activate unless the RPM is less than 15. 
I handle about 90% of my I/O with the PLC and just let Mach4 tell the PLC it wants an output on and the PLC will take care of the rest.  Or I can let the PLC tell Mach4 that an Input is active so Mach can do something with it.
Title: Re: Modbus Programming guidance needed
Post by: ChrisJeffreys on January 02, 2021, 04:35:21 PM
Sounds like you have figured out exactly what I’m trying to accomplish.  I’m trying to use Modbus plugin in Mach4 to write to a coil in a Click PLC to turn on my spindle. I think I worked through the Mach 4 Modbus setup correctly, and also I assigned the “Spindle on” output to the Modbus function I created. When I push cycle start the spindle turns on as expected. I rejoice. Exactly 1 minute later, the coil on the plc turns off.  When I check the Mach 4 Modbus diagnostic tool, it says the Modbus connection is good, everything is highlighted green and “ok”.  Is my PLC rejecting the “write coil”or should Mach 4 be constantly updating the write coil function? Any insight would be much appreciated.
Title: Re: Modbus Programming guidance needed
Post by: Cbyrdtopper on January 02, 2021, 04:57:47 PM
You can check the state of the coil in the mod is diagnostics. Be sure it is still being turned on.
Make sure that something isn’t forcing the coil off on the PLC ladder side as well 
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 02, 2021, 04:59:52 PM
Some of the modbus devices need to see communication . So read a status of something and it should be good.
Title: Re: Modbus Programming guidance needed
Post by: ChrisJeffreys on January 02, 2021, 05:33:52 PM
That was it. I put in a dummy read coil function and now the write coil function persists.  Seems like problem solved.  Thank you! 
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 02, 2021, 06:34:35 PM
Wow, I keep this up people are going to think I have done this before... I don’t need lies like that out there.

Now, to run a spindle the PMC with a small amount of Lua sending over modus is really cool! I set that up for the Maho project that a YouTube guy is doing. If you guys want that I can post a copy here.
Title: Re: Modbus Programming guidance needed
Post by: Cbyrdtopper on January 02, 2021, 06:36:33 PM
Interesting. I always communicate both ways. I never would have thought it would’ve needed that.
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 02, 2021, 06:42:52 PM
Yeah, if you get really, really, really board some time read manuals for a few VFDs and you will be shocked what they cram in for features. This is done so you have a watchdog. So if the master is gone the slave will shutdown. You can setup VFDs to stay running if coms are lost too. This stuff gets complex fast! So many settings!
Title: Re: Modbus Programming guidance needed
Post by: ChrisJeffreys on January 02, 2021, 06:53:29 PM
I will read and appreciate anything you will share about Modbus and Mach 4. It seems like a very useful way to add remote i/o but there just isn’t enough documentation about setting it up (in Mach specifically) I’m especially interested in using read coils as inputs in Mach, but haven’t had any success sorting through the setup parameters and mapping the functions.  I assume you mean ToT on the Maho project. He was what got me interested in machining and my router is loosely based on his gantry router.  They should play his videos (and others like him) for the kids in school, best shop class ever.
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 02, 2021, 07:24:05 PM
Man, I can’t get away with anything! Yes it is TOT... we also added a load meter to his setup ;) .

The issue with modbus is that it is a VERY loose standard. Sometimes it is base 0 sometimes it is base 1 for the registers. Sometimes the manual has the table where the data is in memory and sometimes not. Setting up the VFD for Tony’s machine was a real challenge because it has instructions that are not very clear. But with some goofing around and playing with the settings we got it. Now to help you out I can remote in and take a look at your settings and see if we can make this work. I was thinking that I should make some samples for IO devices that people would like to use. I have been in the forum looking for silly stumbling blocks like this so we can fix them. The issue is will anyone read the manual if we make it :)
Title: Re: Modbus Programming guidance needed
Post by: TOTALLYRC on January 03, 2021, 02:42:24 AM
Dear Santa(Brian),
I promise to read the manual 3 times if you write it!!!!!


Mike
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 03, 2021, 06:21:54 AM
Okay, I have not done a manual in a long time so I guess it is time to suffer ;) . I will start this morning and see what happens.  Soon as I get something worth looking at I will put it here so we can see if I am on the right path.

Thanks
Brian
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 03, 2021, 08:45:27 AM
After spending the morning on this I remember why we didn't make a manual. EVERYTHING depends on the slave, and nothing has a simple answer.. I did remember if the registers are bit packed we can very easily unpack them! Use the I/O bitpack setting and it will place the Modbus in your input selections. I put about all the time I want I want to for a Sunday :) I have 3 pages done and by the looks MANY more to go. I have only covered how to get communications and starting on input configuration. I would like to remote in and see if i can make your machine work the way you would like. When I have something that is worth reading I will put it here.
Title: Re: Modbus Programming guidance needed
Post by: Cbyrdtopper on January 03, 2021, 12:02:31 PM
Using a PLC for IO is the best option IMHO. Very stable communication in a robust setup. It’s rather easy to do in Mach4.  All my inputs are in the input configuration. I guess I set it up right long ago lol. I would like a manual of o go over how stuff works together.
Brian, if you need some real word examples of PLC and Mach working together, let me know.
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 03, 2021, 12:47:42 PM
I was going to use a click PLC to get my screen shots. I have one on the bench all setup. If you guys have some drives you would like examples of I could make an appendix in the manual to show people what the settings are for a few devices. I am thinking click PLC , automation direct drives and then a few of the Chinese ones that people seem to use. I know this sounds dumb but I did a VFD on a coolant pump before. It was really cool! I set it so you could do m8 p50 and get 50% flow :)
Title: Re: Modbus Programming guidance needed
Post by: Cbyrdtopper on January 03, 2021, 01:19:22 PM
Now that’s pretty cool.  I’d love to see how you did that.
I also used a BRX from automation direct. It’s a pretty sweet little PLC. Got the communication setup for it as well.
Title: Re: Modbus Programming guidance needed
Post by: TOTALLYRC on January 03, 2021, 07:51:51 PM
Brian,
Using an Arduino to add extra I/O and analog inputs to add FRO, SRO and RRO would be nice.


I have trouble starting from scratch but I can copy and paste with the best of them.

Mike
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 03, 2021, 08:02:09 PM
Are you guys tapping the lines here? I have been taking with someone about a project for a simple serial communication. We are looking at a simple command set so you can easily send control panel commands. I won’t mention any names in the event we don’t follow through. This seems like it could be a bunch of fun for people.

Mike wound that work for you? You can do it now but you would need to use the serial plugin.
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 04, 2021, 09:30:07 AM
Here you are Chad... it is not code that will change the world or anything but it should give you what you need as a base to start from. The cool part is that it shows you how you can read data from the line of the Mcode! The only thing I could not do for you is set the max Hz or set the register that would set the value.

Please tell me if this works for you ..

Code: [Select]
function m8(hParam) --The param is a P value called from Gcode. M8P50 for example would be 50%, No value will be 100%.
local inst = mc.mcGetInstance()
local varP = 100.00 -- default is 100%
if (hParam ~= nil) then
local flagP, rc = mc.mcCntlGetLocalVarFlag(inst, hParam, mc.SV_P)
if (flagP == 1) then --Check that the flag has been set so we do not get an unexpected value for mc.SV_P
varP = mc.mcCntlGetLocalVar(inst, hParam, mc.SV_P)
end
end
mc.mcCntlSetLastError(inst, string.format("P val == %.0f",varP))
local handle = 0;
handle = mc.mcRegGetHandle(inst,"0Regs0/test")-- TODO  !!!!!!!!!!!!!!! Set Modbus register value here
if (handle ~= 0)then
local MaxVFDSpeed = 7000
local val =  MaxVFDSpeed*varP/100
val = string.format("%.0f",tonumber(val))-- Get Val as an int
mc.mcRegSetValue(regH ,tonumber(val))-- Set the speed of the pump
end
handle = 0;
handle = mc.mcSignalGetHandle(inst, mc.OSIG_COOLANTON)
if (handle ~= 0)then
mc.mcSignalSetState(handle, 1)
end
end

if (mc.mcInEditor() == 1) then
    m8()
end
Title: Re: Modbus Programming guidance needed
Post by: Cbyrdtopper on January 04, 2021, 09:46:18 AM
Thanks Brian,  that’ll be nice to use for other things as well!
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 04, 2021, 10:16:24 AM
I can't tell you that I will not miss some but I am going to try to hit this one message board on a regular basis. So if you have something you would like to see I will be around.  I forgot how much fun I had solving problems ;) .
Title: Re: Modbus Programming guidance needed
Post by: ChrisJeffreys on January 04, 2021, 10:45:58 PM
I will definitely welcome the help if you have time to remote in.  I run Mach 4 hobby on a old laptop and have found I have less problems if I keep it off the internet, seems like Windows waits until I hit cycle start to try and update. You let me know when and I’ll get it connected to the internet .  I’ll give you some background on my setup. I bought and finished a partially assembled machine from Alex Burt at CNC Depot. It is a 3 axis bridge mill/gantry router. It’s a beast. Table is 30”x 96” and it weights about 4000 pounds.It came with a Click PLC, WJ200 VFD, and Clear-path SDSK Nema 34 servos turning ball screws. Later I put a S30C ATC spindle from CNC Depot (which I love). I use an Ethernet Smoothstepper for motion control and now that you helped me get Modbus working, I use the Click PLC as remote relays for stop/start of my drive, power drawbar, and I have several extra air solenoids for a future air blast/mist coolant and retractable dust boot. I use a PWM to 0-10 volt conversion module to set my speed on the drive from the smoothstepper. It works ok. The VFD only has 1 set of  programmable contacts, so you have to pick what info is most important. I’d like to be able to read fault status during normal operation AND be able to read Zero Speed status before the drawbar releases during an auto tool change.  Seems like Modbus might be a solution. Would it be better to get a second USB to rs232 for my laptop and have a dedicated Modbus circuit for the drive or would it be better to daisy chain the drive on the same Modbus circuit as the Click PLC? Is there a preference on baud rate for Mach?
Title: Re: Modbus Programming guidance needed
Post by: TOTALLYRC on January 05, 2021, 07:05:13 AM
Are you guys tapping the lines here? I have been taking with someone about a project for a simple serial communication. We are looking at a simple command set so you can easily send control panel commands. I won’t mention any names in the event we don’t follow through. This seems like it could be a bunch of fun for people.

Mike wound that work for you? You can do it now but you would need to use the serial plugin.
Hi Brian, since there is already a Modbus library for Arduino and I also have a Click PLC, and possibly some VFD's to connect, I personally would like to stick with Modbus.


It is nice to see that Mach 4 has matured enough that you are not stuck in development mode all day everyday.

Mike
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 05, 2021, 08:44:08 AM
Mike, We are trying to make a system that will need no real settings. It is a really cleaver setup and I think you will like it better. This will make for a much shorter development cycle.

Chris,
I have sent you a message off list and we can make a time for me to poke around your machine. I hope we can get this rolling and others will be able to get something from this.
Title: Re: Modbus Programming guidance needed
Post by: TOTALLYRC on January 05, 2021, 04:29:50 PM
 ;D Since it will work even for dummies like me I am more than willing to try it. ;D

Mike
Title: Re: Modbus Programming guidance needed
Post by: compewter_numerical on January 06, 2021, 01:57:15 AM
I made something in the past that may be helpful to this chat. Just updated and tested again before I am posting here.

It's a tutorial with all the code as well as an exe installer to get a Arduino Mega with a simple shield to act as a 32 input 32 output modbus device with the possibility of also having the ability to add an ADC or DAC (or both).

It's really simple and includes an exe installer that configures the modbus settings automatically in the Machine.ini so all you have to give the device an IP address. The installer will not overwrite anything but make a backup if you try this first. It might help some with the confusion of setting up modbus.

Here is all you need in hardware:
https://www.amazon.com/XCSOURCE-ATmega2560-16AU-ATMEGA16U2-Expansion-TE168/dp/B00XXEI2CI/ref=sr_1_5?dchild=1&keywords=w5100&qid=1609915738&s=electronics&sr=1-5

Here is the tutorial with all the code:
https://github.com/kethort/io-over-tcp-modbus-arduino-mach4/blob/master/README.md
Title: Re: Modbus Programming guidance needed
Post by: TOTALLYRC on January 06, 2021, 04:26:10 AM
Hi compewter_numerical,
Nice work!
Since I have the Mega and the ethernet card I will give this a try while waiting for Brian's solution.

Mike
Title: Re: Modbus Programming guidance needed
Post by: Brian Barker on January 06, 2021, 06:37:37 AM
Nice!
Title: Re: Modbus Programming guidance needed
Post by: jbuehn on January 06, 2021, 12:26:44 PM
Very cool!
Title: Re: Modbus Programming guidance needed
Post by: compewter_numerical on January 06, 2021, 11:54:35 PM
cool thanks. I just tested the mega/ethernet repo (a couple posts ago) on a fresh install and fixed some issues and the readme. So here is a demo using a 10k pot to ADC from 0V-5V to adjust on screen slider.

https://www.youtube.com/watch?v=ColmX-h78hE

Unfortunately, the slider goes crazy if you assign it a modbus register and an internal Mach4 function. The ADC values need some work too but I'm too lazy to do anything about it and it's probably going to be different for each ADC (I'm using a 16 bit res ADC).
 
If you get really bored you can try to build this MPG Handwheel I turned wireless using a Arduino like device (ESP32). It also has an installer for easy modbus setup and a wizard.

https://github.com/kethort/esp32-cnc-mpg-handwheel-conversion-mach4