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

📄 uart1.h

📁 arm控制gprs模块
💻 H
字号:
#include     <stdio.h>      /* stand input and output... */#include     "fcntl.h"      /* file controle */#include     "termios.h"    /*PPSIX terminal controle */#include     "errno.h"     /* error */void set_speed(int fd, int speed);int set_data_format(int fd,int databits,int stopbits,int parity);void Uart_Init(int fd);void Uart_Send(int fd, char *data,int n);void Uart_Getch(int fd, char *recv);/************ 设置波特率 ********************/void set_speed(int fd, int speed){    int   i;    int   status;    struct termios Opt;    tcgetattr(fd, &Opt);  // get attribute of serial port    tcflush(fd, TCIOFLUSH);    cfsetispeed(&Opt, speed);    cfsetospeed(&Opt, speed);    status = tcsetattr(fd, TCSANOW, &Opt);  // set attribute    if  (status != 0)    {        perror("tcsetattr fd1");        return;    }    tcflush(fd,TCIOFLUSH);}/***********设置串口模式**********/int set_data_format(int fd,int databits,int stopbits,int parity){    struct termios opt;    if( tcgetattr(fd, &opt)  !=  0)    {  	perror("SetupSerial 1");  	return(FALSE);    }    opt.c_cflag &= ~CSIZE;    switch (databits)    { 	        case 5:  		opt.c_cflag |= CS5;  		break;        case 6:  		opt.c_cflag |= CS6;  		break;        case 7:  		opt.c_cflag |= CS7;  		break;  	case 8:		opt.c_cflag |= CS8;		break;	default:		fprintf(stderr,"Unsupported data size\n");		return (FALSE);    }    switch (parity)    {        case 'n':        case 'N':		opt.c_cflag &= ~PARENB;   /* Clear parity enable */		opt.c_iflag &= ~INPCK;     /* Enable parity checking */		break;	case 'o':	case 'O':		opt.c_cflag |= (PARODD | PARENB);  /* parity checking */ 		opt.c_iflag |= INPCK;      /* Disnable parity checking */		break;	case 'e':	case 'E':		opt.c_cflag |= PARENB;     /* Enable parity */		opt.c_cflag &= ~PARODD;   /*  */  		opt.c_iflag |= INPCK;       /* Disnable parity checking */		break;//	case 'S'://	case 's':  /*as no parity*///		opt.c_cflag &= ~PARENB;//		opt.c_cflag &= ~CSTOPB;//		break;	default:		fprintf(stderr,"Unsupported parity\n");		return (FALSE);    }           switch (stopbits)    {        case 1:  		opt.c_cflag &= ~CSTOPB;		break;	case 2:		opt.c_cflag |= CSTOPB;		break;	default:		fprintf(stderr,"Unsupported stop bits\n");		return (FALSE);    }    /* Set input parity option */    if (parity != 'n')     	opt.c_iflag |= INPCK;    opt.c_cc[VTIME] = 100; // 10 seconds    opt.c_cc[VMIN] = 0;    tcflush(fd, TCIFLUSH); /* Update the options and do it NOW */    if (tcsetattr(fd, TCSANOW, &opt) != 0)    {  	perror("SetupSerial 3");	return (FALSE);    }     return (TRUE); }/********************/void Uart_Init(int fd){  set_speed(fd, B115200);  if (set_data_format(fd,8,1,'N')==FALSE)  {    printf("set parity error! \n");    exit(1);  }}void Uart_Send(int fd, char *data,int n){  int send_len;  send_len=write(fd,data,n);  while(send_len!=n)  {    printf("send data erro! send again!\n");    send_len=write(fd,data,n);  }  printf("send data succesful!\n");}void Uart_Getch(int fd, char *recv){  read(fd, recv, 32);  printf("Received data: %s", recv);}           

⌨️ 快捷键说明

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