?? serialserver.c
字號:
/*serial server on linux pc */#include <stdio.h>#include <string.h>#include <malloc.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <termios.h>#include <errno.h>#include <math.h>int spfd;int main(){ char hd[16],*rbuf; int retv,i,ncount=0; struct termios oldtio; int realdata = 0; spfd = open("/dev/ttyS1",O_RDWR|O_NOCTTY); if(spfd<0) { perror("open/dev/ttyS1"); return -1; } tcgetattr(spfd,&oldtio); //保存串口的當前設置 cfmakeraw(&oldtio); cfsetispeed(&oldtio,B115200); cfsetospeed(&oldtio,B115200); tcsetattr(spfd,TCSANOW,&oldtio); //選擇新的設置,TCSANOW表示設置立即生效 rbuf = hd; printf("ready for receiving data ....\n"); retv= read(spfd,rbuf,1); if(retv==-1) { perror("read"); return -1; } while( *rbuf!='\0') { ncount +=1; rbuf++; retv = read(spfd,rbuf,1); printf("the number received is %d\n",retv); if (retv== -1) perror("read"); } for(i=0;i<ncount;i++) { realdata += (hd[i]-48)*pow(10,ncount-i-1); } printf("complete receiving the data %d\n",realdata); close(spfd); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -