kbhit() and non-blocking read

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

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

kweisser@udel.edu
Kevin

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… :slight_smile:


Wojtek Lerch QNX Software Systems Ltd.

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… > :slight_smile:


Wojtek Lerch QNX Software Systems Ltd.

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… > :slight_smile:


Wojtek Lerch QNX Software Systems Ltd.

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