朋友你好: 我是一名大四的学生,现在做毕业设计,我的 mini2440开发板在调试串口(uart1),每次总是在接收 { 使用函数:write(fd,buff,512) ;} 数据后又发送所接收到的数据,而且还在前后都加上了\r\n.
例如:接收数据:\r\dOK\r\d。接着就发送数据:\r\d\r\dOK\r\d\r\d。
读取数据时{使用函数:read(fd,buff,512);}接着使用printf(“%s”,buff,)打印,如果没有收到数据,打印函数不执行。如果给串口发送一个带回车换行的数据,就能显示,并且ARM回向串口发送数据就是上面提到的情况。
我认为是缓存没有清空,但是在应用程序中我又不知道怎么去清除。希望大家能帮忙看看怎么设置,谢谢啦,我QQ:402828584
我的应用函数如下:
#include <stdio.h> /*标准输入输出定义*/#include <stdlib.h> /*标准函数库定义*/#include <unistd.h> /*Unix标准函数定义*/#include <sys/types.h> /**/#include <sys/stat.h> /**/#include <fcntl.h> /*文件控制定义*/#include <termios.h> /*PPSIX终端控制定义*/#include <errno.h> /*错误号定义*/
#include <string.h>
#define FALSE -1#define TRUE 0/*#define UTRSTAT1 0x50004010#define UERSTAT1 0x50004014
#define TXD0READY (1<<2)#define RXD0READY (1)*///**全局变量出初始化**//char buff[512];char storbuff[512];
**************//////////////
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) { tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arr); cfsetospeed(&Opt, speed_arr); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) perror("tcsetattr fd1"); return; } tcflush(fd,TCIOFLUSH); }}
设置串口数据位,停止位和效验位 fd 类型 int 打开的串口文件句柄* databits 类型 int 数据位 取值 为 7 或者8* stopbits 类型 int 停止位 取值为 1 或者2* 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); }
****打开串**********//////////////
int OpenDev(char *Dev){int fd = open(Dev,O_RDWR); //O_RDWR | O_NOCTTY | O_NDELAY if (-1 == fd) { /*设置数据位数*/ perror("Can't Open Serial Port"); return -1; } else return fd;
}
//******函数*******////**调用函数声名//int JUDGE_OK(char date[]);int TXDATE_TO_BUFF(char date[]);int TBUFF_TO_STORBUFF(char date[]);
////**应用程序主函数实现功能******////////**应用程序主函数实现功能******////int main(int argc, char **argv){ int fd; int nread; char *dev ="/dev/s3c2410_serial1"; fd = OpenDev(dev); if (fd>0) set_speed(fd,19200); else { printf("Can't Open Serial Port!\n"); exit(0); } if (set_Parity(fd,8,1,'N')== FALSE) { printf("Set Parity Error\n"); exit(1); }
//**使用-标志位-初始化**// int read_flag=0;// char stor_buff[512];//**使用-发送数组-初始化//** char AT[]="AT\n"; char *ATD="ATD15958492073;\n"; char ATE0[]="ATE0\n";
memset(buff,0,sizeof(buff)); write(fd,"AT\r\n",4); //write(fd,buff,(TXDATE_TO_BUFF(ATD))); memset(buff,0,sizeof(buff)); while(1) { if((nread = read(fd,buff,512))>0) { buff[nread+1]='\0'; printf("Get date is :%s",buff); if(TBUFF_TO_STORBUFF(buff)==1) { memset(buff,0,sizeof(buff)); read_flag=1; } } if(read_flag==1) { if(1==JUDGE_OK(storbuff)) printf("This is OK\n"); memset(buff,0,sizeof(storbuff)); printf("claean storbuff ok !\n"); read_flag=0; } } close(fd); exit(0);}
////****收到数据判断函数定义******/////int JUDGE_OK(char date[]) { int i,flag=0; if((strlen(date))<5) { for(i=0;i<2;) switch(date[i++]) { case 'O' :flag=1; break; case 'K' :if(flag==1)return 1;else {printf("judge ok is erro\n");return 0;} } } }///****发送组合数组********///int TXDATE_TO_BUFF(char date[]) { int datelen=0; int count=0; datelen=strlen(date); for(count=0;count<datelen;) { buff[count]=date[count]; count++; } return datelen; }///****接受到数组保留********///int TBUFF_TO_STORBUFF(char date[]) { int datelen=0; int count=0; datelen=strlen(date); for(count=0;count<datelen;) { storbuff[count]=date[count]; count++; } return datelen; }