On Fri, Nov 30, 2007 at 01:27:11AM -0800, Klemen Dovrtel wrote:
> Is it possible to send a simple command to rs232
> within the G code using emc. I would like to control
> the pneumatic valves and some other simple stuff and i
> don't want to sped parallel port pins for simple
> things like this. 

Every "serial" device is different, so to do this you will have to write
your own "HAL component".  If the control does not need to be real-time,
then it is fairly simple to do this.  One choice is to use Python and
pyserial.

You will have to install the pyserial package.  If your Ubuntu system is
on the internet, this can be done through the package manager by
selecting the package called 'python-serial'.  It is in the "universe"
repository, which is not enabled by default but you can find
instructions online for how to enable it.  If you're not online, then
download this file onto a usb drive then install it by double-clicking:
    
http://us.archive.ubuntu.com/ubuntu/pool/universe/p/pyserial/python-serial_2.2-1_all.deb

Here is an (untested!) example which should give you a general idea of
the complexity of such a driver.  This driver controls a hypothetical
serial-attached device which turns something on when the character "1"
is received, and turns it off when the character "0" is received:

#!/usr/bin/python
# -----------------------------------------------------------------------
# Import the necessary modules
import hal
import time
import pyserial

# Get the serial connection -- first port, 9600,8,N,1
import serial
ser = serial.Serial(0)

# Create the HAL component and its pin.  You will use a .hal file to
# connect whatever bit signal you want to the pin 'example.enable'.
h = hal.component("example")
h.newpin("enable", hal.HAL_BIT, hal.HAL_IN)
h.ready()

# The previous value of the 'enable' pin so that a byte is only sent on
# the serial port when it is necessary.  This setting, which is not a
# number, means that the first time the value will always be considered
# "changed".
last = None

# (without try/except, the component will print what looks like an error
# when emc is shut down, and the cleanup below won't happen)
try:
    while 1:
        # As long as the component is running, periodically check
        # whether the value has changed; if it has, send a command to
        # set the new value
        new = h["enable"]
        if new != old:
            if new: ser.write("1")
            else: ser.write("0")
        old = new
        time.sleep(.01)
except KeyboardInterrupt: pass
            
# Shut down the connected device at exit
ser.write("0")
# -----------------------------------------------------------------------

Put the above in a file named "example", make it executable (chmod +x
example), and put it in a directory on your $PATH.

In your HAL file, hook it up something like this:
    loadusr -W example
    net coolant iocontrol.0.coolant-mist => example.enable

Jeff

-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
Emc-users mailing list
Emc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/emc-users

Reply via email to