graphlcd-base/glcddrivers/port.c
changeset 5 37602e25a04a
parent 4 df6a40031aa5
child 7 aac14789cd1d
     1.1 --- a/graphlcd-base/glcddrivers/port.c	Wed Feb 06 17:32:55 2008 +0000
     1.2 +++ b/graphlcd-base/glcddrivers/port.c	Wed Feb 06 17:37:50 2008 +0000
     1.3 @@ -16,6 +16,7 @@
     1.4  #include <syslog.h>
     1.5  #include <unistd.h>
     1.6  #include <termios.h>
     1.7 +#include <signal.h>
     1.8  #include <sys/io.h>
     1.9  #include <sys/ioctl.h>
    1.10  #include <linux/ppdev.h>
    1.11 @@ -25,6 +26,9 @@
    1.12  
    1.13  #include "port.h"
    1.14  
    1.15 +
    1.16 +
    1.17 +
    1.18  namespace GLCD
    1.19  {
    1.20  
    1.21 @@ -282,9 +286,10 @@
    1.22  {
    1.23  }
    1.24  
    1.25 -int cSerialPort::Open(const char * device)
    1.26 +int cSerialPort::Open(const char * device, void (*FP)(int))
    1.27  {
    1.28      struct termios options;
    1.29 +    struct sigaction saio;
    1.30  
    1.31      fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
    1.32      if (fd == -1)
    1.33 @@ -292,8 +297,19 @@
    1.34          printf("error opening port\n");
    1.35          return -1;
    1.36      }
    1.37 -    //fcntl(fd, F_SETFL, FNDELAY);
    1.38 -    fcntl(fd, F_SETFL, 0);
    1.39 +
    1.40 +
    1.41 +    // install the signal handler before making the device asynchronous/
    1.42 +    saio.sa_handler = FP;
    1.43 +    sigemptyset (&saio.sa_mask);
    1.44 +    saio.sa_flags = 0;
    1.45 +    saio.sa_restorer = NULL;
    1.46 +    sigaction(SIGIO,&saio,NULL);
    1.47 +    
    1.48 +    // allow the process to receive SIGIO
    1.49 +    fcntl(fd, F_SETOWN, getpid());
    1.50 +    // Make the file descriptor asynchronous 
    1.51 +    fcntl(fd, F_SETFL, FASYNC);
    1.52  
    1.53      tcgetattr(fd, &options);
    1.54  
    1.55 @@ -328,11 +344,11 @@
    1.56      return 0;
    1.57  }
    1.58  
    1.59 -int cSerialPort::ReadData(unsigned char * data)
    1.60 +int cSerialPort::ReadData(unsigned char * data, int num)
    1.61  {
    1.62      if (fd == -1)
    1.63          return 0;
    1.64 -    return read(fd, data, 1);
    1.65 +    return read(fd, data, num);
    1.66  }
    1.67  
    1.68  void cSerialPort::WriteData(unsigned char data)
    1.69 @@ -347,4 +363,6 @@
    1.70      write(fd, data, length);
    1.71  }
    1.72  
    1.73 +
    1.74  } // end of namespace
    1.75 +