《嵌入式系統(tǒng)應(yīng)用》實(shí)驗(yàn)二 Linux編程初步_第1頁
《嵌入式系統(tǒng)應(yīng)用》實(shí)驗(yàn)二 Linux編程初步_第2頁
《嵌入式系統(tǒng)應(yīng)用》實(shí)驗(yàn)二 Linux編程初步_第3頁
《嵌入式系統(tǒng)應(yīng)用》實(shí)驗(yàn)二 Linux編程初步_第4頁
《嵌入式系統(tǒng)應(yīng)用》實(shí)驗(yàn)二 Linux編程初步_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

嵌入式實(shí)驗(yàn)?zāi)0鍖?shí)驗(yàn)二Linux編程初步實(shí)驗(yàn)?zāi)康恼莆詹粠Ь彺娴奈募蘒/O編程方法。掌握標(biāo)準(zhǔn)I/O編程方法(帶緩存)。掌握嵌入式Linux串口讀寫編程方法。實(shí)驗(yàn)環(huán)境1.PC機(jī)2.虛擬機(jī)軟件VMware3.UbuntuLinux操作系統(tǒng)三、實(shí)驗(yàn)內(nèi)容1.不帶緩存的文件I/O編程(1)open和close函數(shù)(p77)/*open1.c*/#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>intmain(void){intfd;/*調(diào)用open函數(shù),以可讀寫的方式打開,注意選項(xiàng)可以用“|”符號(hào)連接*/if((fd=open("/tmp/hello.c",O_CREAT|O_TRUNC|O_WRONLY,0600))<0){perror("open:");exit(1);}else{printf("Openfile:hello.c%d\n",fd);}if(close(fd)<0){perror("close:");exit(1);}elseprintf("Closehello.c\n");exit(0);}實(shí)驗(yàn)要求:運(yùn)行此程序,顯示運(yùn)行結(jié)果界面,open和close函數(shù)的功能分別是什么?read,write和lseek函數(shù)(p79-80)/*write1.c*/#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#defineMAXSIZEintmain(void){inti,fd,size,len;char*buf="Hello!I'mwritingtothisfile!";charbuf_r[10];len=strlen(buf);/*首先調(diào)用open函數(shù),并指定相應(yīng)的權(quán)限*/if((fd=open("/tmp/hello.c",O_CREAT|O_TRUNC|O_RDWR,0666))<0){perror("open:");exit(1);}elseprintf("openfile:hello.c%d\n",fd);/*調(diào)用write函數(shù),將buf中的內(nèi)容寫入到打開的文件中*/if((size=write(fd,buf,len))<0){perror("write:");exit(1);}elseprintf("Write:%s\n",buf);/*調(diào)用lseek函數(shù)將文件指針移到文件起始,并讀出文件中的10個(gè)字節(jié)*/lseek(fd,0,SEEK_SET);if((size=read(fd,buf_r,10))<0){perror("read:");exit(1);}elseprintf("readfromfile:%s\n",buf_r);if(close(fd)<0){perror("close:");exit(1);}elseprintf("Closehello.c\n");exit(0);}實(shí)驗(yàn)要求:1)運(yùn)行此程序,顯示運(yùn)行結(jié)果界面。2)read,write和lseek函數(shù)的功能分別是什么?(3)fcntl和lock函數(shù)(p82-85)/*fcntl_write.c測(cè)試文件寫入鎖主函數(shù)部分*/#include<unistd.h>#include<sys/file.h>#include<sys/types.h>#include<sys/stat.h>#include<stdio.h>#include<stdlib.h>/*lock_set函數(shù)*/voidlock_set(intfd,inttype);intmain(void){intfd;/*首先打開文件*/fd=open("hello",O_RDWR|O_CREAT,0666);if(fd<0){perror("open");exit(1);}/*給文件上寫入鎖*/lock_set(fd,F_WRLCK);getchar();/*給文件解鎖*/lock_set(fd,F_UNLCK);getchar();close(fd);exit(0);}/*lock_set函數(shù)*/voidlock_set(intfd,inttype){structflocklock;lock.l_whence=SEEK_SET;//賦值lock結(jié)構(gòu)體lock.l_start=0;lock.l_len=0;while(1){lock.l_type=type;/*根據(jù)不同的type值給文件上鎖或解鎖*/if((fcntl(fd,F_SETLK,&lock))==0){if(lock.l_type==F_RDLCK)printf("readlocksetby%d\n",getpid());elseif(lock.l_type==F_WRLCK)printf("writelocksetby%d\n",getpid());elseif(lock.l_type==F_UNLCK)printf("releaselockby%d\n",getpid());return;}/*判斷文件是否可以上鎖*/fcntl(fd,F_GETLK,&lock);/*判斷文件不能上鎖的原因*/if(lock.l_type!=F_UNLCK){/*該文件已有寫入鎖*/if(lock.l_type==F_RDLCK)printf("readlockalreadysetby%d\n",lock.l_pid);/*該文件已有讀取鎖*/elseif(lock.l_type==F_WRLCK)printf("writelockalreadysetby%d\n",lock.l_pid);getchar();}}}實(shí)驗(yàn)要求:1)在2個(gè)終端上運(yùn)行此程序,顯示運(yùn)行結(jié)果界面。2)fcntl和lock函數(shù)的功能是什么?3)同樣在兩個(gè)終端上運(yùn)行以下讀取鎖程序,顯示結(jié)果畫面:/*fcntl_read.c測(cè)試文件讀取鎖主函數(shù)部分*/#include<unistd.h>#include<sys/file.h>#include<sys/types.h>#include<sys/stat.h>#include<stdio.h>#include<stdlib.h>voidlock_set(intfd,inttype);intmain(void){intfd;fd=open("hello",O_RDWR|O_CREAT,0666);if(fd<0){perror("open");exit(1);}/*給文件上讀取鎖*/lock_set(fd,F_RDLCK);getchar();/*給文件接鎖*/lock_set(fd,F_UNLCK);getchar();close(fd);exit(0);}/*lock_set函數(shù)*/voidlock_set(intfd,inttype){structflocklock;lock.l_whence=SEEK_SET;//賦值lock結(jié)構(gòu)體lock.l_start=0;lock.l_len=0;while(1){lock.l_type=type;/*根據(jù)不同的type值給文件上鎖或解鎖*/if((fcntl(fd,F_SETLK,&lock))==0){if(lock.l_type==F_RDLCK)printf("readlocksetby%d\n",getpid());elseif(lock.l_type==F_WRLCK)printf("writelocksetby%d\n",getpid());elseif(lock.l_type==F_UNLCK)printf("releaselockby%d\n",getpid());return;}/*判斷文件是否可以上鎖*/fcntl(fd,F_GETLK,&lock);/*判斷文件不能上鎖的原因*/if(lock.l_type!=F_UNLCK){/*該文件已有寫入鎖*/if(lock.l_type==F_RDLCK)printf("readlockalreadysetby%d\n",lock.l_pid);/*該文件已有讀取鎖*/elseif(lock.l_type==F_WRLCK)printf("writelockalreadysetby%d\n",lock.l_pid);getchar();}}}(4)select函數(shù)(p87-89)/*select1.c*/#include<fcntl.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<time.h>intmain(void){intfds[2];charbuf[7];inti,rc,maxfd;fd_setinset1,inset2;structtimevaltv;/*首先按一定的權(quán)限打開hello1文件*/if((fds[0]=open("hello1",O_RDWR|O_CREAT,0666))<0)perror("openhello1");/*再按一定的權(quán)限打開hello2文件*/if((fds[1]=open("hello2",O_RDWR|O_CREAT,0666))<0)perror("openhello2");if((rc=write(fds[0],"Hello!\n",7)))printf("rc=%d\n",rc);lseek(fds[0],0,SEEK_SET);/*取出兩個(gè)文件描述符中的較大者*/maxfd=fds[0]>fds[1]?fds[0]:fds[1];/*初始化讀集合inset1,并在讀集合中加入相應(yīng)的描述集*/FD_ZERO(&inset1);FD_SET(fds[0],&inset1);/*初始化寫集合inset2,并在寫集合中加入相應(yīng)的描述集*/FD_ZERO(&inset2);FD_SET(fds[1],&inset2);tv.tv_sec=2;tv.tv_usec=0;/*循環(huán)測(cè)試該文件描述符是否準(zhǔn)備就緒,并調(diào)用select函數(shù)對(duì)相關(guān)文件描述符做對(duì)應(yīng)操作*/while(FD_ISSET(fds[0],&inset1)||FD_ISSET(fds[1],&inset2)){if(select(maxfd+1,&inset1,&inset2,NULL,&tv)<0)perror("select");else{if(FD_ISSET(fds[0],&inset1)){rc=read(fds[0],buf,7);if(rc>0){buf[rc]='\0';printf("read:%s\n",buf);}elseperror("read");}if(FD_ISSET(fds[1],&inset2)){rc=write(fds[1],buf,7);if(rc>0){buf[rc]='\0';printf("rc=%d,write:%s\n",rc,buf);}elseperror("write");sleep(10);}}}exit(0);}實(shí)驗(yàn)要求:1)運(yùn)行此程序,顯示運(yùn)行結(jié)果界面,體會(huì)I/o復(fù)用原理。2)select函數(shù)的功能是什么?2.標(biāo)準(zhǔn)I/O編程(1)文件的打開,關(guān)閉和寫入程序(p102):/*fwrite1.c*/#include<stdio.h>intmain(){FILE*stream;chars[3]={'a','b','c'};/*首先使用fopen打開文件,之后再調(diào)用fwrite寫入文件*/stream=fopen("what","w");i=fwrite(s,sizeof(char),nmemb,stream);printf("i=%d",i);fclose(stream);}實(shí)驗(yàn)要求:1)運(yùn)行此程序,顯示運(yùn)行結(jié)果界面。2)fopen,fwrite和fclose函數(shù)的功能是什么?(2)輸入輸出函數(shù)(p104)/*gets1.c*/#include<stdio.h>main(){chars[80];/*同上例,把fgets的結(jié)果作為fputs的輸入*/fputs(fgets(s,80,stdin),stdout);}實(shí)驗(yàn)要求:1)運(yùn)行此程序,顯示運(yùn)行結(jié)果界面。2)此處fputs和fgets函數(shù)的功能是什么?3.嵌入式Linux串口讀寫編程(p94-98)/*讀寫串口的主函數(shù)receive.c*/#include<stdio.h>#include<string.h>#include<sys/types.h>#include<errno.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<termios.h>#include<stdlib.h>/*讀串口程序*//*設(shè)置串口函數(shù)*/intset_opt(intfd,intnSpeed,intnBits,charnEvent,intnStop);/*打開串口函數(shù)*/intopen_port(intfd,intcomport);intmain(void){intfd;intnread,i;charbuff[]="Hello\n";if((fd=open_port(fd,1))<0) //打開串口{perror("open_porterror");return;}if((i=set_opt(fd,115200,8,'N',1))<0) //設(shè)置串口{perror("set_opterror");return;}printf("fd=%d\n",fd);fd=3;nread=read(fd,buff,8);//讀串口printf("nread=%d,%s\n",nread,buff);close(fd);return;}/*設(shè)置串口函數(shù)*/intset_opt(intfd,intnSpeed,intnBits,charnEvent,intnStop){structtermiosnewtio,oldtio;/*保存測(cè)試現(xiàn)有串口參數(shù)設(shè)置,在這里如果串口號(hào)等出錯(cuò),會(huì)有相關(guān)的出錯(cuò)信息*/if(tcgetattr(fd,&oldtio)!=0){perror("SetupSerial1");return-1;}bzero(&newtio,sizeof(newtio));/*步驟一,設(shè)置字符大小*/newtio.c_cflag|=CLOCAL|CREAD;newtio.c_cflag&=~CSIZE;/*設(shè)置停止位*/switch(nBits){case7:newtio.c_cflag|=CS7;break;case8:newtio.c_cflag|=CS8;break;}/*設(shè)置奇偶校驗(yàn)位*/switch(nEvent){case'O'://奇數(shù)newtio.c_cflag|=PARENB;newtio.c_cflag|=PARODD;newtio.c_iflag|=(INPCK|ISTRIP);break;case'E'://偶數(shù)newtio.c_iflag|=(INPCK|ISTRIP);newtio.c_cflag|=PARENB;newtio.c_cflag&=~PARODD;break;case'N'://無奇偶校驗(yàn)位newtio.c_cflag&=~PARENB;break;}/*設(shè)置波特率*/switch(nSpeed){case2400:cfsetispeed(&newtio,B2400);cfsetospeed(&newtio,B2400);break;case4800:cfsetispeed(&newtio,B4800);cfsetospeed(&newtio,B4800);break;case9600:cfsetispeed(&newtio,B9600);cfsetospeed(&newtio,B9600);break;case115200:cfsetispeed(&newtio,B115200);cfsetospeed(&newtio,B115200);break;case460800:cfsetispeed(&newtio,B460800);cfsetospeed(&newtio,B460800);break;default:cfsetispeed(&newtio,B9600);cfsetospeed(&newtio,B9600);break;}/*設(shè)置停止位*/if(nStop==1)newtio.c_cflag&=~CSTOPB;elseif(nStop==2)newtio.c_cflag|=CSTOPB;/*設(shè)置等待時(shí)間和最小接收字符*/newtio.c_cc[VTIME]=0;newtio.c_cc[VMIN]=0;/*處理未接收字符*/tcflush(fd,TCIFLUSH);/*激活新配置*/if((tcsetattr(fd,TCSANOW,&newtio))!=0){perror("comseterror");return-1;}printf("setdone!\n");return0;}/*打開串口函數(shù)*/intopen_port(i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論