*BSD News Article 74385


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!munnari.OZ.AU!news.mel.connect.com.au!news.mira.net.au!vic.news.telstra.net!act.news.telstra.net!psgrain!iafrica.com!pipex-sa.net!plug.news.pipex.net!pipex!tank.news.pipex.net!pipex!news.mathworks.com!fu-berlin.de!irz401!orion.sax.de!uriah.heep!news
From: j@uriah.heep.sax.de (J Wunsch)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Serial port reading
Date: 21 Jul 1996 22:21:36 GMT
Organization: Private BSD site, Dresden
Lines: 83
Message-ID: <4suahg$1m8@uriah.heep.sax.de>
References: <Pine.BSF.3.91.960719175019.5545A-100000@ns.tid.com>
Reply-To: joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
NNTP-Posting-Host: localhost.heep.sax.de
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Newsreader: knews 0.9.6
X-Phone: +49-351-2012 669
X-PGP-Fingerprint: DC 47 E6 E4 FF A6 E9 8F  93 21 E0 7D F9 12 D6 4E

Nuno Filipe Monteiro Nunes <nfmn@ns.tid.com> wrote:

> So my problem is this: I have a device connected to my pc serial port 
> (well, to both ports to be more precise...) and it is constantly dumping 
> text lines which I have to dump to a file.
> So the thing is, how can I do this? 

You need a program that handles the serial line.  Most of these
programs are rather known as being ``terminal programs''.  The system
ships with `cu' and `tip', but abusing them to dump raw data from the
port to a file is perhaps overkill and overly complicated.

There's the famous `kermit' in the ports, it has an option to `log a
session'.  Still overkill.

Alternatively, you can tweak /etc/rc.serial so that your ports will be
pre-configured into a well-known state (`clocal' set so they don't
wait forever until some modem carrier might appear, correct baud rate,
etc.).  Then, it should be as simple as

	cat /dev/cuaa0 > myfile

The downside is that there is no good way to terminate this kind of
connection.  If you interrupt it (^C) or kill it, you might lose
unwritten data since the output to `myfile' is probably buffered.  You
can replace `cat' by the following mini-cat-clone that should properly
handle SIGTERM, SIGINT, and SIGHUP:

#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <sysexits.h>

static void
sighandler(int signo)
{
	exit(0);	/* Exit cleanly, will flush the output buffer. */
}

int
main(int argc, char **argv)
{
	FILE *input;
	char b[512];
	size_t amnt;

	if (argc != 2)
		errx(EX_USAGE, "usage: minicat inputfile");

	if ((input = fopen(argv[1], "r")) == NULL)
		err(EX_NOINPUT, "fopen(%s)", argv[1]);

	(void)signal(SIGINT, sighandler);
	(void)signal(SIGTERM, sighandler);
	(void)signal(SIGHUP, sighandler);

	while ((amnt = fread(b, sizeof(char), 512, input)) != 0)
		(void)fwrite(b, sizeof(char), amnt, stdout);

	return 0;
}

(Note that i omitted the error handling for writing stdout, you should
add it if you care for the integrity of your output data.)

Put the above into `minicat.c', and then

	cc -O -o minicat minicat.c

Then use ./minicat instead of `cat' in my example above.  You can
either kill the process (with the default signal 15), or interrupt it
by ^C if it's running in the foreground.  If your line is setup to use
modem pseudo-carrier (e.g. since DTR from the remote side is connected
to your DCD line, and you have _not_ set the clocal option), then a
``carrier drop'' will result in a SIGHUP sent to the program, which
will also exit it cleanly.

-- 
cheers, J"org

joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)