Can outsiders get simple questions answered?

Dan Hensley dan.hensley@sdrc.com
Thu Dec 31 11:21:00 GMT 1998


Chris,

Christopher Faylor wrote:

> >> >* When I set both serial port and keyboard for polling by select, and I
> >> >enter something via the keyboard, FD_ISSET() tells me that both the
> >> >keyboard and the serial port had input.
> >>
> >> My test case doesn't show this.  Sorry.

    I've tried setting up read() to read only one byte from the serial port (my
input string is 9 bytes), and it still hangs.  It seems to me that something is
wrong with read().  I can't debug it here yet (can't get b20.1 to compile yet),
though.  Reading the keyboard buffer works fine every time.  I haven't tried
messing with VMIN or VTIME yet, but it seems that read() has an issue.  Can
anyone else confirm?  This behavior also happens when doing a straight read
from the serial port without using select() first.
     I stuck my source code below.
Thanks,
Dan

------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>

#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

#define BUFFERSIZ 30

int main( void ) {

  int   i, j,
   idBarc,
   iStat,
   iSet,
   iRet ;

  fd_set  rfds ;
  struct  timeval tv ;

  struct termios termios ;

  char   pcDevNm[]="/dev/com1",
   pcBuf[101],
   cBuf[BUFFERSIZ]="Enter something: " ;

 /*
  * Open the serial port for the barcode reader device */

  printf( "Trying to open barcode device %s\n", pcDevNm ) ;

  if ( (idBarc =  open( pcDevNm, O_RDWR )) == -1 ) {
    printf( "Cannot open device %s\n", pcDevNm ) ;
    exit( 0 ) ;
  }
  else {
    printf( "Opened barcode device %s, fid=%d\n", pcDevNm, idBarc ) ;
  }

 /*
  * Get the serial port attributes */

  if( (iStat  =  tcgetattr( idBarc, &termios ) ) == -1 ) {
    printf( "Cannot get port attributes...\n" ) ;
    exit( 0 ) ;
  }
  else {
    printf( "Got serial port attributes\n" ) ;
    printf( "===== Here's the original setup =====\n" ) ;
    printf( "  termios_iflag=0x%x\n", termios.c_iflag ) ;
    printf( "  termios_oflag=0x%x\n", termios.c_oflag ) ;
    printf( "  termios_cflag=0x%x\n", termios.c_cflag ) ;
    printf( "  termios_lflag=0x%x\n", termios.c_lflag ) ;
    printf( "=====================================\n" ) ;
  }

 /*
  * Set the serial port attributes */

  termios.c_iflag =  IGNBRK |
        ICRNL ;
/*  termios.c_oflag =  (tcflag_t) (NULL) ; */
  termios.c_cflag =  CREAD |
      CS7 |
      PARENB |
      B9600 ;
  termios.c_lflag =  ICANON ;

    printf( "===== Here's what I want to set =====\n" ) ;
    printf( "  termios_iflag=0x%x\n", termios.c_iflag ) ;
    printf( "  termios_oflag=0x%x\n", termios.c_oflag ) ;
    printf( "  termios_cflag=0x%x\n", termios.c_cflag ) ;
    printf( "  termios_lflag=0x%x\n", termios.c_lflag ) ;
    printf( "=====================================\n" ) ;

  if( (iStat  =  tcsetattr( idBarc, TCSANOW, &termios ) ) == -1 ) {
    printf( "Cannot set port attributes...\n" ) ;
    exit( 0 ) ;
  }
  else {
    printf( "Set serial port attributes\n" ) ;
  }

 /*
  * Flush the buffer */

  if( (iStat  =  tcflush( idBarc, TCIOFLUSH ) ) == -1 ) {
    printf( "Cannot flush the buffer...\n" ) ;
    exit( 0 ) ;
  }

 /*
  * Get the serial port attributes */

  if( (iStat  =  tcgetattr( idBarc, &termios ) ) == -1 ) {
    printf( "Cannot get port attributes...\n" ) ;
    exit( 0 ) ;
  }
  else {
    printf( "Got serial port attributes\n" ) ;
    printf( "==== Here's what actually got set ===\n" ) ;
    printf( "  termios_iflag=0x%x\n", termios.c_iflag ) ;
    printf( "  termios_oflag=0x%x\n", termios.c_oflag ) ;
    printf( "  termios_cflag=0x%x\n", termios.c_cflag ) ;
    printf( "  termios_lflag=0x%x\n", termios.c_lflag ) ;
    printf( "=====================================\n" ) ;
  }

 /*
  * Initialize a few things */

  FD_ZERO( &rfds ) ;
  FD_SET( 0, &rfds ) ;
  FD_SET( idBarc, &rfds ) ;

 /*
  * Enter something */

  iRet  =  write( 1, cBuf, 20 ) ;

  iRet  =  select( idBarc+1, &rfds, NULL, NULL, NULL ) ;

  printf( "Select finished, iRet=%d\n", iRet ) ;

 /*
  * Find out where input came from */

  if( iRet != -1 ) {
    iSet  =  -1 ;
    printf( "Data is available now" ) ;

    if(      FD_ISSET(      0, &rfds ) ) {
      printf( " from the keyboard" ) ;
      iSet  =  0 ;
    }
    else if( FD_ISSET( idBarc, &rfds ) ) {
      printf( " from the scanner" ) ;
      iSet  =  idBarc ;
    }
    printf( "\n" ) ;

   /*
    * Get the data */

    printf( "Getting the data from the buffer\n" ) ;

    if( iSet != -1 ) {
      iStat =  read( iSet, (void *) pcBuf, 100 ) ;

      if( iStat > 0 ) {
        pcBuf[ iStat ] =  (char) NULL ;
        printf( "iStat=%d\nRead: '%s'\n", iStat, pcBuf ) ;
      }
      else
        printf( "No data read\n" ) ;

    }

  }
  else
    printf( "No data at all\n" ) ;

 /*
  * Close the COM port */

  close( idBarc ) ;

 exit( 0 ) ;

}
------------------------------------------------------------



-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".



More information about the Cygwin mailing list