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