send.c

来自「串口初始化及 串口的接收和发送函数」· C语言 代码 · 共 84 行

C
84
字号
#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 flag_close;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(int argc ,char *argv[]){  char sbuf[]={"hello,this is a serial_port test !\n"};  int sfd,retv,i;  struct termios option;  int length=sizeof(sbuf);/*******************************************************************************/  open_serial(0);  /****************************************************************************/  printf("ready for sending data.....\n");  tcgetattr(fd,&option);  cfmakeraw(&option);  /****************************************************************************/  cfsetispeed(&option,B9600);  cfsetospeed(&option,B9600);  /****************************************************************************/  tcsetattr(fd,TCSANOW,&option);//  rbuf=hd; // printf("ready for receiving data.....\n");  retv=write(fd,sbuf,length);  if(retv==-1)  {   perror("write");  }  printf("the number of char sent is %d\n",retv); /****************************************************************************//* 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 + =
减小字号Ctrl + -
显示快捷键?