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

📄 receive.c

📁 串口初始化及 串口的接收和发送函数
💻 C
字号:
#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"math.h"#define  max_buffer_size	100int fd,s;int open_serial(int k){  if(k==0)  {   fd=open("/dev/ttys0",O_RDWR|O_NOCTTY);   perror("open /dev/ttys0");  }  else  {   fd=open("/dev/ttys1",O_RDWR|O_NOCTTY);   perror("open /dev/ttys1");  }  if(fd==-1)   return -1;   else    return 0;}/*****************************************************************************/int main(){  char hd[max_buffer_size],*rbuf;  int flag_close,retv,i,ncount=0;  struct termios opt;  int realdata=0;  open_serial(0);  /****************************************************************************/  tcgetattr(fd,&opt);  cfmakeraw(&opt);  /****************************************************************************/  cfsetispeed(&opt,B9600);  cfsetospeed(&opt,B9600);  /****************************************************************************/  tcsetattr(fd,TCSANOW,&opt);  rbuf=hd;  printf("ready for receiving data.....\n");  retv=read(fd,rbuf,1);  if(retv==-1)  {   perror("read");  } /****************************************************************************/ while(*rbuf!='\n')  {    ncount+=1;    rbuf++;    retv=read(fd,rbuf,1);    if(retv==-1)    {      perror("read");    }  } /*******************************************************************************/  printf("the data received is :\n");  for(i=0;i<ncount;i++)  {     printf("%d",hd[i]);  }  printf("\n");  flag_close=close(fd);  if(flag_close==-1)   printf("close the device failur! \n");   return 0;}

⌨️ 快捷键说明

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