<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[转]linux下串口编程的个人心得，百度文库串口文章打包]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Wed, 03 Mar 2010 03:34:36 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	一开始在网上找资料，最多是《Linux Serial HOWTO 中譯版》，浏览地址：http://linux.cis.nctu.edu.tw/chinese/how-to/Serial-HOWTO.html。以及众多这个版本的摘选，内容是大同小异的。<br/><br/>后来发现，其实串口编程的实质就是多串口属性的设置。<br/>而属性也就下面这么几个：<br/>c_cflag Control options<br/>c_lflag Line options<br/>c_iflag Input options<br/>c_oflag Output options<br/>c_cc Control characters<br/>c_ispeed Input baud (new interface)<br/>c_ospeed Output baud (new interface)<br/>关键是理解有那些属性参数可以设置以及是什么意思。<br/><br/>继续找资料。发现下面的经典文章，可以说基本上所有的串口编程的文章都或多或少的参考了这篇文章，《Serial Programming Guide for POSIX Operating Systems》是一定要看的，我读的是5th Edition, 3rd Revision - Updated March 11, 2003，下载地址：http://www.easysw.com/~mike/serial/index.html<br/><br/>当把这篇文章看完之后，基本可以解决串口的设置问题了。不过这是一个英文的版本，本人打算在暑假里把他翻译为中文版本。<br/><br/>关于具体的例子：<br/>http://www.comptechdoc.org/os/linux/programming/c/linux_pgcserial.html不错，很详细，不过比较复杂。<br/>中文的《Linux Serial HOWTO 中譯版》上面就有不少，也很值得参考。<br/><br/>下面的是我的程序，一个串口读取，往mysql数据库写数据的程序：<br/><br/><div class="code">#include &lt;sys/types.h&gt;<br/>#include &lt;sys/stat.h&gt;<br/>#include &lt;fcntl.h&gt;<br/>#include &lt;termios.h&gt;<br/>#include &lt;errno.h&gt;<br/>#include &lt;ctype.h&gt;<br/>#include &lt;stdio.h&gt;<br/>#include &lt;stdlib.h&gt;<br/>#include &lt;string.h&gt;<br/>#include &lt;time.h&gt;<br/><br/>#include &quot;mysql.h&quot;<br/><br/>#define BAUDRATE B9600<br/>#define DEVICE &quot;/dev/ttyS0&quot;<br/>#define _POSIX_SOURCE 1<br/>#define FALSE 0<br/>#define TRUE 1<br/><br/>int insertdb(int d1, int d2, int d3, int d4) &#123;<br/> MYSQL connect;<br/> int res, no1, no2, sd1, sd2;<br/> char *query = &quot;INSERT INTO mydata&nbsp;&nbsp;( stime, sno1, sno2, sdata1, sdata2 ) VALUES ( &#039;%s&#039;, %d, %d, %x, %x)&quot;;<br/> char *sql, *st1;<br/> struct tm *ptr;<br/> time_t lt;<br/> <br/> no1 = d1;<br/> no2 = d2;<br/> sd1 = d3;<br/> sd2 = d4;<br/> lt = time(NULL);<br/> ptr = localtime(&amp;lt);<br/> st1 = (char *)asctime(ptr);<br/> st1&#91;strlen(st1) -1 &#93; = &#039;&#92;0&#039;;<br/> sql = (char *)malloc(255*sizeof(char));<br/> sprintf(sql, query, st1, no1, no2, sd1, sd2);<br/> <br/>/* debug here<br/> printf(&quot;%c : &quot;, st1&#91;strlen(st1)&#93;);<br/> printf(&quot;%s : %d :&quot;,sql, strlen(st1));<br/> return EXIT_SUCCESS;<br/>*/<br/> <br/> mysql_init(&amp;connect);<br/> <br/> if(mysql_real_connect(&amp;connect, &quot;localhost&quot;, &quot;root&quot;, &quot;root&quot;, &quot;mytest&quot;, 0, NULL, 0)) &#123;<br/>&nbsp;&nbsp;printf(&quot;connect success!&#92;n&quot;);<br/><br/>&nbsp;&nbsp;res = mysql_query(&amp;connect, sql);<br/><br/>&nbsp;&nbsp;if(!res) &#123;<br/>&nbsp;&nbsp; printf(&quot;insert success!&#92;n&quot;);<br/>&nbsp;&nbsp;&#125; else &#123;<br/>&nbsp;&nbsp; fprintf(stderr, &quot;insert error %d: %s&#92;n&quot;, mysql_errno(&amp;connect), mysql_error(&amp;connect));<br/>&nbsp;&nbsp; return EXIT_FAILURE;<br/>&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;mysql_close(&amp;connect);<br/> &#125; else &#123;<br/>&nbsp;&nbsp;fprintf(stderr, &quot;connect fail!&#92;n&quot;);<br/>&nbsp;&nbsp;return EXIT_FAILURE;<br/> &#125;<br/><br/> return EXIT_SUCCESS;<br/>&#125;<br/><br/>int main(void) &#123;<br/> int fd, res_w, res_r, i, j, k;<br/> struct termios oldtio,newtio;<br/> char inbuf&#91;255&#93;;<br/> char cbuf&#91;4&#93;;<br/> int buf&#91;4&#93;;<br/><br/> res_w = 0;<br/> res_r = 0;<br/> <br/> fd = open(DEVICE, O_RDWR &#124; O_NOCTTY ); // &#124; O_NDELAY);<br/> if(fd &lt; 0) &#123;<br/>&nbsp;&nbsp;perror(DEVICE);<br/>&nbsp;&nbsp;exit(-1);<br/> &#125;<br/><br/> tcgetattr(fd, &amp;oldtio);<br/><br/>bzero(&amp;newtio,sizeof(struct termios));<br/><br/>newtio.c_cflag&#124;= (CLOCAL &#124; CREAD);<br/>newtio.c_cflag&#124;=BAUDRATE;<br/>newtio.c_cflag&amp;=~CSTOPB;<br/>newtio.c_cflag&amp;=~PARENB;<br/>newtio.c_cflag&amp;=~CSIZE;<br/>newtio.c_cflag&#124;=CS8;<br/>newtio.c_cflag&amp;=~CRTSCTS;<br/><br/>newtio.c_lflag=0;<br/><br/>newtio.c_oflag=0;<br/><br/>newtio.c_cc&#91;VMIN&#93;=4;<br/>newtio.c_cc&#91;VTIME&#93;=0;<br/><br/>newtio.c_iflag&amp;=~(IXON&#124;IXOFF&#124;IXANY);<br/><br/>cfsetispeed(&amp;newtio, BAUDRATE);<br/>cfsetospeed(&amp;newtio, BAUDRATE);<br/><br/>tcsetattr(fd, TCSANOW, &amp;newtio);<br/><br/>tcflush(fd, TCIFLUSH);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cbuf&#91;0&#93; = 0x00; <br/>//&nbsp;&nbsp;cbuf&#91;1&#93; = 0x00;<br/><br/>&nbsp;&nbsp;j = 0;<br/><br/>for(k = 0; k &lt; 4; k++) &#123;<br/> switch (j) &#123;<br/>&nbsp;&nbsp;case 0: <br/>&nbsp;&nbsp;default:<br/>&nbsp;&nbsp; cbuf&#91;1&#93; = 0x00;<br/>&nbsp;&nbsp; j = 2;<br/>&nbsp;&nbsp; break;<br/>&nbsp;&nbsp;case 2:<br/>&nbsp;&nbsp; cbuf&#91;1&#93; = 0x02;<br/>&nbsp;&nbsp; j = 0;<br/>&nbsp;&nbsp; break;<br/> &#125;<br/>&nbsp;&nbsp; <br/> res_w = write(fd, cbuf, 2);<br/><br/>/* debug here <br/>printf(&quot;cbuf : %x %x &#92;n&quot;, cbuf&#91;0&#93;, cbuf&#91;1&#93;);<br/>printf(&quot;buf : %x : %x : %x : %x &#92;n&quot;, inbuf&#91;0&#93;, inbuf&#91;1&#93;, inbuf&#91;2&#93;, inbuf&#91;3&#93;);<br/>*/<br/> res_r = read(fd, &amp;inbuf, 255);<br/> <br/> if(res_r != -1) &#123;<br/>&nbsp;&nbsp;for(i = 0; i &lt; res_r; i++) &#123;<br/>&nbsp;&nbsp; buf&#91;i&#93; = (int)inbuf&#91;i&#93;;<br/>&nbsp;&nbsp; buf&#91;i&#93; = buf&#91;i&#93; &amp; 0xff;<br/>&nbsp;&nbsp;// printf(&quot; %x &quot;, buf&#91;i&#93;);&nbsp;&nbsp; <br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;printf(&quot;&#92;n&quot;);<br/>&nbsp;&nbsp;if(insertdb(buf&#91;0&#93;, buf&#91;1&#93;, buf&#91;2&#93;, buf&#91;3&#93;))<br/>&nbsp;&nbsp; printf(&quot;insert into db success!&quot;);<br/> &#125;<br/> else &#123;<br/>&nbsp;&nbsp;perror(&quot;read fail&quot;);<br/>&nbsp;&nbsp;exit(-1);<br/> &#125;// if end here<br/>&#125;// for end here <br/><br/> tcsetattr(fd, TCSANOW, &amp;oldtio);<br/><br/> close(fd);<br/> exit(0);<br/>&#125; // main end here</div><br/>串口相关资料：<br/><a href="attachment.php?fid=215">点击这里下载文件</a>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [转]linux下串口编程的个人心得，百度文库串口文章打包]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>