Hello Guest it is March 28, 2024, 07:51:17 AM

Author Topic: writing modbus coils using LUA for Mach4? how?  (Read 4526 times)

0 Members and 1 Guest are viewing this topic.

writing modbus coils using LUA for Mach4? how?
« on: October 13, 2017, 01:36:13 PM »
I'm interfacing build 3481 of Mach4 to a Hitachi NES1 VFD. I've got them communicating and I can write registers fine from LUA. What I can't seem to do is write coils from LUA since Mach4 does not create registers for coils how do I access it from LUA?

Thanks

Mark

Re: writing modbus coils using LUA for Mach4? how?
« Reply #1 on: October 13, 2017, 01:57:01 PM »
Hi Mark,
have you enabled the Modbus plugin?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: writing modbus coils using LUA for Mach4? how?
« Reply #2 on: October 13, 2017, 02:07:12 PM »
Hi Craig:

I have modbus communicating to the VFD, I can write a frequency to the VFD because that uses the write registers function. And that function when setup in Configure/Modbus automatically creates an instance register that I can access from LUA. And that is working. However the write coil function which is just working on a bit field when setup in modbus does not create any kind of variable automatically. So I don't know how to tie that to my LUA script. I went through all the scripts you did when you helped someone else get a VFD working but he was using only registers. My NES1 needs coils for stop/run and direction but uses registers for the frequency setting.
I really appreciate the help.

Mark
Re: writing modbus coils using LUA for Mach4? how?
« Reply #3 on: October 13, 2017, 02:32:39 PM »
Hi,
you need to add a function to your Modbus connection.

Configure/Plugins/Modbus.....with your connection highlighted, probably Modbus0 unless you have a whole swag of them! There is a row of four
icons at the top of the window...Make New Connection, Delete Connection, Add a Function, Delete a function. The Add a function icon should be active
with the others greyed out. The function setup panel allows you to write to and read from coils, registers and blocks of registers. Enjoy...

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: writing modbus coils using LUA for Mach4? how?
« Reply #4 on: October 13, 2017, 02:37:05 PM »
Hi Craig:

I have done that already. When you do that for a register in modbus you automatically get an instance variable that you can access in LUA. But when you do the same thing for a coil you don't. So the question is once you have the write coil function defined in Modbus how do you actually set it's value in LUA?

Thanks

Mark
Re: writing modbus coils using LUA for Mach4? how?
« Reply #5 on: October 13, 2017, 02:39:27 PM »
Hi,
rereading your post and I think I misunderstood your question.

You can set individual coils in the VFD ergo you must have a working Modbus function to do so. If I understand your question is how do you
set a bit in Lua that will eventually transmitted to the VFD? Is this correct?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: writing modbus coils using LUA for Mach4? how?
« Reply #6 on: October 13, 2017, 03:07:29 PM »
Hi Craig:

See attached files. I have a working modbus connection to the VFD but I'm only able to set register values not coil values.

In the screenshots my setFrequency function I defined in Configure/Modbus causes Mach4 to automatically create the spndlFrequency0 and spndlFrequency1 registers. The script in the screenshot (most of which you wrote lol) does indeed set the frequency in the VFD and it then appears in the modbus diagnostic window and registers diagnostics window when I press the button that the script is attached to. I set the register value and it gets sent to the VFD without error.

So registers work fine for me with the VFD from what I can tell.

What I don't know how to do is change the coil value from 0 to 1

Mach does not create a register that I can get an instance to in LUA script for me for the coil like it does for the write register function.

to make my test button start the spindle I need to do the following

1) set the frequency (I have that working since it uses registers and I now know how to do that)

2) Set the direction by writing to coil 2 either a 1 or 0 depending on if I want fwd or rev

3) set the spindle to run by writing a 1 to coil 1 (that is my sop/run function I have defined in modbus)

I don't know how to do steps 2 or 3 show me some code obi-wan

Sorry for being a pain.

Mark

PS:

If I press the button that executes the script in the screenshot I do see the spndlFrequency0 and spndlFrequency1 values change in the modbus tree on the register diagnostics and I also see them change in the diagnostic window for Modbus (see screenshot)

So I have registers working but don't know how to set a coil to 0 or 1 in LUA after I have it the function defined in modbus

is that any clearer?



Re: writing modbus coils using LUA for Mach4? how?
« Reply #7 on: October 13, 2017, 03:13:35 PM »
forgot to add these screenshots
Re: writing modbus coils using LUA for Mach4? how?
« Reply #8 on: October 13, 2017, 03:29:03 PM »
Hi Mark,
I'm having exactly the same issue. The Modbus function to write a to a coil seems to be there but not a register name therefore no handle
therefore no way to set the bit.

I wonder if we are confusing a coil and a register? My thinking suggests that a coil is just one bit of a register, therefore to set a coil in your
target device you need to write to that one bit in that register. Sounds like you are thinking the same way.

I'm now wondering if that is correct...what if 'I want to set a coil in my device then "write coil" function' ie that the function 'write coil' actually
sets a bit in the device and presumably some other function clears it?

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: writing modbus coils using LUA for Mach4? how?
« Reply #9 on: October 13, 2017, 03:36:40 PM »
Hi Craig:

Now we are on the same page. Simpson36 who seems to have worked a lot with modbus asked the same question in a previous thread but then 2 hours later said "never mind, figured it out" but never posted what he figured out :)

but on the hitachi the coil numbers are 1 and 2 and guess what that is the same address used for the setFrequency register so unless there is some addressing translation scheme that I don't know about I'm stuck.

Here was the thread asking the same question from Simpson367 https://www.machsupport.com/forum/index.php/topic,27158.msg209066.html#msg209066