Access Modbus pins directly with Python - doable?

More
10 years 9 months ago #67756 by Simpson36
"The halcmd language excels in specifiying components and connections but offers no computational capabilities"

It looks like the tcl scripting offers better acces as well as some computational capabilities.

Thanks for this tip! I'll take a look at this scripting and see if it works any better than Python.

I don't have a favorite scripting language and the first macros I ever wrote for LinuxCNC was about a month ago using BASH. That was for Path Pilot, but probably the same as LinuxCNC. BASH is an odd duck, but it had access to the pins and also has the math.

Should I be looking at BASH? What do most people use to do serious development on LinuxCNC?

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

More
10 years 9 months ago #67757 by newbynobi
have you seen this?
Code:
#!/usr/bin/python import hal, time h = hal.component("passthrough") h.newpin("in", hal.HAL_FLOAT, hal.HAL_IN) h.newpin("out", hal.HAL_FLOAT, hal.HAL_OUT) h.ready() try: while 1: time.sleep(1) h['out'] = h['in'] except KeyboardInterrupt: raise SystemExit

it is from http://linuxcnc.org/docs/html/hal/halmodule.html

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

More
10 years 9 months ago #67766 by cmorley
I can't quite follow exactly what you need but this might help:

In your postgui.hal file add:
source another_hal_file.hal

This will call and execute that file.
You can add as many as you need.

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

More
10 years 9 months ago - 10 years 9 months ago #67783 by Simpson36
Newbynobi,

Yes, that exact file (modified as needed) 's what I am using to access the Modbus and communicate with the device.
Note that this is only about 3 registers and 3 coils out of about 80 total. The rest are happily talking to LinuxCNC thru PyVCP which works fine for hooking stuff top the screen.

The question I have is how to get this program to load automatically with LinuxCNC.

Again note that the hal library cannot 'net' the pins, only create them. S I also have another hal file that nets the pins back to Modbus.

SO . . . this python propram has to run first . . THEN the last hal to do the netting. :woohoo:
Last edit: 10 years 9 months ago by Simpson36.

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

More
10 years 9 months ago #67785 by Simpson36

cmorley wrote: I can't quite follow exactly what you need but this might help:

In your postgui.hal file add:
source another_hal_file.hal

This will call and execute that file.
You can add as many as you need.


Tried this and it does not work. The python program using the hal library is what needs to run. I tried putting a loadusr command in the hal file to start the python. It is executable and works fine if I just execute it as a stand alone file (click/execute from the folder), but trying to run that same as a line in the INI doesn't work.

No idea why.

Please forgive my inability to really describe this accurately, I just don't know all the lingo yet.

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

More
10 years 9 months ago #67796 by andypugh
it sounds like your HAL file needs to "loadusr" your Python program and wait for it to finish loading (loadusr -W) then the HAL file can carry on and connect up all the pins.

ie, the Python script creates HAL pins, the HAL file connects the pins together.

You probably _can_ connect the pins from inside the Python script, but that depends on pin names never changing.

You can not (in HAL, or by any other means) connect pins to more than one net.
If you want to programmatically connect pins that are already in a net at that point then you need to connect to the _signal_ not the pin.

This typically uses a "net" command with the prexisting signal name.

We would be able to guess what was going wrong a lot more readily if you pasted the error messages into your messages rather than saying "nope that doesn't work" and leaving us to guess what you did, what you expected to happen, and what actually did happen.

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

Time to create page: 0.279 seconds
Powered by Kunena Forum