📄 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 + -