IvoSilva

writenoncanonical.c

Oct 3rd, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.61 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. #include <string.h>
  9.  
  10. #define BAUDRATE B38400
  11. #define MODEMDEVICE "/dev/ttyS0"
  12. #define _POSIX_SOURCE 1 /* POSIX compliant source */
  13. #define FALSE 0
  14. #define TRUE 1
  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.  // CREATING TRAMA
  25.     unsigned char setup[5];
  26.     setup[0]=F;
  27.     setup[1]=A;
  28.     setup[2]=S;
  29.     setup[3]=setup[1]^setup[2];
  30.     setup[4]=F;
  31.  
  32.  
  33.     int fd,c, res;
  34.     struct termios oldtio,newtio;
  35.     int i=0, sum = 0, speed = 0;
  36.    
  37.     /*if ( (argc < 2) ||
  38.          ((strcmp("/dev/ttyS0", argv[1])!=0) &&
  39.           (strcmp("/dev/ttyS1", argv[1])!=0) )) {
  40.       printf("Usage:\tnserial SerialPort\n\tex: nserial /dev/ttyS1\n");
  41.       exit(1);
  42.     }/*
  43.  
  44.  
  45.   /*
  46.     Open serial port device for reading and writing and not as controlling tty
  47.     because we don't want to get killed if linenoise sends CTRL-C.
  48.   */
  49.  
  50.  
  51.     fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
  52.     if (fd <0) {perror(argv[1]); exit(-1); }
  53.  
  54.     if ( tcgetattr(fd,&oldtio) == -1) { /* save current port settings */
  55.       perror("tcgetattr");
  56.       exit(-1);
  57.     }
  58.  
  59.     bzero(&newtio, sizeof(newtio));
  60.     newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
  61.     newtio.c_iflag = IGNPAR;
  62.     newtio.c_oflag = 0;
  63.  
  64.     /* set input mode (non-canonical, no echo,...) */
  65.     newtio.c_lflag = 0;
  66.  
  67.     newtio.c_cc[VTIME]    = 3;   /* inter-character timer unused */
  68.     newtio.c_cc[VMIN]     = 1;   /* blocking read until 5 chars received */
  69.    
  70.  
  71.  
  72.  
  73.   /*
  74.     VTIME e VMIN devem ser alterados de forma a proteger com um temporizador a
  75.     leitura do(s) próximo(s) caracter(es)
  76.  
  77.   */
  78.  
  79.  
  80.  
  81.     tcflush(fd, TCIOFLUSH);
  82.  
  83.     if ( tcsetattr(fd,TCSANOW,&newtio) == -1) {
  84.       perror("tcsetattr");
  85.       exit(-1);
  86.     }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.   /*
  93.     O ciclo FOR e as instruções seguintes devem ser alterados de modo a respeitar
  94.     o indicado no guião
  95.   */
  96.  
  97.  
  98.  
  99.  
  100.     printf("New termios structure set(Receiving Message)\n");
  101.  
  102.     char buf[5];
  103.     char buf2[5];
  104.     int aux=0;
  105.     int counter=0;
  106.     int sendAgain=1;
  107.  
  108.    
  109.     for(counter ; counter < 3; counter++){
  110.             aux=0;
  111.         printf("Sending Data\n");
  112.             res = write(fd,setup,5);
  113.         while (STOP==FALSE )
  114.             {      
  115.                   res = read(fd,buf,1);
  116.               if(!res) break;  
  117.                   printf("%x - %d\n", buf[0], res);
  118.                   buf2[aux] = buf[0];
  119.                   aux++;
  120.                     switch (aux)
  121.                     {
  122.                             case 1:
  123.                                     if (buf[0] != F)
  124.                                     {
  125.                                             STOP = TRUE;
  126.                                             printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
  127.                                     }
  128.                                     break;
  129.                             case 2:
  130.                                     if (buf[0] != A)
  131.                                     {
  132.                                             STOP = TRUE;
  133.                                             printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
  134.                                     }
  135.                                     break;
  136.                             case 3:
  137.                                     if (buf[0] != U)
  138.                                     {
  139.                                             STOP = TRUE;
  140.                                             printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
  141.                                     }
  142.                                     break;
  143.                             case 4:
  144.                                     if (buf[0] != (A^U))
  145.                                     {
  146.                                             STOP = TRUE;
  147.                                             printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
  148.                                     }
  149.                                     break;
  150.                             case 5:
  151.                                     if (buf[0] != F)
  152.                                     {
  153.                                             STOP = TRUE;
  154.                                             printf ("Error on position %d, value equals: %x\n", aux, buf[0]);
  155.                                     }
  156.                                     break;
  157.                     }
  158.                                    
  159.                   if (buf[0]=='\0' || aux == 5){
  160.                 STOP=TRUE;
  161.                             break;
  162.                           }
  163.             }
  164.         if(aux==5) break;
  165.     }
  166.  
  167.  
  168.  
  169.    
  170.     if ( tcsetattr(fd,TCSANOW,&oldtio) == -1) {
  171.       perror("tcsetattr");
  172.       exit(-1);
  173.     }
  174.     close(fd);
  175.     return 0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment