省肿心蚞蚂切换器

2009年4月23日星期四

省肿心蚞蚂切换器

/*
Serial Port  &  Parallel Port
*/


#include  </usr/include/linux/parport.h>
#include  </usr/include/linux/ppdev.h>
#include  <errno.h>
#include  <fcntl.h>     //O_RDWR
#include  <stdint.h>
#include  <stdio.h>
#include  <stdlib.h>
#include  <sys/ioctl.h>
#include  <sys/poll.h>
#include  <sys/stat.h>
#include  <sys/syscall.h>
#include  <sys/types.h>
#include  <termios.h>
#include  <time.h>
#include  <unistd.h>



#define JJ 256


int speed_arr[] = {B38400, B19200, B9600, B4800, B2400, B1200, B300};
int  name_arr[] = { 38400,  19200,  9600,  4800,  2400,  1200,  300};



main()
{
 // property
 
 // serial
 
    int i;
    int fd_com;
    int nread;


    int length;
    int nByte;
 
    char buff_com[JJ] = "this is a message from lgz";


    char *dev_com = "/dev/ttyS0";
   
    // parallel
   
    int fd_par;


    char buff_par[JJ];
    //char *ptr = buff_par;
    char *ptr = buff_com;
    size_t got;


    int mode; /* We'll need this later. */
    int written;


    char *dev_par = "/dev/parport0";


    // public
   
    // serial
   
    fd_com = open(dev_com, O_RDWR);  
    if (fd_com == -1)
    {   perror("Can't Open Serial Port");
        return -1;  
    }


    set_speed(fd_com, 9600);
    if (set_Parity(fd_com, 8, 1, 'N') == 0)
    {   printf("Set Parity Error\n");
        exit (0);
    }


    // parallel
   
    fd_par = open (dev_par, O_RDWR);
    if (fd_par == -1)
    {   perror ("Can't Open Parallel Port");
        return -1;
    }


    if (ioctl (fd_par, PPCLAIM))
    {   perror("PPCLAIM");
        return -1;
    }


    mode = IEEE1284_MODE_COMPAT;
    if (ioctl (fd_par, PPNEGOT, &mode))
    {   perror ("PPNEGOT");
        return -1;
    }



    while (1)
    {
        memset(buff_com, '\0', JJ);
   
        while ( (nread=read(fd_com, buff_com, JJ)) > 0 )
        {   printf("\n\n nread=%d\n", nread);
            buff_com[nread+1] = '\0';
            printf("buff_com=[%s]\n", buff_com);
           
            if (nread == 1)
            {   int written = write_printer (fd_par, ptr, 1);
                printf("\nwritten=%d\n", written);
               
                if (written < 0)
                {   perror ("write");
                    close (fd);
                    return -1;
                }
            }
        }
    }
}





//==================================================
// Serial Port
//==================================================


void set_speed(int fd, int speed)
{ int  i;
  int  status;
  struct termios opt;
  tcgetattr(fd, &opt);


  for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++) {
    if  (speed == name_arr[i]) {
      tcflush(fd, TCIOFLUSH);
      cfsetispeed(&opt, speed_arr[i]);
      cfsetospeed(&opt, speed_arr[i]);
      status = tcsetattr(fd, TCSANOW, &opt);
      if  (status != 0) {
        perror("tcsetattr fd");
        return;
      }
      tcflush(fd, TCIOFLUSH);
    }
  }
}



int set_Parity(int fd,int databits,int stopbits,int parity)
{ struct termios options;


  if  ( tcgetattr( fd,&options)  !=  0) {
    perror("SetupSerial 1");
    return(0);
  }
 
  options.c_cflag &= ~CSIZE;
  switch (databits)
  { case 7:
      options.c_cflag |= CS7;
      break;
    case 8:
      options.c_cflag |= CS8;
      break;
    default:
      fprintf(stderr,"Unsupported data size\n");
      return (0);
  }


  switch (parity)
  { case 'n':
    case 'N':
      options.c_cflag &= ~PARENB;   /* Clear parity enable */
      options.c_iflag &= ~INPCK;     /* Enable parity checking */
      break;
    case 'o':
    case 'O':
      options.c_cflag |= (PARODD | PARENB);
      options.c_iflag |= INPCK;             /* Disnable parity checking */
      break;
    case 'e':
    case 'E':
      options.c_cflag |= PARENB;     /* Enable parity */
      options.c_cflag &= ~PARODD;  
      options.c_iflag |= INPCK;       /* Disnable parity checking */
      break;
    case 'S':
    case 's':  /*as no parity*/
      options.c_cflag &= ~PARENB;
      options.c_cflag &= ~CSTOPB;break;
    default:
      fprintf(stderr,"Unsupported parity\n");
      return (0);
    }


 
  switch (stopbits)
  { case 1:
      options.c_cflag &= ~CSTOPB;
      break;
    case 2:
      options.c_cflag |= CSTOPB;
      break;
    default:
      fprintf(stderr,"Unsupported stop bits\n");
      return (0);
  }


  /* Set input parity option */
  if (parity != 'n') options.c_iflag |= INPCK;


  tcflush(fd,TCIFLUSH);
  options.c_cc[VTIME] = 150;
  options.c_cc[VMIN] = 0; /* Update the options and do it NOW */


  // Raw Mode
  options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //Input
  options.c_oflag &= ~OPOST; //Output


  if (tcsetattr(fd, TCSANOW, &options) != 0) {
    perror("SetupSerial 3");
    return (0);
  }


  return (1);
}






//==================================================
// Parallel Port
//==================================================


ssize_t write_printer (int fd, const void *ptr, size_t count)
{
    ssize_t wrote = 0;


    while (wrote < count)
    {   unsigned char status, control, data;
        unsigned char mask = (PARPORT_STATUS_ERROR | PARPORT_STATUS_BUSY);
        unsigned char val = (PARPORT_STATUS_ERROR | PARPORT_STATUS_BUSY);
        struct ppdev_frob_struct frob;
        struct timespec ts;


        /* Wait for printer to be ready */
        //for (;;) {
            ioctl (fd, PPRSTATUS, &status);


            if ((status & mask) == val)
                ;//break;


            ioctl (fd, PPRELEASE);
            //sleep (1);
            ioctl (fd, PPCLAIM);
        //}


        /* Set the data lines */
        data = * ((char *) ptr)++;
        ioctl (fd, PPWDATA, &data);


        /* Delay for a bit */
        ts.tv_sec = 0;
        ts.tv_nsec = 1000;
        //nanosleep (&ts, NULL);


        /* Pulse strobe */
        frob.mask = PARPORT_CONTROL_STROBE;
        frob.val = PARPORT_CONTROL_STROBE;
        ioctl (fd, PPFCONTROL, &frob);
        //nanosleep (&ts, NULL);


        /* End the pulse */
        frob.val = 0;
        ioctl (fd, PPFCONTROL, &frob);
        //nanosleep (&ts, NULL);


        wrote++;
    }


    return wrote;
}


 


0 评论:

发表评论