kbhit() and non-blocking read

bridged with qdn.public.qnxrtp.applications
Post Reply
Kevin Weisser

kbhit() and non-blocking read

Post by Kevin Weisser » Wed Jun 05, 2002 5:47 pm

I am writing a serial communications program and have run into two problems.
First I need something similar to DOS's kbhit() is there anything in QNX/C
that could help me. Secondly I would be a big bonus if i used a
non-blocking read call to read from the port. Right now I am using regular
read() is there another function which wouldn't block while there is nothing
to read.

kweisser@udel.edu
Kevin

Xiaodan Tang

Re: kbhit() and non-blocking read

Post by Xiaodan Tang » Wed Jun 05, 2002 6:01 pm

Kevin Weisser <kweisser@udel.edu> wrote:
I am writing a serial communications program and have run into two problems.
First I need something similar to DOS's kbhit() is there anything in QNX/C
that could help me. Secondly I would be a big bonus if i used a
non-blocking read call to read from the port. Right now I am using regular
read() is there another function which wouldn't block while there is nothing
to read.
The GOD of reading serial port is "readcond()", check your lib
reference.

-xtang

Wojtek Lerch

Re: kbhit() and non-blocking read

Post by Wojtek Lerch » Wed Jun 05, 2002 6:40 pm

Xiaodan Tang <xtang@qnx.com> wrote:
The GOD of reading serial port is "readcond()", check your lib
reference.
Of course, there also are portable ways of non-blocking or timed
reading, using the O_NONBLOCK flag or tcsetattr() and the cc_c[VTIME]
parameter in the termios structure... :-)

--
Wojtek Lerch QNX Software Systems Ltd.

Kris Warkentin

Re: kbhit() and non-blocking read

Post by Kris Warkentin » Wed Jun 05, 2002 7:40 pm

I think I've posted this code snippet before but the following raw() and
unraw() functions will put a file descriptor into a mode where one can read
one character at a time as soon as it is typed.

cheers,

Kris

int raw(int fd)
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p ) )
return( -1 );

termios_p.c_cc[VMIN] = 1;
termios_p.c_cc[VTIME] = 0;
termios_p.c_lflag &= ~( ECHO|ICANON|ECHOE|ECHOK|ECHONL );
termios_p.c_oflag &= ~( OPOST );

return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}

int unraw(int fd)
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p ) )
return( -1 );

termios_p.c_lflag |= ( ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL );
termios_p.c_oflag |= ( OPOST );

return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}


"Wojtek Lerch" <wojtek_l@yahoo.ca> wrote in message
news:adllva$jil$1@nntp.qnx.com...
Xiaodan Tang <xtang@qnx.com> wrote:
The GOD of reading serial port is "readcond()", check your lib
reference.

Of course, there also are portable ways of non-blocking or timed
reading, using the O_NONBLOCK flag or tcsetattr() and the cc_c[VTIME]
parameter in the termios structure... :-)

--
Wojtek Lerch QNX Software Systems Ltd.

Xiaodan Tang

Re: kbhit() and non-blocking read

Post by Xiaodan Tang » Wed Jun 05, 2002 8:36 pm

Kris Warkentin <kewarken@qnx.com> wrote:
I think I've posted this code snippet before but the following raw() and
unraw() functions will put a file descriptor into a mode where one can read
one character at a time as soon as it is typed.

cheers,

Kris

int raw(int fd)
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p ) )
return( -1 );

termios_p.c_cc[VMIN] = 1;
termios_p.c_cc[VTIME] = 0;
termios_p.c_lflag &= ~( ECHO|ICANON|ECHOE|ECHOK|ECHONL );
termios_p.c_oflag &= ~( OPOST );
maybe you also need:

termios_p.c_iflag &= ~(IXON | BRKINT | PARMRK);

-xtang
return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}

int unraw(int fd)
{
struct termios termios_p;

if( tcgetattr( fd, &termios_p ) )
return( -1 );

termios_p.c_lflag |= ( ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL );
termios_p.c_oflag |= ( OPOST );

return( tcsetattr( fd, TCSADRAIN, &termios_p ) );
}
"Wojtek Lerch" <wojtek_l@yahoo.ca> wrote in message
news:adllva$jil$1@nntp.qnx.com...
Xiaodan Tang <xtang@qnx.com> wrote:
The GOD of reading serial port is "readcond()", check your lib
reference.

Of course, there also are portable ways of non-blocking or timed
reading, using the O_NONBLOCK flag or tcsetattr() and the cc_c[VTIME]
parameter in the termios structure... :-)

--
Wojtek Lerch QNX Software Systems Ltd.

Thoma

Re: kbhit() and non-blocking read

Post by Thoma » Thu Jun 06, 2002 1:58 pm

Kevin Weisser <kweisser@udel.edu> wrote:
I am writing a serial communications program and have run into two problems.
First I need something similar to DOS's kbhit() is there anything in QNX/C
that could help me. Secondly I would be a big bonus if i used a
non-blocking read call to read from the port. Right now I am using regular
read() is there another function which wouldn't block while there is nothing
to read.
check out select for the *nix way to do it, if you also have to receive messages
or other pulses check out, or you want a signal dropped on your process when the
serial port is ready for reading use ionotify

Tom

Post Reply

Return to “qdn.public.qnxrtp.applications”