Re: [SLUG] Industrial Linux

From: Tim Jones (tim@linuxtampa.com)
Date: Mon Apr 16 2001 - 12:20:11 EDT


On Sun, Apr 15, 2001 at 10:44:19AM -0400, Bob File wrote:
> I apologise profusely if this ends up being the third time this get sent to
> the list. Reading about sendmail wasn't on the list for today. ;-}
> I am an instrument tech at an industrial site (OJ plant).

Tropicana in Bradenton by any chance?

> I have a
> project pending that I would like to use Linux for. I am not sure I can
> pull it off with the skills I have now. We sometimes fill 55 gallon
> drums with orange juice for shipment to a customer. We do this by
> putting the drum on a scale and weighing it as it fills with juice.
> We use a small "computer" to accomplish this. It has 8 digital inputs, 8
> digital outputs and 4 serial ports. It has an embedded interpreter/os in
> it that one writes programs for and they are run in real time. The
> interpreter allows storage of some data in a flat table. I just finished
> rewriting the printing routines to use new bar code printers. I am told
> by management that our barrel filling will be increasing because our
> parent company has bought other OJ facilities and wants to use our site
> to package all barrel orders. Right now the barrel is filled, a ticket
> printed with various information on it to put on the barrel and the same
> information is recorded to a table. At the end of the run another serial
> port is used to print the table. This is the only record of the run
> because the table is cleared before the next run. I think it is time to
> start recording this information to a database on a PC. My current idea
> is to send the barrel data to a PC via serial port, and let the PC
> record the data in a database and then send the data back out another
> serial port to the bar code printer. I am not really a Linux programmer,
> but I think I could probably muddle my way through the database part of
> the setup with one of the interpreted languages available. What I don't
> know or even have a clue about is the serial port part of this project.

The serial port stuff is tricky, for a couple reasons: First off,
you open the /dev/ttyS[0-3] devices, BUT you have to get the baud
rate, stop bits, and parity set EXACTLY the right way, which is done
through the tcgetattr and tcsetattr calls. Look up the manual pages,
they are old, crufty and non-trivial. Jamie Zawinski of Netscape
fame eventually solved this problem by invoking stty and cu, respectively:

    sub open_modem {
        use IPC::Open2;
        my $stty = `/bin/stty -g`;
        open2( \*MODEM_IN, \*MODEM_OUT, "cu -l$modem_device -s2400 2>&1");
        # starting cu hoses /dev/tty's stty settings, even when it has
        # been opened on a pipe...
        system("/bin/stty $stty");
        $_ = <MODEM_IN>;
        chop;
        if ( !m/^Connected/ ) {
            print STDERR "$0: cu printed `$_' instead of `Connected'\n";
        }
    }

The second problem is basically the same with any interprocess communications:
TIMEOUTs. In other words, you have to be able to wait a certain period of
time for your response. You cannot wait indefinitely, because if the other
side goes down, you'll not be able to report the error. Anytime socket
communications or serial ports get involved, I move to an event-based
programming, combined with a state machine. There is a state where I
send to the remote system (sockets or serial, doesn't matter which),
and another state where I set an alarm, and wait for a response. If the
alarm goes off, then I know I didn't get my response. If I DO get my
response, I cancel the alarm, and continue. I usually have a retry
mechanism as well, for devices that report back a status code.

> I think a Linux box for this
> project would give me more flexibility.
> So am I going to have to learn enough "C" to use the serial port or is
> there another way to do this? I am looking at the Kermit program for
> this, but I am not sure I can do what I want to with it's scripting
> language.

I don't think Kermit will be enough for this project.

> Any clues tossed my way would be appreciated.

Well, I gave my clues above. What *I* would appreciate is a little side
work from helping you implement this. Please have your boss consider
having me come to do this project: I charge $75/hour.

Thanks,

Tim

-- 
-----------------------------------------------
 Timothy Jones - LinuxTampa.com - 813-65-LINUX
Open Source Programming, Databases & Networking
-----------------------------------------------



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:20:35 EDT