Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: mcardoso on April 29, 2020, 08:29:45 AM

Title: Interface with Industrial PLC (Ethernet or other options)
Post by: mcardoso on April 29, 2020, 08:29:45 AM
Hi All,

I'm not actively trying to make this work this second, but I'm interested in learning about trying to interface Mach 4 to an industrial PLC. I had a lot of success implementing a serial communication function within LUA so that got me thinking about other methods.

I have an Allen Bradley ControlLogix PLC. It can talk on almost any industrial communication protocol, but the most common is Ethernet/IP. The quick and dirty way to get this to work would to be to add a Modbus TCP module to the PLC and connect that to the PC, however is Mach 4 capable of Ethernet messaging? Is this functionality built into LUA natively?

I'm thinking mostly about just generic I/O consumption from the inputs on the PLC, but perhaps more involved schemes could be created that would allow Mach 4 to instruct the PLC to do things (not coordinated to the motion planner). Ethernet is wicked fast so I think this would be pretty successful.
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: Cbyrdtopper on April 29, 2020, 08:44:51 AM
Mach4 has TCP Modbus.  You just set it up in the modubs configuration.   I have 5 or 6 machines here in the shop that I'm using the Click PLC from Automation Direct and communicating with TCP Modbus.  I use it to handle most  of my IO. 
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: mcardoso on April 29, 2020, 09:07:47 AM
Yup that would definitely work I would think. My PC that runs Mach 4 has 2 Ethernet ports, so one for ESS and one could be for Modbus TCP.

Was just curious if it was a fools errand to try to communicate through Ethernet unsolicited socket messaging or any other method over pure Ethernet (not Modbus)?
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: bcoop on April 29, 2020, 10:31:51 AM
For Compact/control logix  you can quite easily setup modbus/TCP communications and talk to Mach. Rockwell as an example program on their website that you can download and make minor mods to get working.   

Bob
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: mcardoso on April 29, 2020, 10:56:54 AM
Oh perfect. I was actually just researching this. I knew there were Modbus expansion cards, but direct socket messaging to a Modbus TCP master would be much cleaner. I need to play around with this!

I'm assuming that if my goal is to consume digital and analog values into Mach 4 and perhaps use digital bits to sequence logic in the PLC, Mach 4 would be the master and the PLC would be the slave.
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: mcardoso on April 29, 2020, 01:38:13 PM
Holy crap! I poked at the Modbus configuration and code in the PLC. Had everything working in 15 minutes! It is so much more simple than setting up Modbus in Mach 3.

So far the only thing that isn't working is if I power on my slave (PLC) after Mach 4 is opened up, the Modbus won't connect automatically. I have to go into the Diagnostic -> Modbus then click STOP and START. After that, everything is working. Any idea how to get it to connect automatically?

Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: mcardoso on April 29, 2020, 02:35:30 PM
So I'm getting a pretty good feel for this! I do notice that Mach is likely to crash or respond slowly when I setup very high counts of read registers (0x4, qty 1000). Another thing I notice is that each Modbus function takes about 40ms to execute pretty much regardless of the quantity of registers being read. This discourages the user from setting up lots of individual Read Coil (0x1) functions in favor of 1 function with a large quantity of coils.

Where would I find the LUA API calls for the Modbus? I don't see them in the Mach4API.chm file.



Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: jbuehn on April 29, 2020, 03:06:46 PM
In some of the newer development versions, the change log has some items about changing the scan time.
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: bcoop on April 29, 2020, 04:32:52 PM
Glad to hear you got the PLC portion going,  I had some issues with reading data from my VFD for the spindle, seems Mach would read all integers as signed, and my VFD was using some unsigned such as frequency 0-40,000 for for 0-400hz. so when reading that into Mach, anything over 32767 would turn into a negative value, anyway long story short, worked with tech support and they had their programmer add the ability to set the word as unsigned, its now working fine, and in the prerelease FTP site, along with the scan rate denominator to change scan rate for each topic

Bob
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: mcardoso on April 29, 2020, 05:07:41 PM
Wow! Glad you're familiar with this! So does the scan denominator slow down the rate of update? Like only update every 4 scans if the denominator is "4"?

