Salve a tutti, ho un grosso probelam con la comunicazione seriale e il C. è da diversi giorni che ci studio sopra ma non sono ancora riuscito a risolvere il problema.

Ho due dispositivi, il primo fa una richeista e il secondo risponde. Praticamente ogni 5 secondi invio una richiesta ad un dispositivo che mi risponde. E fin qui tutto funziona correttamente. Il problema sorge quando il dispositivo ricevente si spegne. A questo punto il sistema va in blocco attendendo una risposta che non avrà mai. Inserisco il codice e spero che qualcuno possa aiutarmi perchè sono proprio a corto di soluzioni.


con questa funzione praticamente apro la porta e setto le impostazioni.

codice:
int openPort(){
	int fd;

	struct termios tty_attributes;

	if ((fd = open("/dev/ttyUSB0",O_RDWR|O_NOCTTY|O_NONBLOCK))<0) {
		fprintf (stderr,"Errore nell'apertura della porta: %s\n", strerror(errno));
		exit(EXIT_FAILURE);
	}
	else{
		tcgetattr(fd,&tty_attributes);

		// c_cflag
		// Enable receiver
		tty_attributes.c_cflag |= CREAD;

		// 8 data bit
		tty_attributes.c_cflag |= CS8;

		// c_iflag
		// Ignore framing errors and parity errors.
		tty_attributes.c_iflag |= IGNPAR;

		// DISABLE this: Echo input characters.
		tty_attributes.c_lflag &= ~(ICANON);

		tty_attributes.c_lflag &= ~(ECHO);

		// DISABLE this: If ICANON is also set, the ERASE character erases the preceding input
		// character, and WERASE erases the preceding word.
		tty_attributes.c_lflag &= ~(ECHOE);

		// DISABLE this: When any of the characters INTR, QUIT, SUSP, or DSUSP are received, generate the corresponding signal.
		tty_attributes.c_lflag &= ~(ISIG);

		// Minimum number of characters for non-canonical read.
		tty_attributes.c_cc[VMIN]=8;

		// Timeout in deciseconds for non-canonical read.
		tty_attributes.c_cc[VTIME]=10;

		// Set the baud rate
		cfsetospeed(&tty_attributes,B19200);
		cfsetispeed(&tty_attributes,B19200);

		//salvataggio delle impostazioni sulla porta
		tcsetattr(fd, TCSANOW, &tty_attributes);

	}
	//ritorna il valore di fd
	return fd;
}
con questa funzione invio il dato: ovviamente command contiene i 10 byte da inviare.
codice:
void writeQuery(int fd, char *command){
	int iOut = write(fd, command, 10);
	if (iOut < 0){fputs("write failed!\n", stderr);}
}
e infine con questa funzione leggo la risposta:
codice:
void readQuery(int fd,char *response){
			int iIn = read(fd,response,8);
			if (iIn <= 0) {
				if (errno == EAGAIN) {printf("SERIAL EAGAIN ERROR\n");}
				else {printf("SERIAL read error %d %s\n", errno, strerror(errno));}
			}
			toString("::->", response, 8);
}
premetto che non ottengo nessun errore ma solo il sistema si ferma per aspettare una risposta che non verrà mai.

Grazie!