graphlcd-base/glcddrivers/port.c
changeset 29 2723893dc865
parent 21 f1573dd6dace
child 30 7fd00015f62f
     1.1 --- a/graphlcd-base/glcddrivers/port.c	Wed Mar 25 17:48:05 2009 +0100
     1.2 +++ b/graphlcd-base/glcddrivers/port.c	Mon Mar 30 18:40:54 2009 +0200
     1.3 @@ -290,70 +290,79 @@
     1.4  {
     1.5      struct sigaction saio;
     1.6  
     1.7 -    if (FP == NULL)
     1.8 +    if (FP == NULL) {
     1.9 +        return -1;
    1.10 +    }
    1.11 +
    1.12 +    if (Open(device) == 0) {
    1.13 +    	// install the signal handler before making the device asynchronous/
    1.14 +    	saio.sa_handler = FP;
    1.15 +    	sigemptyset (&saio.sa_mask);
    1.16 +    	saio.sa_flags = 0;
    1.17 +    	saio.sa_restorer = NULL;
    1.18 +    	sigaction(SIGIO,&saio,NULL);
    1.19 +    
    1.20 +    	// allow the process to receive SIGIO
    1.21 +    	fcntl(fd, F_SETOWN, getpid());
    1.22 +    	// Make the file descriptor asynchronous 
    1.23 +    	fcntl(fd, F_SETFL, FASYNC | O_NONBLOCK);
    1.24 +
    1.25 +    	return(0);
    1.26 +    }
    1.27 +    else {
    1.28 +    	return -1;
    1.29 +    }
    1.30 +}
    1.31 +
    1.32 +int cSerialPort::Open(const char * device)
    1.33 +{
    1.34 +    struct termios options;
    1.35 +
    1.36 +    fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
    1.37 +    if (fd == -1)
    1.38      {
    1.39          printf("error opening port\n");
    1.40          return -1;
    1.41      }
    1.42  
    1.43 -    Open(device);
    1.44 -
    1.45 -    // install the signal handler before making the device asynchronous/
    1.46 -    saio.sa_handler = FP;
    1.47 +    tcgetattr(fd, &options);
    1.48 +
    1.49 +    cfsetispeed(&options, B921600);
    1.50 +    cfsetospeed(&options, B921600);
    1.51 +
    1.52 +    options.c_cflag &= ~PARENB;
    1.53 +    options.c_cflag &= ~CSTOPB;
    1.54 +    options.c_cflag &= ~CSIZE;
    1.55 +    options.c_cflag |= CS8;
    1.56 +
    1.57 +    options.c_cflag &= ~CRTSCTS;
    1.58 +
    1.59 +    options.c_cflag |= (CLOCAL | CREAD);
    1.60 +
    1.61 +    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    1.62 +
    1.63 +    options.c_iflag &= ~(IXON | IXOFF | IXANY);
    1.64 +
    1.65 +    options.c_oflag &= ~OPOST;
    1.66 +
    1.67 +    tcsetattr(fd, TCSANOW, &options);
    1.68 +
    1.69 +    return 0;
    1.70 +}
    1.71 +
    1.72 +int cSerialPort::Close()
    1.73 +{
    1.74 +    struct sigaction saio;
    1.75 +    if (fd == -1)
    1.76 +        return -1;
    1.77 +    close(fd);
    1.78 +    
    1.79 +    saio.sa_handler = NULL;
    1.80      sigemptyset (&saio.sa_mask);
    1.81      saio.sa_flags = 0;
    1.82      saio.sa_restorer = NULL;
    1.83      sigaction(SIGIO,&saio,NULL);
    1.84 -    
    1.85 -    // allow the process to receive SIGIO
    1.86 -    fcntl(fd, F_SETOWN, getpid());
    1.87 -    // Make the file descriptor asynchronous 
    1.88 -    fcntl(fd, F_SETFL, FASYNC | O_NONBLOCK);
    1.89 -
    1.90 -    return(0);
    1.91 -}
    1.92 -
    1.93 -int cSerialPort::Open(const char * device)
    1.94 -{
    1.95 -    struct termios options;
    1.96 -
    1.97 -    fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
    1.98 -    if (fd == -1)
    1.99 -    {
   1.100 -        printf("error opening port\n");
   1.101 -        return -1;
   1.102 -    }
   1.103 -
   1.104 -    tcgetattr(fd, &options);
   1.105 -
   1.106 -    cfsetispeed(&options, B921600);
   1.107 -    cfsetospeed(&options, B921600);
   1.108 -
   1.109 -    options.c_cflag &= ~PARENB;
   1.110 -    options.c_cflag &= ~CSTOPB;
   1.111 -    options.c_cflag &= ~CSIZE;
   1.112 -    options.c_cflag |= CS8;
   1.113 -
   1.114 -    options.c_cflag &= ~CRTSCTS;
   1.115 -
   1.116 -    options.c_cflag |= (CLOCAL | CREAD);
   1.117 -
   1.118 -    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
   1.119 -
   1.120 -    options.c_iflag &= ~(IXON | IXOFF | IXANY);
   1.121 -
   1.122 -    options.c_oflag &= ~OPOST;
   1.123 -
   1.124 -    tcsetattr(fd, TCSANOW, &options);
   1.125 -
   1.126 -    return 0;
   1.127 -}
   1.128 -
   1.129 -int cSerialPort::Close()
   1.130 -{
   1.131 -    if (fd == -1)
   1.132 -        return -1;
   1.133 -    close(fd);
   1.134 +
   1.135      return 0;
   1.136  }
   1.137  
   1.138 @@ -374,6 +383,7 @@
   1.139      if (fd == -1)
   1.140          return;
   1.141      write(fd, data, length);
   1.142 +//    for (int x=0; x<length; x++)   printf("0x%x ",data[x]);
   1.143  }
   1.144  
   1.145