Hi!
I am working on simple project which will extract temperature from microcontroller. It is connected to computer through serial-to-usb chip.
The testing of connection is: I send to mc one char "p" and it answer me with "p\0" (2 bytes). And even with this simple interchange I have encountered a problem.
Code:
#include<stdio.h>
#include<errno.h>
#include<termios.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
int tty_set(int tty)
{
struct termios tty_opts;
int foo;
foo=tcgetattr(tty,&tty_opts);
if(foo<0) return -1;
foo=cfsetispeed(&tty_opts,B9600);
if(foo<0) return -2;
foo=cfsetospeed(&tty_opts,B9600);
if(foo<0) return -3;
//3
// 8N1
tty_opts.c_cflag &= ~PARENB;
tty_opts.c_cflag &= ~CSTOPB;
tty_opts.c_cflag &= ~CSIZE;
tty_opts.c_cflag |= CS8;
// no flow control
tty_opts.c_cflag &= ~CRTSCTS;
tty_opts.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
tty_opts.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
tty_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
tty_opts.c_oflag &= ~OPOST; // make raw
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
tty_opts.c_cc[VMIN] = 0;
tty_opts.c_cc[VTIME] = 0;
//end3
foo=tcsetattr(tty,TCSANOW, &tty_opts);
if(foo<0) return -4;
return 0;
}
int tty_test_connection(int tty)
{
int foo;
char buf[4]={'p',0,0,0};
foo=write(tty,buf,1);
if(foo<0){
perror("write");
return foo;
}
if(foo==0){
puts("nothing written");
return foo;
}
L0:
foo=read(tty,buf,2); //mc should return 2 bytes p and \0
if(foo<0){
perror("read");
return foo;
}
if(foo<2){
puts("less then 2 bytes was red");
return foo;
}
buf[foo]='\0';
printf("ping was: %s\n",buf);
return foo;
}
int main(int ac,char *av[])
{
int tty_dev;
int foo;
tty_dev=0;
tty_dev = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if(tty_dev < 0){
perror("open");
return -1;
}
puts("open: ok");
foo=tty_set(tty_dev);
if(foo<0){
perror("tty_set");
close(tty_dev);
return -1;
}
puts("tty_set: ok");
foo=tty_test_connection(tty_dev);
if(foo<0){
perror("tty_test_connection");
printf("foo: %d\n",foo);
return -1;
}
printf("tty_test_connection: %d characters red\n",foo);
close(tty_dev);
return 0;
}
The problem is: when read() (see L0 lebel) is executed it returns either "resource temporarily unavailable" or 0.
* mc is working fine. I tested it with screen(1) and minicom(1)
* if set VTIME or VMIN in not 0 value it behaves the same.
* using fscanf(3) just block the program.
* google does not give sufficient results.
Does anyone know where is the problem?
Thanks in advance.