Any idea how to add the modbus signals to LUA? Like lets say I wanted to read the value on one register and write it to another...
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: bcoop on April 29, 2020, 06:46:57 PM
Mike,  so if your poll interval for modbus is set for 100ms,  and the denominator for a MB function is set to "4"  then the scan rate for that function would be 100/4 =25ms.

16/32bit data show up as registers, and can be used just like any register accessible from the register plugin. viewable in diagnostics.
Bits don't show up there, but they do show up were you assign your I/O signals - see snapshot


Bob
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: smurph on May 12, 2020, 01:41:04 AM
Coils and discrete inputs (outputs and inputs) show up as output and input objects in Mach.  And can be operated on via the mcIo*() API calls. 

I should have called the scan denominator a multiplier or "read every n cycles".  I have to change that!   If your scan time is 100ms, and scan denominator is 4, then the function is performed every 4 cycles (every 400ms) milliseconds.  Is is a way to SLOW the function.

All modbus devices implement things differently.  Sometimes, you may be reading a version or the like that comes from SLOW NVRAM.  If you have to read something like this, it can slow you whole scan!  So if you separate the slow read into its' own function, then you can read it less often via the multiplier (what is now called the denominator).

Also, in the later modubs plugins (4458 and up) you can control the plugin via its command register called mbcntl.  Look for it in the regfile plugin diag window.  You can actually test the commands in that interface.

mcRegSendCommand() will send the command and receive the response and it is a nice robust way to control a plugin that provides such a command register.

Anyway, the commands you write to the register are:
stop
start
restart

By themselves, they will operate on the plugin as a whole. But now you can operate on the device and function levels as well! Do this with the name of the device as seen in the modbus diag window.

start modbus0
stop modbus0
restart modubus0

Next, you can drill down to the function level.

start modbus0/fucntion1
stop modbus0/fucntion1
restart modubus0/function1

:) Total control now. So for your slow NVRAM read, just create another function and stop it when you don't need to read it anymore.

Here is the example in code:
Code: [Select]
hReg, rc = mc.mcRegGetHandle(inst, 'mbcntl/command');
response, rc = mc.mcRegSendCommand(hReg, 'stop modbus0') -- response will be OK or FAIL.
response, rc = mc.mcRegSendCommand(hReg, 'start modbus0')
response, rc = mc.mcRegSendCommand(hReg, 'stop modbus0/function1')
response, rc = mc.mcRegSendCommand(hReg, 'start modbus0/function1')

There is an Initial State that is defined at the function level too.  So you can control what is started at startup time.  In case you need to start a function up late with the command register.

Steve
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: Richardtij on May 17, 2021, 03:50:59 AM
Thanks for the topic! You gave me a great idea!
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: Richardtij on May 17, 2021, 03:18:10 PM
Thanks for the topic!
You realy give me a great idea for my project. How well I paused while working. Although I am waiting for my order and the courier anyway. In theory, he was supposed to deliver my order yesterday, it's strange ... I hope my modules  will come without damage. Honestly, next time I'd better order directly from the companies. However, I still have points that I could have done without modules, while I am doing my project. Thanks again for the topic and for giving me the idea! And yes, I think this is a pretty topical topic, so up topic.
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: Cbyrdtopper on May 20, 2021, 12:23:06 PM
Steve,
So the command doesn't like two words in a name.  Click PLC doesn't work.  Click does.
So... I guess I'll just redo the entire modbus setup for the Click.  You guys should make a way to just rename a Modbus connection ;) 
Is there a way to start, stop, and restart the entire modbus?  Like hitting the stop and play button on the modbus diagnostics screen?
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: jbuehn on May 20, 2021, 03:40:59 PM
Using stop, start, and restart commands without specifying a device and function should act the same as hitting the stop/start button in the Modbus diagnostics screen.
Title: Re: Interface with Industrial PLC (Ethernet or other options)
Post by: Cbyrdtopper on May 20, 2021, 03:51:52 PM
That'll do it!  HAHA  Why didn't I think of that!?!? Thanks Jbuehn!