Hi everyone,
We had the following working code on QNX4 using qnx_proxy* and dev_*
functions
and I try to get a working equivalent example for QNX RtP,
since we have many apps using console keyboard and serial port
that we have to develop and to support our customer.
It’s basically a skeleton for writing any Keyboard/Console application
and also a skeleton for writing serial port access application.
It basically take any byte from the keyboard of a serial port dumb terminal
and print it on the console, while any byte from the keyboard is print on
a serial port dumb terminal.
Pretty simple and straight forward.
Here’s the QNX4 SerialChat program:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dev.h>
#include <sys/proxy.h>
#include <sys/kernel.h>
#include <conio.h>
#include <i86.h>
#define LOCAL_KEYBOARD “/dev/con1”
#define ELAN104_SERIAL “//2/dev/ser1”
int main()
{
// This is a bidirectionnal CHAT program
// between the ELAN104, the HP Dump Terminal and the Switchboard
printf("\n\rBasic QNX Serial Port CHAT program: \n\r\n\n");
// Serial Port Stuff
unsigned state;
pid_t kb_proxy;
pid_t mdm_proxy;
pid_t pid;
int kb_fd;
int mdm_fd;
int n;
char buf[80];
// Initialize and setup the Serial Port
kb_fd = open( LOCAL_KEYBOARD, O_RDWR );// Local Keyboard
mdm_fd = open( ELAN104_SERIAL, O_RDWR );// QNX-2 Elan104 Board
kb_proxy = qnx_proxy_attach( 0, 0, 0, -1 );
mdm_proxy = qnx_proxy_attach( 0, 0, 0, -1 );
dev_mode( kb_fd, 0, _DEV_MODES );
dev_mode( mdm_fd, 0, _DEV_MODES );
// Clear, then arm the proxies for future events
dev_state( kb_fd, 0, _DEV_EVENT_INPUT );
dev_arm( kb_fd, kb_proxy, _DEV_EVENT_INPUT );
dev_state( mdm_fd, 0, _DEV_EVENT_INPUT );
dev_arm( mdm_fd, mdm_proxy, _DEV_EVENT_INPUT );
for(; // Infinite Loop, until CTRL-BREAK
{
// Wait for either keyboard or modem to send an input.
pid = Receive( 0, 0, 0 );
// Waiting for an event, then check the Process ID,
// Read the message and send it back to the other machine.
if ( pid == kb_proxy ) {
state = dev_state( kb_fd, 0, _DEV_EVENT_INPUT );
// If there is an Input from Keyboard Console
// then Read it and Send it Back to the Modem
if ( state & _DEV_EVENT_INPUT ) {
n = read( kb_fd, &buf, sizeof( buf ) );
write( mdm_fd, &buf, n );
dev_arm( kb_fd, kb_proxy, _DEV_EVENT_INPUT );
}
}
else if ( pid == mdm_proxy )
{
state = dev_state( mdm_fd, 0, _DEV_EVENT_INPUT );
// If there is an Input from Modem
// then Read it and Send it Back to the Keyboard Console
if ( state & _DEV_EVENT_INPUT )
{
n = read( mdm_fd, &buf, sizeof( buf ) );
write( kb_fd, &buf, n );
dev_arm( mdm_fd, mdm_proxy, _DEV_EVENT_INPUT );
}
}
}
printf("\nDone!\n");
return 0;
}
The only sort of QNX RtP non-working equivalent,
I could come up with from the select() documentation was:
/*
- This example opens a console and a serial port for
- read mode, and calls select() with a 5 second timeout.
- It waits for data to be available on either descriptor.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/time.h>
int max( int x, int y )
{
return ( (x)>(y) ? (x) : (y) );
}
int main( void )
{
int console, serial;
struct timeval tv;
fd_set rfd;
int n;
char buf[80];
if( ( console = open( “/dev/con1”, O_RDWR ) ) == -1
|| ( serial = open( “/dev/ser1”, O_RDWR ) ) == -1 )
{
perror( “open” );
return EXIT_FAILURE;
}
/*
- Clear the set of read file descriptors, and
- add the two we just got from the open calls.
*/
FD_ZERO( &rfd );
FD_SET( console, &rfd );
FD_SET( serial, &rfd );
/*
- Set a 5 second timeout.
*/
tv.tv_sec = 5;
tv.tv_usec = 0;
for(; {
switch ( n = select( 1 + max( console, serial ),
&rfd, 0, 0, &tv ) ) {
case -1:
perror( “select” );
return EXIT_FAILURE;
case 0:
puts( “select timed out” );
break;
default:
printf( “%d descriptors ready …\n”, n );
if( FD_ISSET( console, &rfd ) ) {
puts( " – console descriptor has data pending" );
n = read( console, &buf, sizeof( buf ) );
write( serial, &buf, n );
}
if( FD_ISSET( serial, &rfd ) ) {
puts( " – serial descriptor has data pending" );
n = read( serial, &buf, sizeof( buf ) );
write( console, &buf, n );
}
}
}
return EXIT_SUCCESS;
}
But that code doesn’t work, the keyboard states that bytes has to be
written,
but nothing show up on the dumb terminal, and nothing from the dumb terminal
show up on the Local Console. =(
Anybody could give us a basic working example ?
That would be just great.
Sincerly yours,
Fred.