0

Sorry if it's a dumb question but I really don't know the answer.
I have two function that gives command to my device.

The first function produce output correctly when VTIME = 5 and VMIN = 7
but my second function only works when VTIME = 0 AND VMIN = 0

My question is, is there a way to set VMIN and VTIME for each read()?
If not, what are the possible function to set timeout for each read?

UPDATE

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 function

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;
}

get_firmware_version function returns correct when VTIME = 5 and VMIN = 7

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 function works only when VTIME = 0 AND 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);
}
sdfsfssfsdf
  • 38
  • 12
  • 1
    See: http://unixwiz.net/techtips/termios-vmin-vtime.html – Paul R Oct 16 '15 at 06:15
  • @PaulR sir what if the first function returns data while the second function is not? what configuration should I give to VTIME and VMIN? – sdfsfssfsdf Oct 16 '15 at 06:51
  • I don't have any specific answers for you - I just thought that the unixwiz.net article had some useful guidance in this area. – Paul R Oct 16 '15 at 07:19
  • 1
    You're obviously asking an [XY question](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). It might be interesting to see the code and the details of the data transfer that require `VTIME = 0` and `VMIN = 0`. The real problem is understanding the data requirements and what's wrong with the code that seems to require what you describe. – sawdust Oct 16 '15 at 08:12
  • See [termios](http://man7.org/linux/man-pages/man3/termios.3.html). Personally, I'd be more likely to use a timeout signal to interrupt a blocking read/write/send/receive anyway, so sawdust's comment about this being an XY question is apt: the original problem is more likely to be solved in a way quite different than what sdfsfssfsdf has chosen. We don't know for sure, though, because the question is about a specific solution instead of the actual problem to be solved. This is annoying; I would've liked to try and help. – Nominal Animal Oct 16 '15 at 14:55
  • @sdfsfssfsdf -- What you've shown so far doesn't look as bad as I feared. You're properly using **tc[gs]etattr()** and checking the return codes; that's rare but nice to see. The assignments of c_lflag and c_lflag aren't compliant; see [Setting Terminal Modes Properly](http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_12.html#SEC237). Formatting is inconsistent; but worse has been posted. You need to post the salient procedure **write_and_send()**. And what is at the other end of the serial link, an RFID reader? Make and model? – sawdust Oct 19 '15 at 00:26
  • @sdfsfssfsdf: Delivery of a signal, even to an empty signal handler function, will interrupt all blocking I/O calls on that thread (if the signal handler is installed without the `SA_RESTART` flag). See [here](http://stackoverflow.com/a/12765121/1475978) for an example minilibrary for multiplexed timeouts (many overlapping timeouts) for a single-threaded program. [Here](http://stackoverflow.com/a/29956584/1475978) is an example similar to yours, except with a barcode reader (that shows up as a keyboard in Linux) instead of a serial device. ... – Nominal Animal Oct 19 '15 at 18:32
  • ... @sdfsfssfsdf: [Here](http://stackoverflow.com/a/29307379/1475978) are a few notes wrt. timeouts and flushing. Typically, the main issue especially with USB serial devices is that you might need to be ready to read at all times, not just after you have written a command. Personally, I avoid the mess by using a separate read (or write) thread. A variant of the above timeout minilib will then let you specify timeouts not just per operation, but per logical set of operations, too. You just haven't provided enough information yet, just snippets. – Nominal Animal Oct 19 '15 at 18:39

0 Answers0