Serial-Programming-HOWTO(1).pdf
(
32 KB
)
Pobierz
Serial Programming HOWTO
Serial Programming HOWTO
Gary Frerking
gary@frerking.org
Peter Baumann
Revision History
Revision 1.01
2001−08−26
Revised by: glf
New maintainer, converted to DocBook
Revision 1.0
1998−01−22
Revised by: phb
Initial document release
This document describes how to program communications with devices over a serial port on a Linux box.
Serial Programming HOWTO
Table of Contents
1. Introduction.
....................................................................................................................................................1
1.1. Copyright Information.
.....................................................................................................................1
1.2. Disclaimer.
........................................................................................................................................1
1.3. New Versions.
...................................................................................................................................2
1.4. Credits.
..............................................................................................................................................2
1.5. Feedback.
..........................................................................................................................................2
2. Getting started.
................................................................................................................................................3
2.1. Debugging.
........................................................................................................................................3
2.2. Port Settings.
.....................................................................................................................................3
2.3. Input Concepts for Serial Devices.
...................................................................................................4
2.3.1. Canonical Input Processing.
.............................................................................................4
2.3.2. Non−Canonical Input Processing.
....................................................................................4
2.3.3. Asynchronous Input.
.........................................................................................................4
2.3.4. Waiting for Input from Multiple Sources.
........................................................................4
3. Program Examples..
........................................................................................................................................6
3.1. Canonical Input Processing.
..............................................................................................................6
3.2. Non−Canonical Input Processing
.....................................................................................................8
3.3. Asynchronous Input.
.........................................................................................................................9
3.4. Waiting for Input from Multiple Sources.
......................................................................................11
4. Other Sources of Information.
.....................................................................................................................13
i
1. Introduction
This is the Linux Serial Programming HOWTO. All about how to program communications with other
devices / computers over a serial line under Linux. Different techniques are explained: Canonical I/O (only
complete lines are transmitted/received), asyncronous I/O, and waiting for input from multiple sources.
This is the first update to the initial release of the Linux Serial Programming HOWTO. The primary purpose
of this update is to change the author information and convert the document to DocBook format. In terms of
technical content, very little if anything has changed at this time. Sweeping changes to the technical content
aren't going to happen overnight, but I'll work on it as much as time allows.
If you've been waiting in the wings for someone to take over this HOWTO, you've gotten your wish. Please
send me any and all feedback you have, it'd be very much appreciated.
All examples were tested using a i386 Linux Kernel 2.0.29.
1.1. Copyright Information
This document is copyrighted (c) 1997 Peter Baumann, (c) 2001 Gary Frerking and is distributed under the
terms of the Linux Documentation Project (LDP) license, stated below.
Unless otherwise stated, Linux HOWTO documents are copyrighted by their respective authors. Linux
HOWTO documents may be reproduced and distributed in whole or in part, in any medium physical or
electronic, as long as this copyright notice is retained on all copies. Commercial redistribution is allowed and
encouraged; however, the author would like to be notified of any such distributions.
All translations, derivative works, or aggregate works incorporating any Linux HOWTO documents must be
covered under this copyright notice. That is, you may not produce a derivative work from a HOWTO and
impose additional restrictions on its distribution. Exceptions to these rules may be granted under certain
conditions; please contact the Linux HOWTO coordinator at the address given below.
In short, we wish to promote dissemination of this information through as many channels as possible.
However, we do wish to retain copyright on the HOWTO documents, and would like to be notified of any
plans to redistribute the HOWTOs.
If you have any questions, please contact
<
linux−howto@metalab.unc.edu
>
1.2. Disclaimer
No liability for the contents of this documents can be accepted. Use the concepts, examples and other content
at your own risk. As this is a new edition of this document, there may be errors and inaccuracies, that may of
course be damaging to your system. Proceed with caution, and although this is highly unlikely, the author(s)
do not take any responsibility for that.
All copyrights are held by their by their respective owners, unless specifically noted otherwise. Use of a term
in this document should not be regarded as affecting the validity of any trademark or service mark.
1. Introduction
1
Serial Programming HOWTO
Naming of particular products or brands should not be seen as endorsements.
You are strongly recommended to take a backup of your system before major installation and backups at
regular intervals.
1.3. New Versions
As previously mentioned, not much is new in terms of technical content yet.
1.4. Credits
The original author thanked Mr. Strudthoff, Michael Carter, Peter Waltenberg, Antonino Ianella, Greg
Hankins, Dave Pfaltzgraff, Sean Lincolne, Michael Wiedmann, and Adrey Bonar.
1.5. Feedback
Feedback is most certainly welcome for this document. Without your submissions and input, this document
wouldn't exist. Please send your additions, comments and criticisms to the following email address :
<
gary@frerking.org
>
.
1.3. New Versions
2
2. Getting started
2.1. Debugging
The best way to debug your code is to set up another Linux box, and connect the two computers via a
null−modem cable. Use miniterm (available from the LDP programmers guide
(
ftp://sunsite.unc.edu/pub/Linux/docs/LDP/programmers−guide/lpg−0.4.tar.gz
in
the examples directory) to transmit characters to your Linux box. Miniterm can be compiled very easily and
will transmit all keyboard input raw over the serial port. Only the define statement
#define
MODEMDEVICE "/dev/ttyS0"
has to be checked. Set it to
ttyS0
for COM1,
ttyS1
for COM2, etc.. It
is essential for testing, that
all
characters are transmitted raw (without output processing) over the line. To
test your connection, start miniterm on both computers and just type away. The characters input on one
computer should appear on the other computer and vice versa. The input will not be echoed to the attached
screen.
To make a null−modem cable you have to cross the TxD (transmit) and RxD (receive) lines. For a description
of a cable see sect. 7 of the Serial−HOWTO.
It is also possible to perform this testing with only one computer, if you have two unused serial ports. You
can then run two miniterms off two virtual consoles. If you free a serial port by disconnecting the mouse,
remember to redirect
/dev/mouse
if it exists. If you use a multiport serial card, be sure to configure it
correctly. I had mine configured wrong and everything worked fine as long as I was testing only on my
computer. When I connected to another computer, the port started loosing characters. Executing two
programs on one computer just isn't fully asynchronous.
2.2. Port Settings
The devices
/dev/ttyS*
are intended to hook up terminals to your Linux box, and are configured for this
use after startup. This has to be kept in mind when programming communication with a raw device. E.g. the
ports are configured to echo characters sent from the device back to it, which normally has to be changed for
data transmission.
All parameters can be easily configured from within a program. The configuration is stored in a structure
struct termios
, which is defined in
<asm/termbits.h>
:
#define NCCS 19
struct termios {
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[NCCS]; /* control characters */
};
This file also includes all flag definitions. The input mode flags in
c_iflag
handle all input processing,
which means that the characters sent from the device can be processed before they are read with
read
.
Similarly
c_oflag
handles the output processing.
c_cflag
contains the settings for the port, as the
2. Getting started
3
Plik z chomika:
megaskrypty
Inne pliki z tego folderu:
3-Button-Mouse(1).pdf
(58 KB)
XWindow-Overview-HOWTO(1).pdf
(30 KB)
Xterm-Title(1).pdf
(39 KB)
Xterminals(1).pdf
(30 KB)
Xinerama-HOWTO(1).pdf
(33 KB)
Inne foldery tego chomika:
Programowanie
Zgłoś jeśli
naruszono regulamin