📄 linux 下串口编程入门.htm
字号:
*@param databits 类型 int 数据位 取值 为 7 或者8
*@param stopbits 类型 int 停止位 取值为 1 或者2
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
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>
<P><B>需要注意的是:</B></P>
<P>如果不是开发终端之类的,只是串口传输数据,而不需要串口来处理,那么使用原始模式(Raw Mode)方式来通讯,设置方式如下: </P>
<P>
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0
width="100%"><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>
<P><A name=5><SPAN
class=atitle2>读写串口</SPAN></A><BR>设置好串口之后,读写串口就很容易了,把串口当作文件读写就是。 </P>
<UL>
<LI>发送数据
<P>
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD><PRE><CODE>
char buffer[1024];int Length;int nByte;nByte = write(fd, buffer ,Length)
</CODE></PRE></TD></TR></TBODY></TABLE></P>
<LI>读取串口数据
<P>使用文件操作read函数读取,如果设置为原始模式(Raw Mode)传输数据,那么read函数返回的字符数是实际串口收到的字符数。
</P>
<P>可以使用操作文件的函数来实现异步读取,如fcntl,或者select等来操作。 </P>
<P>
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD><PRE><CODE>
char buff[1024];int Len;int readByte = read(fd,buff,Len);
</CODE></PRE></TD></TR></TBODY></TABLE></P></LI></UL>
<P><A name=6><SPAN class=atitle2>关闭串口</SPAN></A><BR>关闭串口就是关闭文件。 </P>
<P>
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0
width="100%"><TBODY>
<TR>
<TD><PRE><CODE>
close(fd);
</CODE></PRE></TD></TR></TBODY></TABLE></P>
<P><A name=7><SPAN
class=atitle2>例子</SPAN></A><BR>下面是一个简单的读取串口数据的例子,使用了上面定义的一些函数和头文件 </P>
<P>
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0
width="100%"><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><!-- RESOURCES-->
<P><A name=resources><SPAN class=atitle2>参考资料</SPAN></A>
<UL><!-- Comment out list item below if there is no forum for this article-->
<LI><A href="http://digilander.libero.it/robang/rubrica/serial.htm"
target=_blank>Serial Programming Guide for POSIX Operating Systems</A>
<LI>Linux 的源代码
<LI>代码下载: <A
href="http://www-900.cn.ibm.com/developerWorks/cn/linux/l-serials/main.c">代码</A>
</LI></UL>
<P></P>
<P><A name=author><SPAN class=atitle2>关于作者</SPAN></A></P>
<P>左锦,就职南沙资讯科技园,喜爱 Linux,Java 还有蓝天白云青山绿水。通过 <A
href="mailto:zuo170@163.com">zuo170@163.com</A> 和他联系。 <!-- END PAPER BODY--></P></TD>
<TD width=10><IMG alt="" border=0 height=1 src="Linux 下串口编程入门.files/c.gif"
width=10></TD></TR></TBODY></TABLE><BR clear=all><IMG alt="" border=0 height=10
src="Linux 下串口编程入门.files/c.gif" width=100><BR>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR vAlign=top>
<TD align=right width="100%"><A
href="http://www-900.cn.ibm.com/developerWorks/cn/linux/l-serials/index.shtml#top">到页首</A></TD>
<TD width=5><IMG alt="" border=0 height=1 src="Linux 下串口编程入门.files/c.gif"
width=5></TD></TR>
<TR vAlign=top>
<TD bgColor=#000000 colSpan=2><IMG alt="" border=0 height=1
src="Linux 下串口编程入门.files/c.gif" width=100></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff colSpan=2><IMG alt="" border=0 height=8
src="Linux 下串口编程入门.files/c.gif" width=100></TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=10 cellSpacing=0 width="100%">
<TBODY>
<TR vAlign=top>
<TD>
<FORM action=/developerWorks/cn/cnratings.nsf/RateArticle?CreateDocument
method=post name=getURL><INPUT name=ArticleTitle type=hidden
value="Linux 下串口编程入门"> <INPUT name=url type=hidden>
<SCRIPT language=javascript>getURL();</SCRIPT>
<INPUT name=Zone type=hidden value=linux> <INPUT name=RedirectURL
type=hidden value=/developerWorks/cn/thankyou/feedback-linux.html> <A
name=rating><B>您对这篇文章的看法如何?</B></A>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=600>
<TBODY>
<TR>
<TD colSpan=5><IMG alt="" border=0 height=8
src="Linux 下串口编程入门.files/c.gif" width=100></TD></TR>
<TR vAlign=top>
<TD width="16%"><INPUT name=Rating type=radio value=5>真棒!(5)</TD>
<TD width="20%"><INPUT name=Rating type=radio value=4>好材料 (4)</TD>
<TD width="24%"><INPUT name=Rating type=radio value=3>一般;尚可 (3)</TD>
<TD width="22%"><INPUT name=Rating type=radio value=2>需提高 (2)</TD>
<TD width="18%"><INPUT name=Rating type=radio value=1>太差!
(1)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA cols=60 name=Comments rows=5 wrap=virtual></TEXTAREA><BR><BR><INPUT type=submit value=提交反馈意见></FORM></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff><IMG alt="" border=0 height=8
src="Linux 下串口编程入门.files/c.gif" width=100></TD></TR></TBODY></TABLE>
<TABLE border=0 cellPadding=0 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD align=right>(c) Copyright IBM Corp. 2001, (c) Copyright IBM China
2001, All Right Reserved</TD></TR>
<TR vAlign=top>
<TD class=bbg height=21> <A class=mainlink
href="http://www-900.cn.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/index.shtml&origin=dwhead">关于
IBM</A><SPAN class=divider> | </SPAN><A
class=mainlink
href="http://www-900.cn.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/privacy/index.shtml&origin=dwhead">隐私条约</A><SPAN
class=divider> | </SPAN><A class=mainlink
href="http://www-900.cn.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/legal/index.shtml&origin=dwhead">使用条款</A><SPAN
class=divider> | </SPAN><A class=mainlink
href="http://www-900.cn.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/contact/index.shtml&origin=dwhead">联系
IBM</A></TD></TR></TBODY></TABLE>
<SCRIPT language=JavaScript1.2 src="" type=text/javascript></SCRIPT>
<NOSCRIPT><IMG alt="" border=0 height=1 src="" width=1></NOSCRIPT>
</A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -