﻿{"id":1131,"date":"2010-07-31T16:49:07","date_gmt":"2010-07-31T08:49:07","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=1131"},"modified":"2010-07-31T16:50:30","modified_gmt":"2010-07-31T08:50:30","slug":"linux%e4%b8%b2%e5%8f%a3%e9%80%9a%e4%bf%a1%e7%9a%84c%e8%af%ad%e8%a8%80%e6%ba%90%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=1131","title":{"rendered":"Linux\u4e32\u53e3\u901a\u4fe1\u7684C\u8bed\u8a00\u6e90\u4ee3\u7801"},"content":{"rendered":"<p>\u5176\u5b9e\u90a3\u4e2a\u4e32\u53e3\u662f\u4e00\u4e2a\u5f88\u590d\u6742\u7684\u4e1c\u897f\u7684\uff0c\u8fd9\u73a9\u610f\u4e0d\u662f\u4e00\u4e24\u53e5\u8bdd\u5c31\u8bf4\u5f97\u6e05\u695a\u7684\u3002<\/p>\n<p>\u3000\u3000\u5982\u679c\u8981\u771f\u7684\u641e\u660e\u767d\u5b83\uff0c\u975e\u770b\u4e00\u672c\u539a\u539a\u7684\u4e66\u4e0d\u53ef\uff0c\u628a\u5b83\u5b8c\u5168\u63cf\u8ff0\u51fa\u6765\u4e5f\u53ef\u4ee5\u5199\u4e00\u672c\u4e66\u4e86\u3002<\/p>\n<p>\u3000\u3000\u5728Linux\u4e0b\uff0c\u6709\u4e00\u4e2a\u5f88\u5927\u7684\u597d\u5904\uff0c\u6240\u6709\u7684\u786c\u4ef6\u8bbe\u5907\u90fd\u865a\u62df\u6210\u4e86\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n<p>\u3000\u3000\u6210\u4e86\u6587\u4ef6\uff0c\u5c31\u53ef\u4ee5\u5f88\u7b80\u5355\u5730\u64cd\u4f5c\u5b83\u4e86\uff0c\u50cf\u6587\u4ef6\u4e00\u6837\u6253\u5f00\u5173\u95ed\uff0c\u7684\u786e\u5c4f\u853d\u4e86\u5b83\u7684\u590d\u6742\u6027\u3002<a name=\"entrymore\"><\/a><\/p>\n<p>\u3000\u3000\u4e0b\u9762\u662f\u4e00\u4e2aLinux\u7684\u4e32\u53e3\u901a\u4fe1\u7684\u53c2\u8003\u4ee3\u7801\uff0c\u7531\u6211\u674e\u53ef\u5148\u6574\u7406\u51fa\u6765\u7684\uff0c\u53ea\u662f\u5b9e\u73b0\u4e86\u529f\u80fd\uff0c\u6ca1\u592a\u591a\u590d\u6742\u7684\u8bbe\u7f6e\u548c\u73af\u5883\u7684\u8003\u8651\u3002<\/p>\n<p>\u3000\u3000\u8fd9\u6bb5\u4ee3\u7801\u5305\u62ec\u4e86\u4e32\u53e3\u7684\u521d\u59cb\u5316\uff0c\u53d1\u9001\u6570\u636e\uff0c\u63a5\u6536\u6570\u636e\uff0c\u53ea\u662f\u5b9e\u73b0\u529f\u80fd\uff0c\u66f4\u591a\u7684\u4ee3\u7801\u8bf7\u81ea\u884c\u6dfb\u52a0\u3002<\/p>\n<p>\u3000\u3000\u4ee3\u7801\u5982\u4e0b\uff0c\u4ec5\u4f5c\u53c2\u8003\uff0c\u6211\u662f\u53c2\u8003\u4e86\u90a3\u4e2a\u82f1\u6587\u7684\u8d44\u6599\u6574\u7406\u51fa\u6765\u7684\uff0c\u4e0d\u4e00\u5b9a\u5168\u5bf9\u3002<\/p>\n<p>\u3000\u3000\u6211\u674e\u53ef\u5148\u5df2\u5c3d\u6211\u6700\u5927\u6240\u80fd\u53bb\u7cbe\u7b80\u4ee3\u7801\u4e86\uff0c\u66f4\u7b80\u5355\u7684\u5e94\u8be5\u4e0d\u884c\u4e86\u3002<\/p>\n<p>\u3000\u3000#include &lt;stdio.h&gt;<br \/>\n\u3000\u3000#include &lt;sys\/ioctl.h&gt;<br \/>\n\u3000\u3000#include &lt;fcntl.h&gt;<br \/>\n\u3000\u3000#include &lt;termios.h&gt;<br \/>\n\u3000\u3000#include &lt;stdlib.h&gt;<\/p>\n<p>\u3000\u3000struct termios tio;<\/p>\n<p>\u3000\u3000int init_tty(struct termios *oldtio){<br \/>\n\u3000\u3000 int fd;<br \/>\n\u3000\u3000 struct termios newtio;<\/p>\n<p>\u3000\u3000 if((fd = open(&#8220;\/dev\/ttyS0&#8221;, O_RDWR)) == -1)return -1;<\/p>\n<p>\u3000\u3000 fcntl(fd, F_SETFL, 0);<br \/>\n\u3000\u3000 tcgetattr(fd, oldtio);<br \/>\n\u3000\u3000 bzero(&amp;newtio, sizeof(newtio));<br \/>\n\u3000\u3000 cfsetispeed(&amp;newtio, B115200);<br \/>\n\u3000\u3000 cfsetospeed(&amp;newtio, B115200);<br \/>\n\u3000\u3000 newtio.c_cflag |= CLOCAL|CREAD;<br \/>\n\u3000\u3000 newtio.c_cflag &amp;= ~CSIZE;<br \/>\n\u3000\u3000 newtio.c_cflag |= CS8;<br \/>\n\u3000\u3000 newtio.c_cflag &amp;= ~PARENB;<br \/>\n\u3000\u3000 newtio.c_cflag &amp;= ~CSTOPB;<br \/>\n\u3000\u3000 newtio.c_cflag &amp;= ~CRTSCTS;<br \/>\n\u3000\u3000 newtio.c_lflag &amp;= ~(ICANON | ECHO | ECHOE | ISIG);<br \/>\n\u3000\u3000 newtio.c_oflag &amp;= ~OPOST;<br \/>\n\u3000\u3000 tcflush(fd, TCIFLUSH);<br \/>\n\u3000\u3000 newtio.c_cc[VTIME] = 100;<br \/>\n\u3000\u3000 newtio.c_cc[VMIN] = 0;<br \/>\n\u3000\u3000 tcsetattr(fd, TCSANOW, &amp;newtio);<\/p>\n<p>\u3000\u3000 return fd;<br \/>\n\u3000\u3000}<\/p>\n<p>\u3000\u3000int send(int ftty, char *str){<br \/>\n\u3000\u3000 return (!write(ftty, str, strlen(str))) ? -1 : 1;<br \/>\n\u3000\u3000}<\/p>\n<p>\u3000\u3000int read(int ftty, int streamfd){<br \/>\n\u3000\u3000 int n, max_fd, len, count=0;<br \/>\n\u3000\u3000 char buffer[640*500*3], *ptr = buffer;<br \/>\n\u3000\u3000 fd_set input;<br \/>\n\u3000\u3000 struct timeval timeout;<\/p>\n<p>\u3000\u3000 FD_ZERO(&amp;input);<br \/>\n\u3000\u3000 FD_SET(ftty, &amp;input);<br \/>\n\u3000\u3000 max_fd = ftty+1;<\/p>\n<p>\u3000\u3000 while(1){<br \/>\n\u3000\u3000 timeout.tv_sec = 1;<br \/>\n\u3000\u3000 timeout.tv_usec = 0;<br \/>\n\u3000\u3000 if((n = select(max_fd, &amp;input, NULL, NULL, &amp;timeout)) &lt;= 0)return -1;<br \/>\n\u3000\u3000 else{ioctl(ftty, FIONREAD, &amp;len);if(!len)return -1;}<br \/>\n\u3000\u3000 len = read(ftty, ptr, len);<br \/>\n\u3000\u3000 write(streamfd, ptr, len);<br \/>\n\u3000\u3000 ptr += len;<br \/>\n\u3000\u3000 count += len;<br \/>\n\u3000\u3000 }<\/p>\n<p>\u3000\u3000 buffer[count] = 0;<br \/>\n\u3000\u3000 printf(&#8220;reading %d data:%s\\n&#8221;, count, buffer);<br \/>\n\u3000\u3000}<\/p>\n<p>\u3000\u3000int main(int argc, char *argv[]){<br \/>\n\u3000\u3000 int fd, socket = 0xff;<br \/>\n\u3000\u3000 struct termios oldtio;<\/p>\n<p>\u3000\u3000 if((fd = init_tty(&amp;oldtio)) &lt; 0)return -1;<br \/>\n\u3000\u3000 if(send(fd, socket) &lt; 1)return -1;<br \/>\n\u3000\u3000 if(socket = open(&#8220;\/home\/likexian\/likexian.txt&#8221;, O_RDWR))read(fd, socket);<\/p>\n<p>\u3000\u3000 close(socket);<br \/>\n\u3000\u3000 tcsetattr(fd, TCSANOW, &amp;oldtio);<br \/>\n\u3000\u3000}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5176\u5b9e\u90a3\u4e2a\u4e32\u53e3\u662f\u4e00\u4e2a\u5f88\u590d\u6742\u7684\u4e1c\u897f\u7684\uff0c\u8fd9\u73a9\u610f\u4e0d\u662f\u4e00\u4e24\u53e5\u8bdd\u5c31\u8bf4\u5f97\u6e05\u695a\u7684\u3002 \u3000\u3000\u5982\u679c\u8981\u771f\u7684\u641e\u660e\u767d\u5b83\uff0c\u975e\u770b\u4e00\u672c\u539a\u539a\u7684\u4e66\u4e0d [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[24,23,25],"class_list":["post-1131","post","type-post","status-publish","format-standard","hentry","category-22","tag-c","tag-linux","tag-25"],"views":1337,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1131"}],"version-history":[{"count":4,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1131\/revisions"}],"predecessor-version":[{"id":1135,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/1131\/revisions\/1135"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1131"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}