Hi,
I'm trying to communicate from the ipaq on the serial port. I can write on it
and see it on the console, but I can't read from the serial port, and even when
I call read(fd, answ, 0) which should return zero according to the read man
page, the read call is blocking and nevers stops ???
Can someone help me ???
Here is my code :
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <errno.h>
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttySA0"
#define _POSIX_SOURCE 1 /* code source conforme à POSIX */
#define FALSE 0
#define TRUE 1
int main(int argc, char **argv)
{
int fd, res;
struct termios oldtio,newtio;
struct sigaction saio; /* définition de l'action du signal */
char *buf, *answ;
__sigset_t aide;
buf = (char *) malloc (10*sizeof(char));
answ = (char *) malloc (10*sizeof(char));
/* ouvre le port en mode non-bloquant (read retourne immédiatement) */
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd <0) {perror(MODEMDEVICE); exit(-1); }
tcgetattr(fd,&oldtio); /* sauvegarde de la configuration courante */
/*valeur pour lecture non canonique : on reçoit tjrs le même nbre de bits */
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
/* positionne le mode de lecture (non canonique, sans echo, ...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; /* timer inter-caractères non utilisé */
newtio.c_cc[VMIN] = 0; /* read bloquant jusqu'à ce que 5 */
/* caractères soient lus */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
printf("je veux ecrire sur le port serie\n");
buf = argv[1];
write(fd,buf,10);
printf("j'ai écrit %s\n", buf);
printf("je veux lire\n");
res=read(fd,answ,0);
if (res == -1)
if (errno == EINTR) printf("read interrompu par le signal !!!\n");
answ[res]=0;
printf("j'ai lu %d charactères: %s\n)",res, answ);
/* restaure les anciens paramètres du port */
tcsetattr(fd,TCSANOW,&oldtio);
return 0;
}
Thx
Valentin
Received on Wed Mar 24 2004 - 13:10:38 EST
This archive was generated by hypermail 2.2.0 : Mon Jul 25 2005 - 18:33:25 EDT