SUMMARY: Capturing text from a serial port

From: John Clinton (
Date: Wed Dec 02 1998 - 06:31:43 CST

Thanks to all who responded. Apologies for my delay posting the Summary.

The question was: How do I capture text from a serial port and log to a

There were two general responses. However a (small C program I imagine)
binary was sent to me as well as a suggestion to use Peek.

Option one: Use Kermit

Option two: Write a shell script using stty. Douglas Carr had the cleanest approach I think. Although Carsten
Knudsen's script is equivalent. Douglas's email is below.
Please contact him for permission to use it. Carsten's email is after

Douglas's Email


  Several years ago my boss asked me to do something
similar. We have a in house digital phone system in our
department. This system has the capabilities of outputting
phone usage data via a serial port. Not wanting to do a
lot of C programming I decided to write a small shell
program to capture ASCII data arriving on the serial port
to a file. It was quite a simple program once I did
some research. Your free to use it if you want.

****** Cut Here ******

# Phone * Watch
# 02/02/98
# Douglas E. Carr
# All rights assigned to,
# Agricultural Engineering Department
# University of Kentucky
# A program to monitor the data comming in on "/dev/ttyb" from
# the Executech II phone system.
# Features that need to be added in the future.
# 1. File (device locking).
# 2. Copy and move the data file to the administrators login.

# Create a new file each month, with the format "mmyy.log"

#FILE=/var/log/`date +%m%y.log`

# Assign file descriptor 3 to "/dev/ttyb"

exec 3< /dev/ttyb

# Set IOCTL parameters for "/dev/ttyb"

/usr/5bin/stty 2400 -parenb -parodd cs7 cstopb -ixoff igncr < /dev/ttyb

# Capture date from "/dev/ttyb" forever(?).

while true
  cat <&3 >> $FILE

******* Cut here *******


Carsten's Email:

Hi John,

This can be done in any number of ways using any number of utilities
(as usual in UNiX;-). The most political correct way is probably
writing a small C program using ioctl(2) to setup the tty port. Others
will probably prefer a PERL script. Not being a PERL equilibrist, I
prefer a /bin/sh script:


exec 3< /dev/ttyXXX # Open whatever tty you are using as filedes #3

# Use stty to setup port - redirect stty input to the just opened fd#3

/bin/stty {preferred baudrate and other settings} <&3

while true
do line <&3 >> $logfile

It's generally smarter (while not necessarily a performance gain) to
read a line at a time from the port that to use cat(1); this ensures
that the logfile is always updated on a line-by-line basis.

good luck


  Carsten B. Knudsen Email:
  TERMA Elektronik A/S Phone: +45 8622 2000
  Hovmarken 4-6 Fax: +45 8622 2799
  DK-8520 Lystrup, Denmark (@home:

Any my original posting to the list:

-----Original Message-----
[]On Behalf Of John
Sent: Wednesday, November 25, 1998 8:20 AM
To: Sun Managers
Subject: Capturing text from a serial port

Hi Sun Managers,

I need to capture the text sent to a Sun's serial port and send it to a

Background information:

There is PC sending text out its serial port intended for a printer (just
plain text). My manager has asked that I connect this PC's serial port to a
Sun (Solaris 2.6) and send this text to a file.

How do I configure the port for this on the Sun? I plan on using a null
modem cable and make sure all the pin outs are correct, however I need
direction with the software configuration.

Can anyone point me in the right direction? I have searched on the web
and only found general serial port configuration guidelines.

John Clinton

This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:12:53 CDT