From: kevin@shady.UUCP (Kevin Smith)
Newsgroups: biz.sco.general
Subject: Re: Capturing data from a RS232 Port
Date: 31 Dec 92 19:00:15 GMT
Organization: ShadeTree Software, Inc.

In article <9212302347.aa18271@alvarez.alvarez.COM> alexl@alvarez.COM (/dev/null) writes:
>.>   From: Steven E Frazier <eng.norstan.com!sfrazier@uunet.UU.NET>
>.>   Subject: Capturing data from a RS232 Port
>.>   Organization: Norstan Communications - Columbus, Ohio
>.>   Date: Wed, 30 Dec 1992 21:04:44 GMT
>.>   Message-Id: <C03Bvy.3JB@norstan.com>
>
>
>.>   Is there a way to use an existing program on SCO to open a file and capture
>.>   data coming from say a RS232 port /dev/ttyak and then use cron to drop 
>.>   DTR @ say 0200 then open the port back up?  I am thinking cu could do
>.>   that but can than be run in the background?
>
>.>   The purpose is to capture CDR (Call Detail Records) from a PBX, at 0200
>.>   close the port copy them to another file and then reopen the port again
>.>   for data capture.  
>
>.>   Thanks in advance.
>
>
>It is crude but it should work.

... crude shell script ...

>

Assuming the data is some kind of newline/return terminated ASCII data,
you could read it line by line.  I'll explain why in a minute.

Script to read lines from the tty and log to a file.

    LOGFILE=...
    TTY=...
    (
	# Note: redirected input of the parenthesized(?) code
	# will setup the stdin for both the stty and the read.
	# You may need to fool with settings like 'ixoff, icanon,
	# icrnl...' to get the right behavior.
	stty .....

	# Read lines from tty one at a time and append them to the
	# log file.  The logic here is that another process will
	# come along and move the log file occasionally (once a day
	# at... say 2am).  This methodolgy will automatically start
	# new file with out skipping a beat.
	while :
	do
	    read line
	    echo "$line" >>$LOGFILE
	done

	# Note: If the input data is non-ascii but fixed length you
	# could use dd instead of the read/echo combo.  Remember to
	# set -icanon on the port.
	#   while :
	#   do
	#       dd bs=?? count=1 >>$LOGFILE
	#   done
    ) <$TTY

From cron, periodically

    LOGFILE=...
    DATAFILE=...

    # Moving the log file will accomplish two things (for this to work,
    # the LOG and DATA files must be on the same filesystem to get the
    # benifit of move by linking rather than copying).  One, it will
    # Isolate the data from the active log file.  The next transaction
    # read in the above script will start a new log file.  Two, by
    # mv'ing the file, if the logging script just happens to be in the
    # middle of writing a record it will be either added, in full, to the
    # DATA file (by virtue of the fact that it had the file open when
    # you moved it and since you are really linking and unlinking,
    # it is really still the same file so the log record will actually
    # go into the renamed file), or will cause the creation of a new
    # log file (if you moved it before it actually got the log file
    # open).  In either case you have a full record one place or the
    # other.

    mv $LOGFILE $DATAFILE

    # Wait a little while to let the log process finish writing it's
    # record (in case of the obscure race condition described above).

    sleep 5

    <process the data file>
    
DTR should automatically track the opened status of the port (i.e.  as long
as the port is open by someone, DTR will be high).  With this setup, DTR
will stay high as long as the logger is running.  If the PBX in question
uses DTR to determine when to send data you may have a race condition
between the port being opened and the stty setting the proper modes.
-- 
	    | Email - !shady!kevin uunet!shady!kevin kevin%shady@uunet.uu.net
Kevin Smith | Voice - (+1) (908) 874-7980
	    | Mail  - ShadeTree Software, Inc., 192 Capricorn Dr. #10,
	    |         Somerville, NJ  08876, USA
Newsgroups: biz.sco.announce