版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
ARM嵌入式Linux系統(tǒng)開(kāi)發(fā)詳解.Linux.c應(yīng)用程序開(kāi)發(fā)詳解源代碼
#include<stdio.h>intmain(){ printf("HelloWorld!\n"); return0;}//從文件讀寫(xiě)成塊數(shù)據(jù)#include<stdio.h>intmain(){ intbuf[1024]={0}; intp; FILE*fp=fopen(“./blk_file.dat”,“rb+”); if(NULL=fp) return-1; fwrite(buf,sizeof(int),1024,fp); //把1024個(gè)數(shù)據(jù)塊寫(xiě)入文件流fp,每個(gè)數(shù)據(jù)塊4個(gè)字節(jié) /*修改buf的數(shù)據(jù),供讀取后比較*/ for(i=0;i<16;i++) buf[i]=-1; p=&buf[0];//設(shè)置指針p指向buf,供從文件讀取數(shù)據(jù)使用 fread(p,sizeof(int),1024,fp); //從文件讀取1024個(gè)數(shù)據(jù)塊到buf,每個(gè)數(shù)據(jù)塊4個(gè)字節(jié) /*打印從文件讀取的二進(jìn)制數(shù)據(jù)*/ for(i=0;i<1024;i++) printf("buf[%d]=%d\n",i,buf[i]); fclose(fp); //最后別忘了關(guān)閉文件 return0;}#include<stdio.h>intmain(){#include<unistd.h>#include<fcntl.h>#include<stdio.h>#include<errno.h>externinterrno;intmain(){ intfd; //注意,文件描述符是整型值 charbuf[64]="thisisaposixfile!(line1)\n"; off_tcurr_pos; fd=open("./posix.data",O_CREAT|O_RDWR|O_EXCL,S_IRWXU);//打開(kāi)一個(gè)不存在的文件,并創(chuàng)建文件,權(quán)限是用戶(hù)可讀寫(xiě)執(zhí)行 if(-1==fd){//檢查文件打開(kāi)是否成功 switch(errno){ caseEEXIST: //文件已存在 printf("Fileexist!\n"); break; default: //其他錯(cuò)誤 printf("openfilefail!\n"); break; } return0; } write(fd,buf,strlen(buf)); //把字符串寫(xiě)入文件 curr_pos=lseek(fd,0,SEEK_CUR); //取得當(dāng)前文件偏移量位置 printf("FilePointat:%d\n",curr_pos); lseek(fd,0,SEEK_SET); //把文件偏移量移動(dòng)到文件開(kāi)頭 strcpy(buf,"FilePointerMoved!\n"); write(fd,buf,strlen(buf)); //把新的數(shù)據(jù)寫(xiě)入文件 file_mode=fcntl(fd,F_GETFL); //獲取文件狀態(tài)標(biāo)記 if(-1!=file_mode){ switch(file_mode&O_ACCMODE){ //檢查文件狀態(tài) caseO_RDONLY: printf("filemodeisREADONLY\n"); break; caseO_WRONLY: printf("filemodeisWRITEONLY\n"); break; caseO_RDWR: printf("filemodeisREAD&WRITE\n"); break; } } close(fd); return0; }//test.cintg_var_a=0; //存放在全局已初始化數(shù)據(jù)區(qū)charg_var_b; //存放在BSS區(qū)(未初始化全局變量)intmain(){ intvar_a; //存放在棧區(qū) charvar_str[]=“string1”; //存放在棧區(qū) char*p_str1,*p_str2; //存放在棧區(qū) char*p_str3=“string2”; //存放在已初始化數(shù)據(jù)區(qū)ptr_str3存放在棧區(qū) staticintvar_b=100; //全局靜態(tài)數(shù)據(jù),存放在已初始化區(qū) p_str1=(char*)malloc(1024); //從堆區(qū)分配1024字節(jié)內(nèi)存 p_str2=(char*)malloc(2048); //從堆區(qū)分配2048字節(jié)內(nèi)存 free(p_str1); free(p_str2); return0;}//filenamegetenv.c–獲取環(huán)境變量測(cè)試#include<stdio.h>#include<stdlib.h>intmain(){char*env_path="PATH"; //打算獲取的環(huán)境變量名稱(chēng)char*env_value=NULL; //環(huán)境變量值env_value=getenv(env_path); //使用系統(tǒng)函數(shù)獲取指定環(huán)境變量if(NULL==env_value) //檢查是否獲取到變量的值printf("Notfound!\n");printf("GetEnvPATH:\n%s",env_value); //輸出PATH環(huán)境變量的值return0;}#include<sys/types.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>intmain(){pid_tpid;pid=fork(); //創(chuàng)建進(jìn)程if(-1==pid){ //創(chuàng)建進(jìn)程失敗printf("Errortocreatenewprocess!\n");return0;}elseif(pid==0){ //子進(jìn)程printf("Childprocess!\n");}else{ //父進(jìn)程printf("Parentprocess!ChildprocessID:%d\n",pid);}return0;}//process_demo.c#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>intmain(){pid_tpid,pid_wait;intfd[2];charbuff[64],*cmd="exit";if(pipe(fd)){ //創(chuàng)建管道perror("Createpipefail!");return0;}pid=fork();if(-1==pid){perror("Createprocessfail!");return0;}elseif(0==pid){ //子進(jìn)程close(fd[1]); //關(guān)閉寫(xiě)操作printf("waitcommandfromparent!\n");while(1){read(fd[0],buff,64);if(0==strcmp(buff,cmd)){printf("recvcommandok!\n");close(fd[0]);exit(0);}}}else{ //父進(jìn)程printf("Parentprocess!childprocessid:%d\n",pid);close(fd[0]); //關(guān)閉讀操作sleep(2);printf("Sendcommandtochildprocess.\n");write(fd[1],cmd,strlen(cmd)+1); //寫(xiě)入命令close(fd[1]);}return0;}#include<sys/types.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){intfd[2];pid_tpid;charbuf[64]="I'mparentprocess!\n"; //父進(jìn)程要寫(xiě)入管道的信息charline[64];if(0!=pipe(fd)){ //創(chuàng)建管道并檢查結(jié)果fprintf(stderr,"Failtocreatepipe!\n");return0;}pid=fork(); //創(chuàng)建進(jìn)程if(pid<0){fprintf(stderr,"Failtocreateprocess!\n");return0;}elseif(0==pid){ //父進(jìn)程close(fd[0]); //關(guān)閉讀管道,使得父進(jìn)程只能向管道寫(xiě)入數(shù)據(jù)write(fd[1],buf,strlen(buf)); //寫(xiě)數(shù)據(jù)到管道close(fd[1]); //關(guān)閉寫(xiě)管道}else{ //子進(jìn)程close(fd[1]); //關(guān)閉寫(xiě)管道,使得子進(jìn)程只能從管道讀取數(shù)據(jù)read(fd[0],line,64); //從管道讀取數(shù)據(jù)printf("DATAFromParent:%s",line);close(fd[0]); //關(guān)閉讀管道}return0;}#include<stdio.h>#include<stdlib.h>#include<unistd.h>voidbye(void) //退出時(shí)回調(diào)的函數(shù){printf("Thatwasall,folks\n");}voidbye1(void) //退出時(shí)回調(diào)的函數(shù){printf("Thisshouldcalledfirst!\n");}intmain(){longa;inti;i=atexit(bye); //設(shè)置退出回調(diào)函數(shù)并檢查返回結(jié)果if(i!=0){fprintf(stderr,"cannotsetexitfunctionbye\n");returnEXIT_FAILURE;}i=atexit(bye1); //設(shè)置退出回調(diào)函數(shù)并檢查返回結(jié)果if(i!=0){fprintf(stderr,"cannotsetexitfunctionbye1\n");returnEXIT_FAILURE;}returnEXIT_SUCCESS;}#include<sys/types.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>intmain(){pid_tpid,pid_wait;intstatus;pid=fork(); //創(chuàng)建子進(jìn)程if(-1==pid){ //檢查是否創(chuàng)建成功printf("Errortocreatenewprocess!\n");return0;}elseif(pid==0){ //子進(jìn)程printf("Childprocess!\n");}else{ //父進(jìn)程printf("Parentprocess!ChildprocessID:%d\n",pid);pid_wait=waitpid(pid,&status,0); //等待指定進(jìn)程號(hào)的子進(jìn)程printf("Childprocess%dreturned!\n",pid_wait);}return0;}#include<pthread.h>#include<stdio.h>#include<stdlib.h>void*thread_func(void*arg) //線程函數(shù){int*val=arg;printf("Hi,I'mathread!\n");if(NULL!=arg){ //如果參數(shù)不為空,打印參數(shù)內(nèi)容while(1)printf("argumentset:%d\n",*val);}}intmain(){pthread_ttid; //線程IDintt_arg=100; //給線程傳入的參數(shù)值if(pthread_create(&tid,NULL,thread_func,&t_arg)) //創(chuàng)建線程perror("Failtocreatethread");sleep(1); //睡眠1秒,等待線程執(zhí)行printf("Mainthread!\n");pthread_cancel(tid); //取消線程return0;}#include<pthread.h>#include<stdio.h>#include<stdlib.h>void*thread_func(void*arg) //線程函數(shù){int*val=arg;printf("Hi,I'mathread!\n");if(NULL!=arg) //如果參數(shù)不為空,打印參數(shù)內(nèi)容printf("argumentset:%d\n",*val);}intmain(){pthread_ttid; //線程IDintt_arg=100; //給線程傳入的參數(shù)值if(pthread_create(&tid,NULL,thread_func,&t_arg)) //創(chuàng)建線程perror("Failtocreatethread");sleep(1); //睡眠1秒,等待線程執(zhí)行printf("Mainthread!\n");return0;}//pthread_demo.c#include<pthread.h>#include<unistd.h>#include<stdio.h>#include<stdlib.h>void*mid_thread(void*arg); //mid線程聲明void*term_thread(void*arg); //term線程聲明intmain(){pthread_tmid_tid,term_tid; //存放線程idif(pthread_create(&mid_tid,NULL,mid_thread,NULL)){ //創(chuàng)建mid線程perror("Createmidthreaderror!");return0;}if(pthread_create(&term_tid,NULL,term_thread,(void*)&mid_thread)){ //創(chuàng)建term線程perror("Createtermthreadfail!\n");return0;}if(pthread_join(mid_tid,NULL)){ //等待mid線程結(jié)束perror("waitmidthreaderror!");return0;}if(pthread_join(term_tid,NULL)){ //等待term線程結(jié)束perror("waittermthreaderror!");return0;}return0;}void*mid_thread(void*arg) //mid線程定義{inttimes=0;printf("midthreadcreated!\n");while(1){ //不斷打印等待的次數(shù),間隔2秒printf("waittingtermthread%dtimes!\n",times);sleep(2);times++;}}void*term_thread(void*arg) //term線程定義{pthread_t*tid;printf("termthreadcreated!\n");sleep(2);if(NULL!=arg){tid=(pthread_t*)arg;pthread_cancel((*tid)); //如果線程id合法,結(jié)束線程}}//shm_read.c-->gcc-orshm_read.c#include<sys/ipc.h>#include<sys/shm.h>#include<sys/types.h>#include<unistd.h>intmain(){intshmid; //定義共享內(nèi)存idchar*ptr;shmid=shmget(0x90,1024,SHM_W|SHM_R|IPC_EXCL);//根據(jù)key獲得共享內(nèi)存idif(-1==shmid)perror("createsharememory");ptr=shmat(shmid,0,0); //通過(guò)共享內(nèi)存id獲得共享內(nèi)存地址if((void*)-1==ptr)perror("getsharememory");printf("stringinsharememory:%s\n",ptr); //打印共享內(nèi)存中的內(nèi)容shmdt(ptr);return0;}//shm_write.c-->gcc-owshm_write.c#include<sys/ipc.h>#include<sys/shm.h>#include<sys/types.h>#include<unistd.h>intmain(){intshmid; //定義共享內(nèi)存idchar*ptr;char*shm_str="stringinasharememory";shmid=shmget(0x90,1024,SHM_W|SHM_R|IPC_CREAT|IPC_EXCL);//創(chuàng)建共享內(nèi)存if(-1==shmid)perror("createsharememory");ptr=(char*)shmat(shmid,0,0); //通過(guò)共享內(nèi)存id獲得共享內(nèi)存地址if((void*)-1==ptr)perror("getsharememory");strcpy(ptr,shm_str); //把字符串寫(xiě)入共享內(nèi)存shmdt(ptr);return0;}//echo_client–gcc–ocecho_client.c#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<stdio.h>#include<errno.h>#defineEHCO_PORT8080#defineMAX_COMMAND5intmain(){intsock_fd;structsockaddr_inserv_addr;char*buff[MAX_COMMAND]={"abc","def","test","hello","quit"};chartmp_buf[100];socklen_tlen;intn,i;/*創(chuàng)建socket*/sock_fd=socket(AF_INET,SOCK_STREAM,0);if(sock_fd==-1){perror("createsocketerror!");return0;}else{printf("Successtocreatesocket%d\n",sock_fd);}/*設(shè)置server地址結(jié)構(gòu)*/bzero(&serv_addr,sizeof(serv_addr)); //初始化結(jié)構(gòu)占用的內(nèi)存serv_addr.sin_family=AF_INET; //設(shè)置地址傳輸層類(lèi)型serv_addr.sin_port=htons(EHCO_PORT); //設(shè)置監(jiān)聽(tīng)端口serv_addr.sin_addr.s_addr=htons(INADDR_ANY); //設(shè)置服務(wù)器地址bzero(&(serv_addr.sin_zero),8);/*連接到服務(wù)端*/if(-1==connect(sock_fd,(structsockaddr*)&serv_addr,sizeof(serv_addr))){perror("connect()error!\n");close(sock_fd);return0;}printf("Successconnecttoserver!\n");/*發(fā)送并接收緩沖的數(shù)據(jù)*/for(i=0;i<MAX_COMMAND;i++){send(sock_fd,buff[i],100,0); //發(fā)送數(shù)據(jù)給服務(wù)端n=recv(sock_fd,tmp_buf,100,0); //從服務(wù)端接收數(shù)據(jù)tmp_buf[n]='\0';//給字符串添加結(jié)束標(biāo)志printf("datasend:%sreceive:%s\n",buff[i],tmp_buf); //打印字符串if(0==strncmp(tmp_buf,"quit",4)) //判斷是否是退出命令break;}close(sock_fd); //關(guān)閉套接字return0;}//echo_serv.c–gcc–osecho_serv.c#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<stdio.h>#include<errno.h>#defineEHCO_PORT8080#defineMAX_CLIENT_NUM10intmain(){intsock_fd;structsockaddr_inserv_addr;intclientfd;structsockaddr_inclientAdd;charbuff[101];socklen_tlen;intclosing=0;intn;/*創(chuàng)建socket*/sock_fd=socket(AF_INET,SOCK_STREAM,0);if(sock_fd==-1){perror("createsocketerror!");return0;}else{printf("Successtocreatesocket%d\n",sock_fd);}/*設(shè)置server地址結(jié)構(gòu)*/bzero(&serv_addr,sizeof(serv_addr)); //初始化結(jié)構(gòu)占用的內(nèi)存serv_addr.sin_family=AF_INET; //設(shè)置地址傳輸層類(lèi)型serv_addr.sin_port=htons(EHCO_PORT); //設(shè)置監(jiān)聽(tīng)端口serv_addr.sin_addr.s_addr=htons(INADDR_ANY); //設(shè)置服務(wù)器地址bzero(&(serv_addr.sin_zero),8);/*把地址和套接字綁定*/if(bind(sock_fd,(structsockaddr*)&serv_addr,sizeof(serv_addr))!=0){printf("bindaddressfail!%d\n",errno);close(sock_fd);return0;}else{printf("Successtobindaddress!\n");}/*設(shè)置套接字監(jiān)聽(tīng)*/if(listen(sock_fd,MAX_CLIENT_NUM)!=0){perror("listensocketerror!\n");close(sock_fd);return0;}else{printf("Successtolisten\n");}/*創(chuàng)建新連接對(duì)應(yīng)的套接字*/len=sizeof(clientAdd);clientfd=accept(sock_fd,(structsockaddr*)&clientAdd,&len);if(clientfd<=0){perror("accept()error!\n");close(sock_fd);return0;}/*接收用戶(hù)發(fā)來(lái)的數(shù)據(jù)*/while((n=recv(clientfd,buff,100,0))>0){buff[n]='\0';//給字符串加入結(jié)束符printf("numberofreceivebytes=%ddata=%s\n",n,buff); //打印字符串長(zhǎng)度和內(nèi)容fflush(stdout);send(clientfd,buff,n,0); //發(fā)送字符串內(nèi)容給客戶(hù)端if(strncmp(buff,"quit",4)==0) //判斷是否是退出命令break;}close(clientfd); //關(guān)閉新建的連接close(sock_fd); //關(guān)閉服務(wù)端監(jiān)聽(tīng)的socketreturn0;}//time_client.c-gcc-octime_client.c#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<stdio.h>#include<errno.h>#defineTIME_PORT9090#defineDATA_SIZE256intmain(){intsock_fd;structsockaddr_inserv;intservlen,n;charbuff[DATA_SIZE];sock_fd=socket(AF_INET,SOCK_DGRAM,0); //創(chuàng)建套接字if(sock_fd<=0){perror("createsocketerror!");return0;}perror("Createsocket");/*設(shè)置服務(wù)端IP和端口*/serv.sin_family=AF_INET;serv.sin_port=htons(TIME_PORT); //監(jiān)聽(tīng)端口serv.sin_addr.s_addr=INADDR_ANY; //本機(jī)IPservlen=sizeof(serv);/*請(qǐng)求時(shí)間*/strcpy(buff,"time");if(-1==sendto(sock_fd,buff,sizeof(buff),0,(structsockaddr*)&serv,servlen)){ //發(fā)送請(qǐng)求perror("senddata");close(sock_fd);return0;}printf("sendtimerequest\n");n=recvfrom(sock_fd,buff,sizeof(buff),0,(structsockaddr*)&serv,&servlen); //接收返回if(n<=0){perror("recvdata!\n");close(sock_fd);return0;}buff[n]='\0';printf("timefromserver:%s",buff);/*退出連接*/strcpy(buff,"quit");if(-1==sendto(sock_fd,buff,sizeof(buff),0,(structsockaddr*)&serv,servlen)){perror("senddata");close(sock_fd);return0;}printf("sendquitcommand\n");close(sock_fd); //關(guān)閉套接字return0;}//time_serv.c-gcc-ostime_serv.c#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<stdio.h>#include<errno.h>#include<time.h>#defineTIME_PORT9090#defineDATA_SIZE256intmain(){intsock_fd;structsockaddr_inlocal;structsockaddr_infrom;intfromlen,n;charbuff[DATA_SIZE];time_tcur_time;sock_fd=socket(AF_INET,SOCK_DGRAM,0); //建立套接字if(sock_fd<=0){perror("createsocketerror!");return0;}perror("Createsocket");/*設(shè)置要綁定的IP和端口*/local.sin_family=AF_INET;local.sin_port=htons(TIME_PORT);//監(jiān)聽(tīng)端口local.sin_addr.s_addr=INADDR_ANY;//本機(jī)/*綁定本機(jī)到套接字*/if(0!=bind(sock_fd,(structsockaddr*)&local,sizeof(local))){perror("bindsocketerror!");close(sock_fd);return0;}printf("Bindsocket");fromlen=sizeof(from);printf("waitingrequestfromclient...\n");while(1){n=recvfrom(sock_fd,buff,sizeof(buff),0,(structsockaddr*)&from,&fromlen); //接收數(shù)據(jù)if(n<=0){perror("recvdata!\n");close(sock_fd);return0;}buff[n]='\0'; //設(shè)置字符串結(jié)束符printf("clientrequest:%s\n",buff); //打印接收到的字符串if(0==strncmp(buff,"quit",4)) //判斷是否退出break;if(0==strncmp(buff,"time",4)){ //判斷是否請(qǐng)求時(shí)間cur_time=time(NULL);strcpy(buff,asctime(gmtime(&cur_time))); //生成當(dāng)前時(shí)間字符串sendto(sock_fd,buff,sizeof(buff),0,(structsockaddr*)&from,fromlen); //發(fā)送時(shí)間給客戶(hù)端}}close(sock_fd); //關(guān)閉套接字return0;}/*at_test.c-gcc-oat_testat_test.c*/#include<stdio.h> /*標(biāo)準(zhǔn)輸入輸出定義*/#include<stdlib.h> /*標(biāo)準(zhǔn)函數(shù)庫(kù)定義*/#include<unistd.h> /*Unix標(biāo)準(zhǔn)函數(shù)定義*/#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h> /*文件控制定義*/#include<termios.h> /*PPSIX終端控制定義*/#include<errno.h> /*錯(cuò)誤號(hào)定義*/#include<iconv.h>#defineSTTY_DEV"/dev/ttyS0"#defineBUFF_SIZE512intSetOption(intfd){structtermiosopt;/*取得當(dāng)前串口配置*/tcgetattr(fd,&opt);tcflush(fd,TCIOFLUSH);/*設(shè)置波特率-19200bps*/cfsetispeed(&opt,B19200);cfsetospeed(&opt,B19200);/*設(shè)置數(shù)據(jù)位-8位數(shù)據(jù)位*/opt.c_cflag&=~CSIZE;opt.c_cflag|=CS8;/*設(shè)置奇偶位-無(wú)奇偶校驗(yàn)*/opt.c_cflag&=~PARENB;opt.c_iflag&=~INPCK;/*設(shè)置停止位-1位停止位*/opt.c_cflag&=~CSTOPB;/*設(shè)置超時(shí)時(shí)間-15秒*/opt.c_cc[VTIME]=150;opt.c_cc[VMIN]=0;/*設(shè)置寫(xiě)入設(shè)備*/if(0!=tcsetattr(fd,TCSANOW,&opt)){perror("setbaudrate");return-1;}tcflush(fd,TCIOFLUSH);return0;}intmain(){intstty_fd,n;iconv_tcd;charbuffer[BUFF_SIZE];charphone[20]="+8618912345678"; //定義手機(jī)號(hào)碼charsms_number[20]="+8613800200500"; //定義短消息中心號(hào)碼charsms_gb2312[140]="工作愉快!"; //定義短消息內(nèi)容哦charsms_utf8[140];char*sms_in=sms_gb2312;char*sms_out=sms_utf8;intstr_len,i,tmp;intgb2312_len,utf8_len;/*打開(kāi)串口設(shè)備*/stty_fd=open(STTY_DEV,O_RDWR);if(-1==stty_fd){perror("opendevice");return0;}printf("Opendevicesuccess!\n");/*設(shè)置串口參數(shù)*/if(0!=SetOption(stty_fd)){close(stty_fd);return0;}/*轉(zhuǎn)換電話(huà)號(hào)*/if(phone[0]='+'){//去掉號(hào)碼開(kāi)頭的'+'for(i=0;i<strlen(phone)-1;i++)phone[i]=phone[i+1];}phone[i]='\0';str_len=strlen(phone);if((strlen(phone)%2)!=0){ //如果號(hào)碼長(zhǎng)度是奇數(shù),在后面加字符'F'phone[str_len]='F';phone[str_len+1]='\0';}for(i=0;i<strlen(phone);i+=2){ //把號(hào)碼的奇偶位調(diào)換tmp=phone[i];phone[i]=phone[i+1];phone[i+1]=tmp;}/*轉(zhuǎn)換短消息中心號(hào)碼*/if(sms_number[0]='+'){//去掉號(hào)碼開(kāi)頭的'+'for(i=0;i<strlen(sms_number)-1;i++)sms_number[i]=sms_number[i+1];}sms_number[i]='\0';str_len=strlen(sms_number);if((strlen(sms_number)%2)!=0){ //如果號(hào)碼長(zhǎng)度是奇數(shù),在后面加字符'F'sms_number[str_len]='F';sms_number[str_len+1]='\0';}for(i=0;i<strlen(sms_number);i+=2){ //把號(hào)碼的奇偶位調(diào)換tmp=sms_number[i];sms_number[i]=sms_number[i+1];sms_number[i+1]=tmp;}str_len=strlen(sms_number);for(i=strlen(sms_number)+2;i!=0;i--) //所有的字符向后移動(dòng)兩個(gè)字節(jié)sms_number[i]=sms_number[i-2];sms_number[str_len+3]='\0';strncpy(sms_number,"91",2); //開(kāi)頭寫(xiě)入字符串"91"tmp=strlen(sms_number)/2; //計(jì)算字符串長(zhǎng)度str_len=strlen(sms_number);for(i=strlen(sms_number)+2;i!=0;i--) //所有的字符向后移動(dòng)兩個(gè)字節(jié)sms_number[i]=sms_number[i-2];sms_number[str_len+3]='\0';sms_number[0]=(char)(tmp/10)+0x30; //字符串長(zhǎng)度轉(zhuǎn)換為字符類(lèi)型寫(xiě)入開(kāi)頭sms_number[1]=(char)(tmp%10)+0x30;/*轉(zhuǎn)換短消息內(nèi)容*/cd=iconv_open("utf-8","gb2312"); //設(shè)置轉(zhuǎn)換類(lèi)型"gb2312"==>"utf-8"if(0==cd){perror("createiconvhandle!");close(stty_fd);return0;}gb2312_len=strlen(sms_gb2312); //輸入字符串的長(zhǎng)度utf8_len=140;if(-1==iconv(cd,&sms_in,&gb2312_len,&sms_out,&utf8_len)){ //轉(zhuǎn)換字符為Unicode編碼perror("convertcode");close(stty_fd);return0;}iconv_close(cd);/*設(shè)置使用PDU模式*/strcpy(buffer,"AT+CMGF=0\n");write(stty_fd,buffer,strlen(buffer)); //寫(xiě)入配置命令n=read(stty_fd,buffer,BUFF_SIZE);if(n<=0){perror("setpdumode");close(stty_fd);return0;}if(0!=strncmp(buffer,"OK",2)){ //判斷命令是否執(zhí)行成功perror("setpdumode");close(stty_fd);return0;}/*發(fā)送消息*/sprintf(buffer,"AT+CMGS=%d\n",utf8_len); //寫(xiě)入發(fā)送消息命令write(stty_fd,buffer,strlen(buffer));write(stty_fd,sms_utf8,utf8_len); //寫(xiě)入消息內(nèi)容printf("SendmessageOK!\n");close(stty_fd);}#include<stdio.h> /*標(biāo)準(zhǔn)輸入輸出定義*/#include<stdlib.h> /*標(biāo)準(zhǔn)函數(shù)庫(kù)定義*/#include<unistd.h> /*Unix標(biāo)準(zhǔn)函數(shù)定義*/#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h> /*文件控制定義*/#include<termios.h> /*PPSIX終端控制定義*/#include<errno.h> /*錯(cuò)誤號(hào)定義*/intmain(){ intfd; fd=open("/dev/ttyS0",O_RDWR); //使用讀寫(xiě)方式打開(kāi)串口 if(-1==fd){ perror("openttyS0"); return0; } printf("OpenttyS0OK!\n"); close(fd); //關(guān)閉串口 return0;}/*stty_echo.c-gcc-ostty_echostty_echo.c*/#include<stdio.h> /*標(biāo)準(zhǔn)輸入輸出定義*/#include<stdlib.h> /*標(biāo)準(zhǔn)函數(shù)庫(kù)定義*/#include<unistd.h> /*Unix標(biāo)準(zhǔn)函數(shù)定義*/#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h> /*文件控制定義*/#include<termios.h> /*PPSIX終端控制定義*/#include<errno.h> /*錯(cuò)誤號(hào)定義*/#defineSTTY_DEV"/dev/ttyS0"#defineBUFF_SIZE512intmain(){intstty_fd,n;charbuffer[BUFF_SIZE];structtermiosopt;/*打開(kāi)串口設(shè)備*/stty_fd=open(STTY_DEV,O_RDWR);if(-1==stty_fd){perror("opendevice");return0;}printf("Opendevicesuccess,waitinguserinput...\n");/*取得當(dāng)前串口配置*/tcgetattr(stty_fd,&opt);tcflush(stty_fd,TCIOFLUSH);/*設(shè)置波特率-19200bps*/cfsetispeed(&opt,B19200);cfsetospeed(&opt,B19200);/*設(shè)置數(shù)據(jù)位-8位數(shù)據(jù)位*/opt.c_cflag&=~CSIZE;opt.c_cflag|=CS8;/*設(shè)置奇偶位-無(wú)奇偶校驗(yàn)*/opt.c_cflag&=~PARENB;opt.c_iflag&=~INPCK;/*設(shè)置停止位-1位停止位*/opt.c_cflag&=~CSTOPB;/*設(shè)置超時(shí)時(shí)間-15秒*/opt.c_cc[VTIME]=150;opt.c_cc[VMIN]=0;/*設(shè)置寫(xiě)入設(shè)備*/if(0!=tcsetattr(stty_fd,TCSANOW,&opt)){perror("setbaudrate");return0;}tcflush(stty_fd,TCIOFLUSH);/*讀取數(shù)據(jù),直到接收到'quit'字符串退出*/while(1){n=read(stty_fd,buffer,BUFF_SIZE);if(n<=0){perror("readdata");break;}buffer[n]='\0';printf("%s",buffer);if(0==strncmp(buffer,"quit",4)){printf("usersendquit!\n");break;}}printf("Programwillexit!\n");close(stty_fd);}//FindDialog.cpp#include<qcheckbox.h>#include<qlabel.h>#include<qlayout.h>#include<qlineedit.h>#include<qpushbutton.h>#include"FindDialog.h"FindDialog::FindDialog(QWidget*parent):QDialog(parent) //構(gòu)造函數(shù){Label=newQLabel(tr("Find&String:"),this); //創(chuàng)建文本標(biāo)簽控件LineEdit=newQLineEdit(this); //創(chuàng)建文本框控件Label->setBuddy(LineEdit); //綁定文本框控件和標(biāo)簽控件CaseCB=newQCheckBox(tr("Match&Case"),this); //創(chuàng)建大小寫(xiě)CheckBoxBackwardCB=newQCheckBox(tr("Search&backward"),this); //創(chuàng)建搜索方向CheckBoxFindBtn=newQPushButton(t
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年軟件著作權(quán)許可使用合同
- 家長(zhǎng)參與學(xué)?;顒?dòng)的設(shè)計(jì)與實(shí)踐
- 2024版事業(yè)單位標(biāo)準(zhǔn)化粉刷作業(yè)承包協(xié)議版B版
- 二零二五年度建筑工程土方供應(yīng)與運(yùn)輸服務(wù)合同2篇
- 賓館服務(wù)質(zhì)量評(píng)價(jià)體系構(gòu)建
- 二零二五年度國(guó)有企業(yè)股權(quán)轉(zhuǎn)讓與合同續(xù)簽及調(diào)整協(xié)議3篇
- 共育模式下的家庭教育質(zhì)量評(píng)估與提升策略研究
- 商業(yè)視角下的學(xué)生創(chuàng)新創(chuàng)業(yè)能力培養(yǎng)模式
- 算法課程設(shè)計(jì)導(dǎo)游系統(tǒng)
- 《基于深度學(xué)習(xí)理論的高中語(yǔ)文閱讀教學(xué)研究》
- 2024年鋁錠購(gòu)銷(xiāo)的合同雙方信息登記表
- 貴州省遵義市播州區(qū)2023-2024學(xué)年二年級(jí)上學(xué)期數(shù)學(xué)期末質(zhì)量監(jiān)測(cè)試卷
- 2024版智能硬件產(chǎn)品研發(fā)合作協(xié)議3篇
- 甘肅省蘭州市第一中學(xué)2023-2024學(xué)年高一上學(xué)期期末考試 物理 含解析
- 草地調(diào)查規(guī)劃學(xué)知到智慧樹(shù)章節(jié)測(cè)試課后答案2024年秋東北農(nóng)業(yè)大學(xué)
- 2024年礦產(chǎn)資源開(kāi)發(fā)咨詢(xún)服務(wù)合同
- 上海市2024-2025學(xué)年高一語(yǔ)文下學(xué)期期末試題含解析
- 國(guó)家電網(wǎng)招聘之財(cái)務(wù)會(huì)計(jì)類(lèi)題庫(kù)含完整答案(必刷)
- 建筑物拆除的拆除工廠考核試卷
- 廣東省深圳市2023-2024學(xué)年高二上學(xué)期期末測(cè)試英語(yǔ)試卷(含答案)
- 2024年手術(shù)室?guī)Ы坦ぷ饔?jì)劃樣本(5篇)
評(píng)論
0/150
提交評(píng)論