⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialclient.c

📁 Linux串口通信客户端和服务器程序
💻 C
字号:
/*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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -