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

📄 recieve.c

📁 实现串口通讯文件收发的功能,在宿主机和目标机中用串口来来传送文件数据.
💻 C
字号:
#include "unistd.h"
#include "fcntl.h"
#include "stdio.h"
#include "sys/types.h"
#include "errno.h"
#include "string.h"
#include     <sys/stat.h>   /**/
#include     <termios.h>    /*PPSIX终端控制定义*/


#define BUFFER_SIZE 1024
#define FALSE 0
#define TRUE 1

/***@brief  设置串口通信速率
*@param  fd     类型 int  打开串口的文件句柄
*@param  speed  类型 int  串口速度
*@return  void*/

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
	    B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,
	    38400,  19200,  9600, 4800, 2400, 1200,  300, };
void set_speed(int fd, int speed)
{
  int   i;
  int   status;
  struct termios   Opt;
  tcgetattr(fd, &Opt);
  for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
   {
   	if  (speed == name_arr[i])
   	{
   	    tcflush(fd, TCIOFLUSH);
    	cfsetispeed(&Opt, speed_arr[i]);
    	cfsetospeed(&Opt, speed_arr[i]);
    	status = tcsetattr(fd, TCSANOW, &Opt);
    	if  (status != 0)
            perror("tcsetattr fd1");
     	return;
     	}
   tcflush(fd,TCIOFLUSH);
   }
}
/**
*@brief   设置串口数据位,停止位和效验位
*@param  fd     类型  int  打开的串口文件句柄*
*@param  databits 类型  int 数据位   取值 为 7 或者8*
*@param  stopbits 类型  int 停止位   取值为 1 或者2*
*@param  parity  类型  int  效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
	struct termios options;
 if  ( tcgetattr( fd,&options)  !=  0)
  {
  	perror("SetupSerial 1");
  	return(FALSE);
  }
  options.c_cflag &= ~CSIZE;
  switch (databits) /*设置数据位数*/
  {
  	case 7:
  		options.c_cflag |= CS7;
  		break;
  	case 8:
		options.c_cflag |= CS8;
		break;
	default:
		fprintf(stderr,"Unsupported data size\n");
		return (FALSE);
	}
  switch (parity)
  	{
  	case 'n':
	case 'N':
		options.c_cflag &= ~PARENB;   /* Clear parity enable */
		options.c_iflag &= ~INPCK;     /* Enable parity checking */
		break;
	case 'o':
	case 'O':
		options.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/ 
		options.c_iflag |= INPCK;             /* Disnable parity checking */
		break;
	case 'e':
	case 'E':
		options.c_cflag |= PARENB;     /* Enable parity */
		options.c_cflag &= ~PARODD;   /* 转换为偶效验*/  
		options.c_iflag |= INPCK;       /* Disnable parity checking */
		break;
	case 'S':
	case 's':  /*as no parity*/
		options.c_cflag &= ~PARENB;
		options.c_cflag &= ~CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported parity\n");
		return (FALSE);
		}
  /* 设置停止位*/   
  switch (stopbits)
  	{
  	case 1:
  		options.c_cflag &= ~CSTOPB;
		break;
	case 2:
		options.c_cflag |= CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported stop bits\n");
		return (FALSE);
	}
  /* Set input parity option */
  if (parity != 'n')
  		options.c_iflag |= INPCK;
    options.c_cc[VTIME] = 150; // 15 seconds
    options.c_cc[VMIN] = 0;

  tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  if (tcsetattr(fd,TCSANOW,&options) != 0)
  	{
  		perror("SetupSerial 3");
		return (FALSE);
	}
  return (TRUE);
 }
/**
*@breif 打开串口
*/
int OpenDev(char *Dev)
{
int	fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
	if (-1 == fd)
		{ /*设置数据位数*/
			perror("Can't Open Serial Port");
			return -1;
		}
	else
	return fd;

}


int main(int argc,char **argv)
{
  int from_fd,to_fd,i,j,total_bytes;
  int bytes_read,bytes_write;
  char buffer[BUFFER_SIZE];
  char *ptr;
  char *dev ="/dev/ttyS0";  //linux下的端口就是通过打开设备文件操作的
  from_fd = OpenDev(dev);   //打开
  if (from_fd>0)
  {
    printf("form_fd open succeed\n");
    set_speed(from_fd,19200); //打开后设置波特率19200
    printf("speed is set\n");
  }
  else
  {
    printf("Can't Open Serial Port!\n");
    exit(0);
  }
  if (set_Parity(from_fd,8,1,'N')== FALSE) //设置8,1,n 注意,这里和上面要和下位机相符才可能通信
  {
    printf("Parity is set\n");
    printf("Set Parity Error\n");
    exit(1);
  }
  
  if(argc!=2)
  {
    fprintf(stderr,"Usage: %s from file /dev/ttyS0,and create file \n\a",argv[0],argv[1]);
    exit(1);
  }
  
  i =0;
  while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
  {
    total_bytes += bytes_read;
    if((bytes_read==-1)&&(errno!=EINTR))
      break;
    else if(bytes_read>0)
         {
           printf("in %d read, we get %d bytes form /dev/ttyS0\n",++i,bytes_read);
           
           /*create the wanted file*/
           if(1==i)
           {
               if((to_fd=open(argv[1],O_WRONLY|O_CREAT,10600))==-1)
               {
                fprintf(stderr,"Open %s Error: %s\n",argv[1],strerror(errno));
                exit(1);
               }  
               printf("the wanted file %s created in the current directory!\n",argv[1]);
           }
           
           ptr=buffer;
           j=0;
        //   printf("now we write the to file.\n\n");
           while(bytes_write=write(to_fd,ptr,bytes_read))
           {
             printf("in this read cycle,we write %d bytes in the %dst write\n",bytes_write,++j);
             if((bytes_write==-1)&&(errno!=EINTR))
                break;
             else if(bytes_write==bytes_read)
                    break;
             else if(bytes_write>0)
                  {
                     ptr+=bytes_write;
                     bytes_read-=bytes_write;
                  }
            
           }
           if(bytes_write==-1)
              break;
         }  
  }
  printf("recieve over! we recieved %d byts total\n",total_bytes);
  close(from_fd);
  close(to_fd);
  exit(0);
}

⌨️ 快捷键说明

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