Advertisement
Guest User

C Terminal

a guest
Nov 6th, 2010
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <termios.h>
  8. #include <signal.h>
  9. #include <unistd.h>
  10. #include <iostream>
  11. #include <signal.h>
  12.  
  13.  
  14. int open_port(void);
  15.  
  16. int main()
  17. {
  18.     char dato[1];
  19.     int fd = 0;
  20.     fd = open_port();
  21.     while(1)
  22.     {
  23.         read(fd,dato,1);
  24.         //~ if(dato == "B")
  25.         //~ return 0;
  26.         printf(dato);
  27.     }
  28. }
  29.  
  30. int open_port(void)
  31. {
  32.     int fd; /* File descriptor for the port */ 
  33.    
  34.     //~ fd = open("/home/tomas/ttySV1", O_RDWR | O_NOCTTY | O_NDELAY);
  35.     fd = open("/dev/ttyUSB0", O_RDWR | O_NDELAY);
  36.     //~ fd = open("/dev/ttyUSB0", O_RDWR);
  37.  
  38.     if (fd == -1)
  39.     {
  40.         perror("open_port: No se pudo abrir el puerto: ");
  41.     }
  42.     else
  43.     {
  44.         struct termios options;
  45.    
  46.         /*
  47.          * Get the current options for the port...
  48.          */
  49.    
  50.         tcgetattr(fd, &options);
  51.    
  52.         /*
  53.          * Set the baud rates to B9600...
  54.          */
  55.    
  56.         cfsetispeed(&options, B9600);
  57.         cfsetispeed(&options, B9600);
  58.    
  59.         /*
  60.          * Enable the receiver and set local mode...
  61.          */
  62.    
  63.         options.c_cflag |= (CLOCAL | CREAD);
  64.    
  65.         /*
  66.          * Set the new options for the port...
  67.          */
  68.    
  69.         tcsetattr(fd, TCSANOW, &options);
  70.        
  71.         options.c_cflag &= ~CSIZE; /* Mask the character size bits */
  72.         options.c_cflag |= CS8;    /* Select 8 data bits */
  73.        
  74.         options.c_cflag &= ~PARENB;
  75.         options.c_cflag &= ~CSTOPB;
  76.         options.c_cflag &= ~CSIZE;
  77.         options.c_cflag |= CS8;
  78.  
  79.         //~ fcntl(fd, F_SETFL, 0);
  80.         return (fd);
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement