📄 a linux serial port test program.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0070)http://www.comptechdoc.org/os/linux/programming/c/linux_pgcserial.html -->
<HTML><HEAD><TITLE>A Linux serial port test program</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2600.0" name=GENERATOR>
<META content="Shows the source code for a Linux serial port test program"
name=description>
<META
content="linux serial port, linux serial port, serial port testing, test program, c program example"
name=keywords><LINK href="A Linux serial port test program.files/style.css"
type=text/css rel=stylesheet><!-- Background white, links blue (unvisited), purple (visited), red (active) --></HEAD>
<BODY>
<TABLE>
<TBODY>
<TR>
<TD vAlign=top>
<HR class=firsthr>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0 valign="top">
<TBODY>
<TR>
<TD vAlign=top width=150><BR>
<DIV align=center>
<DIV><A class=vertlinks href="http://www.comptechdoc.org/"
target=_top>Home</A></DIV>
<DIV><A class=vertlinks href="http://www.comptechdoc.org/os/"
target=_top>Operating Systems</A></DIV>
<DIV><A class=vertlinks href="http://www.comptechdoc.org/os/linux/"
target=_top>Linux</A></DIV></DIV>
<OL class=leftnumbers>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/linux_pgintro.html">Introduction</A>
<H3>System Information</H3>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/linux_pgcomm.html">Inter-Process
Communication</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/linux_pgsignals.html">Signals</A>
<H3>Programming in Various Environments</H3>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptintro.html">Script
Programming</A>
<OL class=leftnumbers>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptvariables.html">Script
Variables</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscripttest.html">Test
Conditions</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptcontrol.html">Control
and Iteration</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptprog.html">Commonly
used Programs</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptbuiltin.html">Shell
Capabilities</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptexloop.html">Example
looping script</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptexamples.html">Example
using Variables</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptexfiles.html">Example
working with files</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/script/linux_pgscriptexinstall.html">Example
install script</A> </LI></OL>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/c/linux_pgcintro.html">C
and C++ Programming</A>
<OL class=leftnumbers>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/c/linux_pgcpostest.html">POSIX
System Capabilities</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/c/linux_pgcmoreposix.html">More
POSIX</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/c/linux_pgcthreads.html">Threads</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/c/linux_pgcmutex.html">Mutexes</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/c/linux_pgcviewmod.html">An
example viewmod program</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/c/linux_pgcserial.html">An
example serial program</A> </LI></OL>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/x/linux_pgxintro.html">X
Programming</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/linux_pgdebug.html">Debugging</A>
<LI><A class=vertlinks
href="http://www.comptechdoc.org/os/linux/programming/linux_pgcredits.html">Credits</A>
</LI></OL>
<DIV align=center>
<DIV><A class=vertlinks href="http://www.comptechdoc.org/os/linux/"
target=_top>Linux</A></DIV>
<DIV><A class=vertlinks href="http://www.comptechdoc.org/os/"
target=_top>Operating Systems</A></DIV>
<DIV><A class=vertlinks href="http://www.comptechdoc.org/"
target=_top>Home</A></DIV></DIV></TD>
<TD vAlign=top align=left>
<TABLE width="96%" align=center>
<TBODY>
<TR>
<TD>
<TABLE>
<TBODY>
<TR>
<TD vAlign=top width="100%"><!--Begin TechTutorials.info Banner code -->
<DIV style="TEXT-ALIGN: center"><IFRAME
style="BORDER-TOP-WIDTH: 0px; PADDING-RIGHT: 0px; PADDING-LEFT: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; VERTICAL-ALIGN: top; PADDING-TOP: 0px; TEXT-ALIGN: center; BORDER-RIGHT-WIDTH: 0px"
align=center marginWidth=0 marginHeight=0
src="A Linux serial port test program.files/bandisplay.htm"
frameBorder=0 width=468 scrolling=no
height=60></IFRAME></DIV><!--End TechTutorials.info Banner code -->
<DIV class=center><A
href="http://www.comptechdoc.org/os/linux/programming/c/linux_pgc.html"
target=_top>Linux C Programmer's Guide Contents
Page</A></DIV>
<H1>A Linux serial port test program</H1>
<P>Below is a Linux serial test program which requires
the command parameters to be entered on the command line
when the program is started. This program will send
output typed on the computer keyboard after the program
is started, through the serial port specified. This
program can be downloaded using this link: <A
href="http://www.comptechdoc.org/os/linux/programming/c/com.c">com.c</A>.
I recommend that you right click on the link to download
it rather than viewing it in your browser and saving it
so you do not get carriage returns and line feeds in the
text which may cause the compilation to fail. </P><PRE>#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 //POSIX compliant source
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
void signal_handler_IO (int status); //definition of signal handler
int wait_flag=TRUE; //TRUE while no signal received
char devicename[80];
long Baud_Rate = 38400; // default Baud Rate (110 through 38400)
long BAUD; // derived baud rate from command line
long DATABITS;
long STOPBITS;
long PARITYON;
long PARITY;
int Data_Bits = 8; // Number of data bits
int Stop_Bits = 1; // Number of stop bits
int Parity = 0; // Parity as follows:
// 00 = NONE, 01 = Odd, 02 = Even, 03 = Mark, 04 = Space
int Format = 4;
FILE *input;
FILE *output;
int status;
main(int Parm_Count, char *Parms[])
{
char version[80] = " POSIX compliant Communications test program version 1.00 4-25-1999\r\n";
char version1[80] = " Copyright(C) Mark Zehner/Peter Baumann 1999\r\n";
char version2[80] = " This code is based on a DOS based test program by Mark Zehner and a Serial\r\n";
char version3[80] = " Programming POSIX howto by Peter Baumann, integrated by Mark Zehner\r\n";
char version4[80] = " This program allows you to send characters out the specified port by typing\r\n";
char version5[80] = " on the keyboard. Characters typed will be echoed to the console, and \r\n";
char version6[80] = " characters received will be echoed to the console.\r\n";
char version7[80] = " The setup parameters for the device name, receive data format, baud rate\r\n";
char version8[80] = " and other serial port parameters must be entered on the command line \r\n";
char version9[80] = " To see how to do this, just type the name of this program. \r\n";
char version10[80] = " This program is free software; you can redistribute it and/or modify it\r\n";
char version11[80] = " under the terms of the GNU General Public License as published by the \r\n";
char version12[80] = " Free Software Foundation, version 2.\r\n";
char version13[80] = " This program comes with ABSOLUTELY NO WARRANTY.\r\n";
char instr[100] ="\r\nOn the command you must include six items in the following order, they are:\r\n";
char instr1[80] =" 1. The device name Ex: ttyS0 for com1, ttyS1 for com2, etc\r\n";
char instr2[80] =" 2. Baud Rate Ex: 38400 \r\n";
char instr3[80] =" 3. Number of Data Bits Ex: 8 \r\n";
char instr4[80] =" 4. Number of Stop Bits Ex: 0 or 1\r\n";
char instr5[80] =" 5. Parity Ex: 0=none, 1=odd, 2=even\r\n";
char instr6[80] =" 6. Format of data received: 1=hex, 2=dec, 3=hex/asc, 4=dec/asc, 5=asc\r\n";
char instr7[80] =" Example command line: com ttyS0 38400 8 0 0 4 \r\n";
char Param_strings[7][80];
char message[90];
int fd, tty, c, res, i, error;
char In1, Key;
struct termios oldtio, newtio; //place for old and new port settings for serial port
struct termios oldkey, newkey; //place tor old and new port settings for keyboard teletype
struct sigaction saio; //definition of signal action
char buf[255]; //buffer for where data is put
input = fopen("/dev/tty", "r"); //open the terminal keyboard
output = fopen("/dev/tty", "w"); //open the terminal screen
if (!input || !output)
{
fprintf(stderr, "Unable to open /dev/tty\n");
exit(1);
}
error=0;
fputs(version,output); //display the program introduction
fputs(version1,output);
fputs(version2,output);
fputs(version3,output);
fputs(version4,output);
fputs(version5,output);
fputs(version6,output);
fputs(version7,output);
fputs(version8,output);
fputs(version9,output);
fputs(version10,output);
fputs(version11,output);
fputs(version12,output);
fputs(version13,output);
//read the parameters from the command line
if (Parm_Count==7) //if there are the right number of parameters on the command line
{
for (i=1; i&#60Parm_Count; i++) // for all wild search parameters
{
strcpy(Param_strings[i-1],Parms[i]);
}
i=sscanf(Param_strings[0],"%s",devicename);
if (i != 1) error=1;
i=sscanf(Param_strings[1],"%li",&Baud_Rate);
if (i != 1) error=1;
i=sscanf(Param_strings[2],"%i",&Data_Bits);
if (i != 1) error=1;
i=sscanf(Param_strings[3],"%i",&Stop_Bits);
if (i != 1) error=1;
i=sscanf(Param_strings[4],"%i",&Parity);
if (i != 1) error=1;
i=sscanf(Param_strings[5],"%i",&Format);
if (i != 1) error=1;
sprintf(message,"Device=%s, Baud=%li\r\n",devicename, Baud_Rate); //output the received setup parameters
fputs(message,output);
sprintf(message,"Data Bits=%i Stop Bits=%i Parity=%i Format=%i\r\n",Data_Bits, Stop_Bits, Parity, Format);
fputs(message,output);
} //end of if param_count==7
if ((Parm_Count==7) && (error==0)) //if the command line entries were correct
{ //run the program
tty = open("/dev/tty", O_RDWR | O_NOCTTY | O_NONBLOCK); //set the user console port up
tcgetattr(tty,&oldkey); // save current port settings //so commands are interpreted right for this program
// set new port settings for non-canonical input processing //must be NOCTTY
newkey.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newkey.c_iflag = IGNPAR;
newkey.c_oflag = 0;
newkey.c_lflag = 0; //ICANON;
newkey.c_cc[VMIN]=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -