* result=read(fd,buffer,200);
@ 2004-07-20 13:12 Sayang Oin
2004-07-20 15:41 ` result=read(fd,buffer,200); Gerald Emig
0 siblings, 1 reply; 3+ messages in thread
From: Sayang Oin @ 2004-07-20 13:12 UTC (permalink / raw)
To: linux-serial
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!
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: result=read(fd,buffer,200);
2004-07-20 13:12 result=read(fd,buffer,200); Sayang Oin
@ 2004-07-20 15:41 ` Gerald Emig
2004-07-20 17:09 ` result=read(fd,buffer,200); Jan-Benedict Glaw
0 siblings, 1 reply; 3+ messages in thread
From: Gerald Emig @ 2004-07-20 15:41 UTC (permalink / raw)
To: linux-serial
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
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: result=read(fd,buffer,200);
2004-07-20 15:41 ` result=read(fd,buffer,200); Gerald Emig
@ 2004-07-20 17:09 ` Jan-Benedict Glaw
0 siblings, 0 replies; 3+ messages in thread
From: Jan-Benedict Glaw @ 2004-07-20 17:09 UTC (permalink / raw)
To: linux-serial
[-- Attachment #1: Type: text/plain, Size: 1384 bytes --]
On Tue, 2004-07-20 17:41:42 +0200, Gerald Emig <gme@emig-software.de>
wrote in message <20040720174142.7b8b6525@emig4.heisch.inka.de>:
> 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.
That's a general thing to keep in mind.
Additionally, keep in mind that c_cc[VMIN] will control how long to
wait, too...
> Additionally, checking errno is stronly recommended.
As always:)
> Use something like the following to read exactly 200 chars:
>
>
> int read_amount (int iwantread)
This int should have been size_t.
> {
> int readpointer, fetch;
...and fetch needs to be ssize_t.
> 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
> }
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak!
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2004-07-20 17:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-20 13:12 result=read(fd,buffer,200); Sayang Oin
2004-07-20 15:41 ` result=read(fd,buffer,200); Gerald Emig
2004-07-20 17:09 ` result=read(fd,buffer,200); Jan-Benedict Glaw
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox