Subject: Re: Serial Port Read/Write
To: Alain Volmat <netbsd-users@netbsd.org>
From: Steven Grunza <steven_grunza@ieee.org>
List: netbsd-users
Date: 12/06/2000 10:07:24
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I usually use a select() loop for stuff like this; however, the
FNDELAY ( a.k.a. O_NONBLOCK) causes a read to return -1 if there is
not data ready to be read and errno is set to EAGAIN.

I would suggest adding a check to the (count < 0) case to see if
errno is EAGAIN and re-try the read() call until count is less than
zero and errno is not EAGAIN.

Just a suggestion...I usually do this type of stuff on TCP/IP ports
but I believe select() should work in this case....

At 05:05 PM 12/6/00 +0900, Alain Volmat wrote:
>Hi,
>
>I'm trying to access the serial port from a very simple program. I'm
>using NetBSD1.4.3 i386 and I would like the program return me the
>contents of the serial port buffer / or 0 if there is no data.
>I write this little program but every time the read function return
>-1. I know that I use the correct port and also configuration is (I
>think correct) because when I use write it works correctly.
>Does anyone have an idea about that ??
>
>Thanks in advance.
>
>Alain Volmat
>
>----------------------
>
>#include<stdio.h>
>#include<fcntl.h>
>#include<errno.h>
>#include<termios.h>
>#include<malloc.h>
>
>main()
>{
>  int count;
>  int fd,i;
>
>  char *buff;
>
>  int buffsize = 255;
>
>  buff = (char*)malloc(255);
>
>  fd = open("/dev/tty00",O_RDWR | O_NOCTTY | O_NDELAY );
>  if( fd==-1)
>        perror("unable to open the port");
>  else
>   fcntl(fd, F_SETFL, FNDELAY);
>
>  count = read(fd,buff,(size_t)buffsize);
> if(count < 0){
>        printf("read error!\n");
>        return 1;
> }
> for(i=0;i<count;i++)
>  printf("[read]%x %x\n",i,buff[i]);
>
>  close(fd);
>}
>
>
>

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBOi5WLL5JUwXGNWihEQIwhgCdF+cHRkvKKZDvi5sq9vUWyGpz6y8AoNE6
myemuAEE9d4qsErg7j1eLP65
=0aJ4
-----END PGP SIGNATURE-----



----------------------------------------------------------------------
"Luke, you're going to find that many | Steven Grunza
of the truths we cling to depend      | voice:  (856) 787 - 2759 
greatly on our own point of view."    | fax:    (856) 866 - 2033
 - Obi Wan Kenobi, Return of the Jedi | e-mail: steven_grunza@ieee.org
----------------------------------------------------------------------