📄 linux 下串口编程入门.htm
字号:
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n"); return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
</CODE></PRE></TD></TR></TBODY></TABLE>
<P><B xmlns:dw="http://www.ibm.com/developerworks/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">需要注意的是:</B> </P>
<P>如果不是开发终端之类的,只是串口传输数据,而不需要串口来处理,那么使用原始模式(Raw Mode)方式来通讯,设置方式如下: </P><A
name=N10229><B></B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc
border=1><TBODY>
<TR>
<TD><PRE><CODE>
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
</CODE></PRE></TD></TR></TBODY></TABLE>
<P><A name=N10232><SPAN
class=atitle2>读写串口</SPAN></A><BR>设置好串口之后,读写串口就很容易了,把串口当作文件读写就是。 </P>
<UL xmlns:dw="http://www.ibm.com/developerworks/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<LI>发送数据
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc
border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
char buffer[1024];int Length;int nByte;nByte = write(fd, buffer ,Length)
</CODE></PRE></TD></TR></TBODY></TABLE>
<LI>读取串口数据
<P>使用文件操作read函数读取,如果设置为原始模式(Raw Mode)传输数据,那么read函数返回的字符数是实际串口收到的字符数。
</P>
<P>可以使用操作文件的函数来实现异步读取,如fcntl,或者select等来操作。 </P>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc
border=1>
<TBODY>
<TR>
<TD><PRE><CODE>
char buff[1024];int Len;int readByte = read(fd,buff,Len);
</CODE></PRE></TD></TR></TBODY></TABLE></LI></UL>
<P><A name=N10252><SPAN class=atitle2>关闭串口</SPAN></A><BR>关闭串口就是关闭文件。
</P><A name=N1025B><B></B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc
border=1><TBODY>
<TR>
<TD><PRE><CODE>
close(fd);
</CODE></PRE></TD></TR></TBODY></TABLE>
<P><A name=N10264><SPAN
class=atitle2>例子</SPAN></A><BR>下面是一个简单的读取串口数据的例子,使用了上面定义的一些函数和头文件 </P><A
name=N1026D><B></B></A><BR>
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc
border=1><TBODY>
<TR>
<TD><PRE><CODE>
/**********************************************************************代码说明:使用串口二测试的,发送的数据是字符,
但是没有发送字符串结束符号,所以接收到后,后面加上了结束符号。我测试使用的是单片机发送数据到第二个串口,测试通过。
**********************************************************************/
#define FALSE -1
#define TRUE 0
/*********************************************************************/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
int main(int argc, char **argv){
int fd;
int nread;
char buff[512];
char *dev = "/dev/ttyS1"; //串口二
fd = OpenDev(dev);
set_speed(fd,19200);
if (set_Parity(fd,8,1,'N') == FALSE) {
printf("Set Parity Error\n");
exit (0);
}
while (1) //循环读取数据
{
while((nread = read(fd, buff, 512))>0)
{
printf("\nLen %d\n",nread);
buff[nread+1] = '\0';
printf( "\n%s", buff);
}
}
//close(fd);
// exit (0);
}
</CODE></PRE></TD></TR></TBODY></TABLE>
<P><A name=resources><SPAN class=atitle2>参考资料 </SPAN></A>
<UL>
<LI><A href="http://digilander.libero.it/robang/rubrica/serial.htm"
target=_blank xmlns:dw="http://www.ibm.com/developerworks/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Serial Programming
Guide for POSIX Operating Systems</A> <BR><BR>
<LI>Linux 的源代码<BR><BR>
<LI>代码下载: <A
href="http://www-128.ibm.com/developerworks/cn/linux/l-serials/main.c"
xmlns:dw="http://www.ibm.com/developerworks/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">代码</A>
<BR></LI></UL>
<P></P>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><A name=author1></A><SPAN
class=atitle2>关于作者</SPAN><BR>左锦,就职南沙资讯科技园,喜爱 Linux,Java
还有蓝天白云青山绿水。通过 <A href="mailto:zuo170@163.com"
xmlns:dw="http://www.ibm.com/developerworks/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">zuo170@163.com</A>和他联系。
</TD></TR></TBODY></TABLE><BR clear=all><IMG height=10 alt=""
src="Linux 下串口编程入门.files/c.gif" width=100 border=0><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD align=right width="100%">
<TABLE cellSpacing=0 cellPadding=0>
<TBODY>
<TR vAlign=top>
<TD align=right></TD>
<TD width=46>
<FORM
action=https://www-130.ibm.com/developerworks/secure/email-it.jsp><INPUT
type=hidden
value="Linux 操作系统从一开始就对串行口提供了很好的支持,本文就 Linux 下的串行口通讯编程进行简单的介绍。"
name=body><INPUT type=hidden value="Linux 下串口编程入门"
name=subject><INPUT type=hidden value=cn name=lang><INPUT
type=image src="Linux 下串口编程入门.files/icon-email.gif" border=0
name=email></FORM></TD></TR></TBODY></TABLE></TD>
<TD width=5><IMG height=1 alt="" src="Linux 下串口编程入门.files/c.gif"
width=5 border=0></TD></TR>
<TR vAlign=top>
<TD bgColor=#000000 colSpan=2><IMG height=1 alt=""
src="Linux 下串口编程入门.files/c.gif" width=100 border=0></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff colSpan=2><IMG height=8 alt=""
src="Linux 下串口编程入门.files/c.gif" width=100
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<TBODY>
<TR vAlign=top>
<TD>
<FORM
action=https://www-130.ibm.com/developerworks/secure/cnratings.jsp
method=get><INPUT type=hidden value="Linux 下串口编程入门"
name=ArticleTitle><INPUT type=hidden value=Linux name=Zone><INPUT
type=hidden
value=http://www.ibm.com/developerworks/cn/thankyou/index.html
name=RedirectURL><INPUT type=hidden value=china name=localsite>
<SCRIPT language=javascript>document.write('<input type="HIDDEN" name="url" value="'+location.href+'">');</SCRIPT>
<A name=rating><SPAN class=atitle2>对本文的评价</SPAN></A>
<P>您对这篇文章的看法如何?</P>
<TABLE cellSpacing=0 cellPadding=0 width=600 border=0>
<TBODY>
<TR>
<TD colSpan=5><IMG height=8 alt=""
src="Linux 下串口编程入门.files/c.gif" width=100 border=0></TD></TR>
<TR vAlign=top>
<TD width="21%"><INPUT type=radio value=1 name=Rating>太差!
(1)</TD>
<TD width="17%"><INPUT type=radio value=2 name=Rating>需提高
(2)</TD>
<TD width="24%"><INPUT type=radio value=3 name=Rating>一般;尚可
(3)</TD>
<TD width="17%"><INPUT type=radio value=4 name=Rating>好文章
(4)</TD>
<TD width="21%"><INPUT type=radio value=5
name=Rating>真棒!(5)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA name=Comments rows=5 wrap=virtual cols=60></TEXTAREA><BR><BR><INPUT type=submit value=提交反馈意见></FORM></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff><IMG height=8 alt=""
src="Linux 下串口编程入门.files/c.gif" width=100
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width="100%">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD width="100%"><IMG height=4 alt=""
src="Linux 下串口编程入门.files/c.gif" width=2 border=0><BR><A
class=bctl
href="http://www-128.ibm.com/developerworks/cn/">developerWorks
中国</A><SPAN class=bct> > </SPAN><A class=bctl
href="http://www-128.ibm.com/developerworks/cn/linux/">Linux</A>
<SPAN class=bct>> </SPAN></TD>
<TD align=right width=136><A
href="http://www-128.ibm.com/developerworks/cn/"><IMG
height=24 alt=developerWorks
src="Linux 下串口编程入门.files/dwlogo-small.gif" width=136
border=0></A></TD>
<TD width=5><IMG height=1 alt=""
src="Linux 下串口编程入门.files/c.gif" width=5
border=0></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD>
<TD width=1><IMG height=1 alt="" src="Linux 下串口编程入门.files/c.gif" width=1
border=0></TD></TR></TBODY></TABLE><!-- IBM FOOTER -->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=bbg height=19>
<TABLE cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD><SPAN class=spacer> </SPAN><A
class=mainlink href="http://www.ibm.com/cn/ibm/index.shtml">关于
IBM</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink
href="http://www.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</A></TD>
<TD class=footer-divider width=27> </TD>
<TD><A class=mainlink href="http://www.ibm.com/contact/">联系
IBM</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!-- end footer -->
<SCRIPT language=JavaScript1.2 src="Linux 下串口编程入门.files/stats.js"
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG height=1 alt="" src="D:\linux\串口\Linux 下串口编程入门.files\c(1).gif"
width=1 border=0></NOSCRIPT></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -