为每个read()设置VMIN和VTIME

时间:2015-10-16 05:35:22

标签: c linux unix serial-port termios

对不起,如果这是一个愚蠢的问题,但我真的不知道答案。
我有两个功能可以命令我的设备。

第一个功能在VTIME = 5VMIN = 7时正确生成输出,但我的第二个功能仅在VTIME = 0VMIN = 0时生效 我的问题是,有没有办法为每个read()设置VMIN和VTIME? 如果没有,为每次读取设置超时的可能功能是什么?

更新

的main.c

char *portname = "/dev/ttyUSB4";
char readeraddr = 0xFF;

printf("Initializing Connection... ");
int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
        printf("error %d opening %s: %s", errno, portname, strerror (errno));
        return;
}
printf("Connected\n");

printf("Setting up baud rate... ");
set_interface_attribs (fd, B9600, 0);

printf("Checking firmware... ");
get_firmware_version(fd, readeraddr);

....

printf("clear scan... ");
clear_tag_buffer(fd, readeraddr);

set_interface_attribs函数

int
set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                printf("error %d from tcgetattr", errno);
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars

        tty.c_iflag &= ~IGNBRK;         // disable break processing
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays

        tty.c_cc[VMIN]  = 100;          // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                printf("error %d from tcsetattr", errno);
                return -1;
        }
        return 0;
}

当VTIME = 5且VMIN = 7

时,get_firmware_version函数返回正确
void
get_firmware_version(int fd, char readeraddr)
{

unsigned char WBuf[5] = {0x0A,readeraddr,0x02,0x25};
unsigned char RBuf[1024];

check_sum( WBuf, sizeof WBuf -1);
write_and_send(fd, WBuf, RBuf);

unsigned char Major = RBuf[4];
unsigned char Minor = RBuf[5];

printf("The version is %02d.%02d \n", Major, Minor);
}

clear_tag_buffer函数仅在VTIME = 0且VMIN = 0

时有效
clear_tag_buffer(int fd, char readeraddr)
{
    unsigned char WBuf[5] = {0x0A,readeraddr,0x03,0x45};
    unsigned char RBuf[1024];

    check_sum( WBuf, sizeof WBuf -1);
    int j;
    for(j = 0;j < sizeof WBuf; j++) {
        printf("0x%02X \n", WBuf[j]);
    }
    memset(RBuf,0,1024);
    write_and_send(fd, WBuf, RBuf);
}

0 个答案:

没有答案