IvoSilva

noncanonical.c v2

Oct 3rd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 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.     unsigned char buf;
  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.     printf ("Program open.\n");
  45.  
  46.     fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
  47.     if (fd <0) {perror(argv[1]); exit(-1); }
  48.  
  49.     if ( tcgetattr(fd,&oldtio) == -1) { /* save current port settings */
  50.       perror("tcgetattr");
  51.       exit(-1);
  52.     }
  53.  
  54.     bzero(&newtio, sizeof(newtio));
  55.     newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
  56.     newtio.c_iflag = IGNPAR;
  57.     newtio.c_oflag = 0;
  58.  
  59.     /* set input mode (non-canonical, no echo,...) */
  60.     newtio.c_lflag = 0;
  61.  
  62.     newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
  63.     newtio.c_cc[VMIN]     = 1;   /* blocking read until 1 chars received */
  64.  
  65.  
  66.   /*
  67.     VTIME e VMIN devem ser alterados de forma a proteger com um temporizador a
  68.     leitura do(s) próximo(s) caracter(es)
  69.   */
  70.  
  71.  
  72.  
  73.     tcflush(fd, TCIOFLUSH);
  74.  
  75.     if ( tcsetattr(fd,TCSANOW,&newtio) == -1) {
  76.       perror("tcsetattr");
  77.       exit(-1);
  78.     }
  79.    
  80.     int aux = 0, state = 0;
  81.     while (state < 5)
  82.     {
  83.     res = read (fd, &buf, 1);
  84.     switch (state)
  85.     {
  86.         case 0:
  87.             if (buf == F)
  88.             {
  89.                 SET[state] = buf;
  90.                 state++;
  91.             }
  92.             break;
  93.         case 1:
  94.             if (buf == A)
  95.             {
  96.                 SET[state] = buf;
  97.                 state++;
  98.             }
  99.             else if (buf != F && buf != A)
  100.                 state--;
  101.             break;
  102.         case 2:
  103.             if (buf == S)
  104.             {
  105.                 SET[state] = buf;
  106.                 state++;
  107.             }
  108.             else if (buf == F)
  109.                 state--;
  110.             else
  111.                 state = 0;
  112.             break;
  113.         case 3:
  114.             if (buf == (A ^ S))
  115.             {
  116.                 SET[state] = buf;
  117.                 state++;
  118.             }
  119.             else if (buf == F)
  120.                 state = 1;
  121.             else
  122.                 state = 0;
  123.             break;
  124.         case 4:
  125.             if (buf == F)
  126.             {
  127.                 SET[state] = buf;
  128.                 state++;
  129.             }
  130.             else
  131.                 state = 0;
  132.             break;
  133.     }
  134.     }
  135.  
  136.     printf ("Printing SET:\n");
  137.     int i=0;
  138.     for(i; i < state; i++)
  139.         printf("%x", SET[i]);
  140.     printf("\n\n");
  141.  
  142.     ua[0] = F;
  143.     ua[1] = A;
  144.     ua[2] = U;
  145.     ua[3] = (A ^ U);
  146.     ua[4] = F;
  147.  
  148.     res = write(fd, ua, 5);
  149.     if (!res)
  150.         printf("Mensagem não enviada.\n",res);
  151.     else
  152.         printf("%d bytes enviados.\n",res);
  153.  
  154.  
  155.     tcsetattr(fd,TCSANOW,&oldtio);
  156.     close(fd);
  157.     return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment