problem while reading data from serial port

I’m working with a pc104 card with qnx installed on it, and i have to get a connection between this card and a merlin mvb board. My problem is I can open a connection via the serial port, it seems to work when i send a message /a data on this port but I’m not sure because i am supposed to receive an answer from the board but when I try to read on the port to see if I have the answers, I have an error:“ressource temporarily unavailable”. Does someone have an idea of why it doesn’t work.

This error usually mean the red buffer is empty. There are many possible scenario:

  • Wrong connection;
  • Didn’t wait long enough to read after a write
  • Other end didn’t understand the message ( wrong baud rate )

indeed the baudrate was at a bad speed. I can now Know the number of bytes in the buffer, but when I try to read, I’ve an error and perror print “Bad address”, but the address I use to read is the same than I use to know the size of the data in the buffer (the file descriptor of the port). So why do I have an error?
Thanks again for the answer

Post some code

so this is the opening code:

[code]struct termios options;
int fd; /* File descriptor for the port */
int n;

fd = open("/dev/ser3", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
perror("open_port: Unable to open /dev/ser1 - ");
fcntl(fd, F_SETFL, O_NONBLOCK);
tcgetattr(fd, &options);

/configure communication/
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSIZE; /* Mask the character size bits /
options.c_cflag |= CS8; /
Select 8 data bits */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
n=tcsetattr(fd, TCSANOW, &options);

and this is the reading code

int n=0;
int bytes;
struct timespec tempo;
	/*read the number of byte available in the buffer*/
  ioctl(fd, FIONREAD, &bytes);
  /*read the data in the buffer*/
	tempo.tv_sec = 0;
	tempo.tv_nsec = 500000000;
	nanosleep(&tempo, NULL);
	n = read(fd, buffer, bytes);
  	if (n <= 0)

And I always have the perror message:“read failed(): Bad address”

You didn’t specify the part that was important. Bad address usually mean bad address ;-) My guess is the problem is the variable buffer, which we don’t see in your code.

Also why the nanosleep? If you have to, why not use delay(5) instead would probably make the code more readable.

Depending on your design this may on may not work, because I said between the ioctl and the read more bytes may have been inserted in the serial port buffer.

Personnally I like to use serial port in NON BLOCKING mode. That means when you do a read you can ask for a big number of bytes like 16k but because it is non blocking, read() will return with what ever is in the buffer.

That way in a nice loop you can get all the data from the serial buffer.