serialclient.c

来自「Linux串口通信客户端和服务器程序」· C语言 代码 · 共 49 行

C
49
字号
/*serial client on uclinux terminal */#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 <stdlib.h>#include <unistd.h>int spfd;int main(){ char fname[16],*sbuf; int retv; struct termios oldtio; spfd = open("/dev/ttyS0",O_RDWR|O_NOCTTY); if(spfd<0)	{	 perror("open/dev/ttyS0");	 return -1;	} printf("\n ready for sending data....\n"); tcgetattr(spfd,&oldtio); //保存串口的当前设置 cfmakeraw(&oldtio); cfsetispeed(&oldtio,B115200); cfsetospeed(&oldtio,B115200); tcsetattr(spfd,TCSANOW,&oldtio); //选择新的设置,TCSANOW表示设置立即生效 fname[0] = '1'; fname[1] = '2'; fname[2] = '3'; fname[3] = '\0';  sbuf = (char *)malloc(4); strncpy(sbuf,fname,4); retv= write(spfd,sbuf,4);  if(retv==-1)   perror("read");  printf("the number of char sent is %d\n",retv);  return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?