From: Gerald Emig <gme@emig-software.de>
To: linux-serial@vger.kernel.org
Subject: Re: result=read(fd,buffer,200);
Date: Tue, 20 Jul 2004 17:41:42 +0200 [thread overview]
Message-ID: <20040720174142.7b8b6525@emig4.heisch.inka.de> (raw)
In-Reply-To: <BAY15-F17edoJhTahSR0009232d@hotmail.com>
HI,
According to the documentation provided in info:libc, read
returns the number of character it actually has read.
Especially if you are using serial ports, the amount read is often fewer
as you expected.
Additionally, checking errno is stronly recommended.
Use something like the following to read exactly 200 chars:
int read_amount (int iwantread)
{
int readpointer, fetch;
readpointer=0;
while (iwantread>0) {
fetch=read (fd,&buffer[readpointer],iwantread);
if (fetch >= 0) {
readpointer += fetch;
iwantread -= fetch;
} else if ((errno!=EINTR) && (errno!=EAGAIN)) return -1;
}
return iwantread; // should be 0 on success
}
Best regards,
Gerald Emig
On Tue, 20 Jul 2004 15:12:24 +0200
"Sayang Oin" <sayangoin@hotmail.com> wrote:
> Hello,
>
> I would like to read for example 200 bytes from my UART device
> throught the serial port.
>
> I use
>
> result=read(fd,buffer,200);
>
> I don't know why the result always lesser then 200 bytes.
>
> my open_port is like this:
>
> int open_port ()
> {
>
> struct termios options, oldtio;
>
> int fd;
>
> fd = open (COM,O_RDWR | O_NOCTTY );
>
>
> if (fd < 0)
> {
> fprintf(stderr, "Open_port: unable to open %s\n", COM);
> exit (1);
> }
> else
> {
> fcntl (fd, F_SETFL, FNDELAY);
> printf("\nopen_port:
> %s,%s,%d,%d,%s\n",COM,BAUD,stopbits,datenbits,PARITY);
> }
>
> bzero(&options, sizeof(options));
>
>
> if(!strcmp(BAUD,"B9600"))
> {
> cfsetispeed (&options, B9600);
> cfsetospeed (&options, B9600);
> }
> if(!strcmp(BAUD,"B19200"))
> {
> cfsetispeed (&options, B19200);
> cfsetospeed (&options, B19200);
> }
> if(!strcmp(BAUD,"B38400"))
> {
> cfsetispeed (&options, B38400);
> cfsetospeed (&options, B38400);
> }
> if(!strcmp(BAUD,"B57600"))
> {
> cfsetispeed (&options, B57600);
> cfsetospeed (&options, B57600);
> }
> if(!strcmp(BAUD,"B115200"))
> {
> cfsetispeed (&options, B115200);
> cfsetospeed (&options, B115200);
> }
>
>
>
> switch(stopbits)
> {
> case 1:
> options.c_cflag &= ~CSTOPB;
> break;
> case 2:
> options.c_cflag |= CSTOPB;
> break;
> }
>
> options.c_cflag &= ~CSIZE;
> switch(datenbits)
> {
> case 5:
> options.c_cflag |= CS5;
> break;
> case 6:
> options.c_cflag |= CS6;
> break;
> case 7:
> options.c_cflag |= CS7;
> break;
> case 8:
> options.c_cflag |= CS8;
> break;
> }
>
> if(!strcmp(PARITY,"even"))
> {
> options.c_cflag |= PARENB;
> options.c_cflag &= ~PARODD;
> }
> else if(!strcmp(PARITY,"odd"))
> {
> options.c_cflag |= PARENB;
> options.c_cflag |= PARODD;
> }
> else if(!strcmp(PARITY,"no"))
> {
> options.c_cflag &= ~PARENB;
> }
>
> options.c_cflag &= ~CRTSCTS;
>
>
> options.c_cflag |= (CLOCAL | CREAD);
>
> options.c_lflag &= ~(ICANON | ECHO | ECHOE |ISIG);
>
> cfmakeraw (&options);
>
> tcflush(fd, TCIFLUSH);
> tcsetattr (fd, TCSAFLUSH, &options);
>
> return fd;
> }
>
>
> Thanks for any advice..
>
> Sayangoin
>
> _________________________________________________________________
> MSN Toolbar kostenloser Pop-Up Blocker Jetzt herunterladen
> http://toolbar.msn.de Jetzt kostenlos downloaden!
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-serial" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
-
To unsubscribe from this list: send the line "unsubscribe linux-serial" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2004-07-20 15:41 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-07-20 13:12 result=read(fd,buffer,200); Sayang Oin
2004-07-20 15:41 ` Gerald Emig [this message]
2004-07-20 17:09 ` result=read(fd,buffer,200); Jan-Benedict Glaw
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20040720174142.7b8b6525@emig4.heisch.inka.de \
--to=gme@emig-software.de \
--cc=linux-serial@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox