Access Modbus pins directly with Python - doable?

More
10 years 10 months ago #67656 by andypugh
You might want to re-think using PyVCP and switch to GladeVCP instead. That allows you to allocate Python event handler code to the controls.

You might not want mb2hal at all, you might want to write your own Python userspace component that imports Modbus and handles all comms.

However, if that seems too much work, I would suggest a Python userspace component that creates HAL pins to connect to the mb2hal pins. The only reason not to do this is that it requires the extra step of net-ing all the HAL pins together.

This page here describes how to do it:
linuxcnc.org/docs/2.7/html/hal/halmodule.html

Though you could actually embed all the code (and pin creation) in your Glade panel handler.

You can do quite a lot with LinuxCNC from the Python interface:
linuxcnc.org/docs/2.7/html/config/python-interface.html

This page has some info on Python and Modbus, though I feel that there should not be any need to install any new packages to do what you want to do.
stackoverflow.com/questions/17081442/python-modbus-library

Please Log in or Create an account to join the conversation.

More
10 years 10 months ago #67672 by Simpson36

andypugh wrote: You might want to re-think using PyVCP and switch to GladeVCP instead. That allows you to allocate Python event handler code to the controls.

Yes, I looked at both and it seemed PyVCP was the faster track to get things going. I have started working with GladeVCP now and the event handlers are great. The problem I run into is that GladeVCP seems to be targeted at a specific set of tasks and if you need to do something outside that box, you are in for some headaches.
For example, I could find no way to set the value of a pin from within the handler of another pin. Maybe there is a way to do this that I have not discovered.

You might not want mb2hal at all, you might want to write your own Python userspace component that imports Modbus and handles all comms.

Not sure I understand this. mb2hal is the Modbus communications. Without that there would be nothing to import. I had to extensively modify a Modbus library to get it to run on an Arduino DUE when that board first came out and I had to go back and modify it again to work with mb2hal. Really not up for writing an all new Modbus for LinuxCNC.

Other than the data type being inconvenient, mb2hal works extremely well. It is very fast and has per function error checking! Hard to improve on that.

However, if that seems too much work, I would suggest a Python userspace component that creates HAL pins to connect to the mb2hal pins. The only reason not to do this is that it requires the extra step of net-ing all the HAL pins together.

This page here describes how to do it:
linuxcnc.org/docs/2.7/html/hal/halmodule.html


This seems to be the key. I kept searching for 'Python and Modbus' and maybe that 's why I missed it.

I worked with the halmodule only for about an hour, but I did not find a way to access the existing mb2hal pins, but making new ones and netting them to the existing mb2hal pins is easy enough and I have that working.

The Modbus for my device uses about 30 registers and 40 coils (these were all 'bitpacked' prior to porting to LinuxCNC) and only a half dozen are involved in passing the cels, so making new pins for these and 'netting' them to the existing mb2hal pins is not a big deal at all. I have Python getting the data off the 'netted' Modbus pins and doing some simple calculations and then sending the data to another pin, so I *think* I'm off and running to finish this project using Halmodule.

Though you could actually embed all the code (and pin creation) in your Glade panel handler.


While I was successful is stuffing a bunch of code into a 'def', I have been unable to get the Glade panel handlers to reach outside the specific pin they are working on. I tried a bunch of stuff, but nothing worked except creating a set of variables to hold data from one action and pass it to the next. That works, but it's not an acceptable solution.

Simply put, is there a way to have the Glade handlers read or write the value of a pin other than the one in the 'def' action?

You can do quite a lot with LinuxCNC from the Python interface:
linuxcnc.org/docs/2.7/html/config/python-interface.html


I looked at this and will probably use it to pass the commands to LinuxCNC, but there is no ability to manipulate pins in this module . . at least not that I could find. :(

This page has some info on Python and Modbus, though I feel that there should not be any need to install any new packages to do what you want to do.
stackoverflow.com/questions/17081442/python-modbus-library


This library is serial. I may have neglected to mention in this thread that I am using TCP.

You're right about not needing any new packages! mb2hal and PyVCP or GladeVCP together with some mods to the modbus on the device side and your conversion routines, takes care of 90% or the interface. The challenge was the other 10% that ironically just needed the simplest read/write functions on existing pins.

It looks like hal.component is going to be the solution. :)

Please Log in or Create an account to join the conversation.

More
10 years 9 months ago #67674 by newbynobi
You may want to look at the source code of gmoccapy or gscreen, both use gladd and python and gmoccapy does modify pins ove several defs.

Norbert
The following user(s) said Thank You: Simpson36

Please Log in or Create an account to join the conversation.

More
10 years 9 months ago #67679 by Simpson36
Excellent tip, thanks!

Please Log in or Create an account to join the conversation.

More
10 years 9 months ago #67727 by Simpson36
Thanks to all the help, I have the CEL system connected between the device and LinuxCNC with Python program sending/receiving and managing the command/acknowledge communications. There are actually 18 steps involved in each 'transaction'.

I have one last question that I cannot find in the docs. Hoping it is something simple.

LinuxCNC starts up fine with a custom hal, a PostGui hal, PyVCP and mb2hal and everything works great.

Now I need to add a Python program and another .ini, but I cannot figure out where to add them.

The Python program creates 6 new pins and the .ini nets those pins, so the Python program has to fun after the GUI (I think) and then the INI file has to run after that because the pins don't exist until after the python runs. I can't find that scenario in the documentation.

At the moment I run the Python program as an 'executable' after LinuxCNC starts and then open a terminal and paste in and run this line:

"halcmd -f /home/sfs/LinuxCNC/configs/sim.axis/inturncel.hal"

and that gets everything going, but I'm sure there is a way to get these guys to run automatically, but the timing complicates things as they need to run in a certain order.

Can anyone point me to the correct documentation or provide some guidance (with syntax) as to where and how to add these programs to the startup sequence?

Please Log in or Create an account to join the conversation.

More
10 years 9 months ago #67728 by andypugh
I think you can have more than one POSTGUI_HALFILE in the INI, if you need to.

Please Log in or Create an account to join the conversation.

Time to create page: 0.137 seconds
Powered by Kunena Forum