Access Modbus pins directly with Python - doable?
For the last piece of the puzzle, I need Python to have access to read and write the TCP Modbus pins that are set-up by mb2hal on startup.
FWIW, I have done lots of homework on this and searches, tutorials and research has turned up a bunch of related stuff but all having to do with using the GUI and/or over a serial connection. I have also played with the Python interface, which gets all of the info you could want . . except the pins.
If there a way to read/write TCP Modbus pins directly from Python?
Please Log in or Create an account to join the conversation.
- seb_kuzminsky
- Offline
- Administrator
-
- Posts: 75
- Thank you received: 14
Here's an example of a python program that makes hal pins: github.com/jepler/linuxcnc-mirror/blob/m...anger/m61/test-ui.py
And here's the "postgui.hal" config that it runs after creating its pins, to connect the pins to pre-existing hal nets: github.com/jepler/linuxcnc-mirror/blob/m...nger/m61/postgui.hal
Those nets are created by an earlier hal file: github.com/jepler/linuxcnc-mirror/blob/m...ger/m61/core_sim.hal
Hope this helps.
Please Log in or Create an account to join the conversation.
One way is to write a Python HAL component that creates its own pins, and those can be netted to the HAL pins you want to read/write.
Can you give a bit more detail about what the desired result is?
Please Log in or Create an account to join the conversation.
I have PyVCP panel with a bunch of 'nets' to the mb2hal pins and that works great for the CNC control signals. The need for Python to access existing pins outside of the GUI will be explained in my next post.
Again, thanks for responding!
Please Log in or Create an account to join the conversation.
The process is this:
The pendant has an unlimited number of 'CELs' that contain any CNC command, text, G-code fragment, Macro, parameters, or whatever the user creates. each CEL content is assigned a unique number and kept in a disk file on the CNC computer.
the process, over simplified, is this
1) the user touches a cel on screen, and the pendant loads the item number into a Modbus register and then sends a 'trip' to the CNC via a modbus coil. At this point the CNC also displays an message on the CNC screen stating that a remote command has come in
2) the CNC gets the trip, then reads the item number out of the Modbus register
3) the CNC places this item number on a different modbus register and sends an 'acknowledge' signal (bit) to the pendant on a coil
4) the pendant detects the ack and reads out the item number sent by the CNC and reads the returned item number from the Modbus reg.
5) the pendant compares the item number sent to the item number returned and only if they match it sends an 'OK to proceed' (bit) back to the CNC on yet another coil.
6) the CNC looks up the data for that item number on a disk file and sends the contents to the CNC for execution (or whatever action is indicated).
7) when successfully completed, the CNC sends a last acknowledge to the pendant, again via Modbus
8) the pendant the displays the status (successful, error, or whatever) in text and echos that message number over a Modbus register back to the CNC for display on the CNC screen.
Notice that step 8 is the only one that woud use the GUI. All of the rest are 'invisible' communications between the pendant processor and the Python program via Modbus.
There are some other tasks that require processing (exponential curves for rotary control knobs on the pendant, etc) that require some calculations (including conditional statements) before being sent on to the CNC and that would be a lot to try to stuff into an 'action'. tripped by the GUI pin.
- Steve
Please Log in or Create an account to join the conversation.
I have another thread going about fixing some things in the mb2hal and the C header file has been located and there are instructions from 'Big John' on downloading some development tools. I am familiar with C and compilers (to a degree) , but at this point I have no idea how to go about compiling code for Linux/LinuxCNC or how to contribute and navigate the git stuff.
I am more than happy to help out, but I'll need a project lead (or equiv) to give me specific tasks to do . . . and probably how to do them . . .
![:blush: :blush:](/media/kunena/emoticons/blush.png)
Please Log in or Create an account to join the conversation.
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.
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.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.
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.
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.You might not want mb2hal at all, you might want to write your own Python userspace component that imports Modbus and handles all comms.
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.
![:( :(](/media/kunena/emoticons/sad.png)
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.
![:) :)](/media/kunena/emoticons/smile.png)
Please Log in or Create an account to join the conversation.
Norbert
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.