日期:2014-05-16  浏览次数:20674 次

mini2440 串口读写
以下串口能正确写,但是不能读,请高人指教一下,谢谢
int main()
{
int fd;
char *buf_r;
char buf_w[100];
struct termios opt;
char af = 0;
FILE *f;

fd = open("/dev/ttySAC1", O_RDWR);

if(fd < 0) printf("ttySAC1 open err");

tcgetattr(fd,&opt);
tcflush(fd, TCIFLUSH);
cfsetispeed(&opt,B38400);
cfsetospeed(&opt,B38400);

tcsetattr(fd,TCSANOW,&opt);
tcflush(fd,TCIOFLUSH);

while(1)
{
sprintf(buf_w,"%c%c%c%c%c%c%c",0x68,0x68,0x68,0x68,0x68,0x68,50);
printf("write %s\n",buf_w);

write(fd, buf_w, strlen(buf_w));

read(fd,buf_r, 5);
printf("read : %s\n",buf_r);

sleep(2);

}

}



------解决方案--------------------
在中间给它,延时一下,还有就是你串口配置在好好看看吧

------解决方案--------------------
恩。

write 和 read 之间sleep(2);
------解决方案--------------------
配置一下数据位等信息试试

C/C++ code

/**
*@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(-1);  
    }
    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 (-1);  
    }
    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 (-1);  
    }  
    /* 设置停止位*/  
    switch (stopbits)
    {   
        case 1:    
            options.c_cflag &= ~CSTOPB;  
            break;  
        case 2:    
            options.c_cflag |= CSTOPB;  
           break;
        default:    
             fprintf(stderr,"Unsupported stop bits\n");  
             return (-1); 
    } 
    /* Set input parity option */ 
    if (parity != 'n')   
        options.c_iflag |= INPCK; 
    tcflush(fd,TCIFLUSH);
    //设置最少字符和等待时间,对于接收字符和等待时间没有特别要求时,可设为0
//    options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/   
    options.c_cc[VTIME] = 0; /* 设置超时0 seconds*/   
    options.c_cc[VMIN] = 0; /* Update the options and do it NOW */

    //如果不是开发终端之类的,只是串口传输数据,而不需要串口来处理,那么使用原始模式(Raw Mode)方式来通讯
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);//Input 
    options.c_oflag &= ~OPOST; //Output 

    if (tcsetattr(fd,TCSANOW,&options) != 0)   
    { 
        perror("SetupSerial 3");   
        return (-1);  
    } 
    return (0);  
}