IvoSilva

noncanonical.c

Oct 10th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. /*Non-Canonical Input Processing*/
  2.  
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <termios.h>
  7. #include <stdio.h>
  8.  
  9. #define BAUDRATE B38400
  10. #define _POSIX_SOURCE 1 /* POSIX compliant source */
  11. #define MODEMDEVICE "/dev/ttyS0"
  12. #define FALSE 0
  13. #define TRUE 1
  14.  
  15. #define F 0x7e
  16. #define A 0x03
  17. #define S 0x03
  18. #define U 0x07
  19.  
  20. //volatile int STOP=FALSE;
  21.  
  22. int main(int argc, char** argv)
  23. {
  24.     int fd,c, res;
  25.     struct termios oldtio,newtio;
  26.     char buf[255];
  27.     char SET[255];
  28.     unsigned char ua[5];
  29. /*
  30.     if ( (argc < 2) ||
  31.          ((strcmp("/dev/ttyS0", argv[1])!=0) &&
  32.           (strcmp("/dev/ttyS1", argv[1])!=0) )) {
  33.       printf("Usage:\tnserial SerialPort\n\tex: nserial /dev/ttyS1\n");
  34.       exit(1);
  35.     }
  36.  
  37. */
  38.   /*
  39.     Open serial port device for reading and writing and not as controlling tty
  40.     because we don't want to get killed if linenoise sends CTRL-C.
  41.   */
  42.  
  43.     system ("clear");
  44.  
  45.     fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  46.     if (fd <0) {perror(argv[1]); exit(-1); }
  47.  
  48.     if ( tcgetattr(fd,&oldtio) == -1) { /* save current port settings */
  49.       perror("tcgetattr");
  50.       exit(-1);
  51.     }
  52.  
  53.     bzero(&newtio, sizeof(newtio));
  54.     newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
  55.     newtio.c_iflag = IGNPAR;
  56.     newtio.c_oflag = 0;
  57.  
  58.     /* set input mode (non-canonical, no echo,...) */
  59.     newtio.c_lflag = 0;
  60.  
  61.     newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
  62.     newtio.c_cc[VMIN]     = 1;   /* blocking read until 1 chars received */
  63.  
  64.  
  65.   /*
  66.     VTIME e VMIN devem ser alterados de forma a proteger com um temporizador a
  67.     leitura do(s) próximo(s) caracter(es)
  68.   */
  69.  
  70.  
  71.  
  72.     tcflush(fd, TCIOFLUSH);
  73.  
  74.     if ( tcsetattr(fd,TCSANOW,&newtio) == -1) {
  75.       perror("tcsetattr");
  76.       exit(-1);
  77.     }
  78.  
  79.     printf("New termios structure set\n");
  80.    
  81.     int aux = 0, state = 0;
  82.     while (state < 5)
  83.     {
  84.     res = read(fd,buf,1);
  85.     printf("%x - %d\n", buf[0], res);
  86.     switch (state)
  87.     {
  88.         case 0:
  89.             if (buf[0] == F)
  90.             {
  91.                 SET[state] = buf[0];
  92.                 state++;
  93.             }
  94.             break;
  95.         case 1:
  96.             if (buf[0] == A)
  97.             {
  98.                 SET[state] = buf[0];
  99.                 state++;
  100.             }
  101.             else if (buf[0] != F && buf[0] != A)
  102.                 state--;
  103.             break;
  104.         case 2:
  105.             if (buf[0] == S)
  106.             {
  107.                 SET[state] = buf[0];
  108.                 state++;
  109.             }
  110.             else if (buf[0] == F)
  111.                 state--;
  112.             else
  113.                 state = 0;
  114.             break;
  115.         case 3:
  116.             if (buf[0] == (A ^ S))
  117.             {
  118.                 SET[state] = buf[0];
  119.                 state++;
  120.             }
  121.             else if (buf[0] == F)
  122.                 state = 1;
  123.             else
  124.                 state = 0;
  125.             break;
  126.         case 4:
  127.             if (buf[0] == F)
  128.             {
  129.                 SET[state] = buf[0];
  130.                 state++;
  131.             }
  132.             else
  133.                 state = 0;
  134.             break;
  135.     }
  136.     }
  137.  
  138.     printf ("Printing SET:\n");
  139.     int i=0;
  140.     for(i; i < state; i++)
  141.         printf("%x", SET[i]);
  142.     printf("\n");
  143.  
  144.     ua[0] = F;
  145.     ua[1] = A;
  146.     ua[2] = U;
  147.     ua[3] = (A ^ U);
  148.     ua[4] = F;
  149.  
  150.     res = write(fd, ua, 5);
  151.     printf("%d bytes written\n",res);
  152.  
  153.  
  154.     tcsetattr(fd,TCSANOW,&oldtio);
  155.     close(fd);
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment