免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1155 | 回复: 0
打印 上一主题 下一主题

串口读写的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-17 12:09 |只看该作者 |倒序浏览
我写了个串口读写程序,来对串口下命令并读取响应~  但是在将命令写入串口之后,select立刻返回了,而且返回的就是我刚才写进去的命令而不是这个命令的返回结果   请各位大侠帮我看看到底是怎么回事。
代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <time.h>
#include <string.h>
static const char *TTYFILE = "/dev/ttyUSB0";
int main(int argc, char **argv)
{
    int fd;
    int status = 0;
    int str_len = 0;
    char *tty_file = NULL;
    char at_cmd[128];
    char read_buf[1024];
    struct termios Opt;
    struct timeval timeout;
    fd_set fd_read;
    timeout.tv_sec = 2;
    timeout.tv_usec = 0;
    memset(at_cmd, 0x00, sizeof(at_cmd));
    memset(&Opt, 0x00, sizeof(struct termios));
    memset(read_buf, 'B', sizeof(read_buf));
    if( 2 == argc )
    {
        strcpy(at_cmd, argv[1]);
        strcat(at_cmd, "\r\n");
        tty_file = TTYFILE;
    }
    else if( 4 == argc )
    {
        if( strcmp(argv[1], "-f") == 0 )
        {
            strcpy(at_cmd, argv[3]);
            strcat(at_cmd, "\r\n");
            tty_file = argv[2];
        }
        else if( strcmp(argv[2], "-f") == 0 )
        {
            strcpy(at_cmd, argv[1]);
            strcat(at_cmd, "\r\n");
            tty_file = argv[3];
        }
        else
        {
            printf("ttycrl error:wrong argument\n");
            return -1;
        }
    }
    else
    {
        printf("ttycrl error:wrong argument\n");
        return -1;
    }
    //printf("tty file: %s\nat cmd: %s", tty_file, at_cmd);
   
    if( (fd=open(tty_file, O_RDWR)) == -1 )
    {
        perror("ttycrl error");
        return -1;
    }
    tcgetattr(fd, &Opt);
    tcflush(fd,TCIFLUSH);
   
    cfsetispeed(&Opt, B115200);
    cfsetospeed(&Opt, B115200);
    Opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    Opt.c_oflag &= ~OPOST;   
    if((status = tcsetattr(fd, TCSANOW, &Opt)))
    {
        perror("ttycrl error");
        close(fd);
        return status;
    }
    //printf("write to tty cmd %d\n", strlen(at_cmd));
    str_len = strlen(at_cmd);
    if( write(fd, at_cmd, str_len) != str_len)
    {
        perror("ttycrl error");
        close(fd);
        return -1;
    }
    //while(1){
    FD_ZERO(&fd_read);
    FD_SET(fd, &fd_read);
    //printf("select \n");
   
    //status = select(fd + 1, &fd_read, NULL, NULL, &timeout);
    status = select(fd + 1, &fd_read, NULL, NULL, NULL);
    printf("something to read");
    if(status <= 0)
    {
        printf("ttycrl error:read error\n");
        close(fd);
        return -1;
    }
    //printf("read\n");
    usleep(200);
   
    str_len = read(fd, read_buf, 1024);
    read_buf[str_len] = '\0';
   
    //printf("result: \n");
    printf("%s\n", read_buf);
    //}
    close(fd);
    return 0;
   
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP