Advertisement
Guest User

ttyACM problem

a guest
Jun 27th, 2014
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <sys/ioctl.h>
  13. #include <iostream>
  14. #include <sys/poll.h>
  15. #include <vector>
  16.  
  17.  
  18. int open(const char *device)
  19. {
  20.     tcflag_t bitrate = B115200;
  21.     int fd = ::open( device, O_RDWR | O_NOCTTY );
  22.     if ( fd < 0 )
  23.     {
  24.         fprintf( stderr, "Failed to open serial device '%s' (errno: %s)\n", device, strerror(errno) );
  25.         return -1;
  26.     }
  27.     if (::ioctl(fd, TIOCEXCL))
  28.     {
  29.         fprintf( stderr, "Failed to lock serial device '%s' (errno: %s)\n", device, strerror(errno) );
  30.         return -1;
  31.     }
  32.  
  33.  
  34.  
  35.     // Check if device is a terminal device
  36.     if ( !isatty( fd ) )
  37.     {
  38.         fprintf( stderr, "Device '%s' is not a terminal device (errno: %s)!\n", device, strerror(errno) );
  39.         ::close( fd );
  40.         return -1;
  41.     }
  42.  
  43.     struct termios settings;
  44.     // Set input flags
  45.     settings.c_iflag =  IGNBRK          // Ignore BREAKS on Input
  46.                      |  IGNPAR;         // No Parity
  47.                                         // ICRNL: map CR to NL (otherwise a CR input on the other computer will not terminate input)
  48.  
  49.     // Set output flags
  50.     settings.c_oflag = 0;               // Raw output
  51.  
  52.     // Set controlflags
  53.     settings.c_cflag = bitrate
  54.                      | CS8              // 8 bits per byte
  55.                      | CSTOPB           // Stop bit
  56.                      | CREAD            // characters may be read
  57.                      | CLOCAL;          // ignore modem state, local connection
  58.  
  59.     // Set local flags
  60.     settings.c_lflag = 0;               // Other option: ICANON = enable canonical input
  61.  
  62.     // non-canonical mode: set device to non blocking mode:
  63.     // Set timer
  64.     settings.c_cc[VTIME] = 0;           // 0 means timer is not used
  65.  
  66.     // Set minimum bytes to read
  67.     settings.c_cc[VMIN]  = 0;           // 1 means wait until at least 1 character is received; 0 means don't wait
  68.  
  69.     // Now clean the modem line and activate the settings for the port
  70.     tcflush( fd, TCIFLUSH );
  71.     tcsetattr( fd, TCSANOW, &settings );
  72.     return fd;
  73. }
  74.  
  75. int readAll(int fd)
  76. {
  77.     unsigned char buf[256];
  78.     int res;
  79.     int count = 0;
  80.     while(1)
  81.     {
  82.         res = read( fd, &buf, 100 );
  83.         if (res == 0) return count;
  84.         if (res < 0) return -1;
  85.         count += res;
  86.         //printf("res = %d\n", res);
  87.     }
  88. }
  89.  
  90. void readSleepLoop(int fd1, int fd2)
  91. {
  92.     printf("start readSleepLoop\n");
  93.    
  94.     for(int n = 0; n < 10000; n++)
  95.     {
  96.         readAll(fd1);
  97.         readAll(fd2);
  98.         usleep(1);
  99.     }
  100. }
  101.  
  102. void readPollLoop(int fd)
  103. {
  104.     struct pollfd fds[1];
  105.     fds[0].fd = fd;
  106.     fds[0].events = POLLIN ;
  107.     while(1)
  108.     {
  109.         int pollrc = poll( fds, 1, 1000);
  110.         if (pollrc < 0)
  111.         {
  112.             perror("poll");
  113.         }
  114.         else if( pollrc > 0)
  115.         {
  116.             if( fds[0].revents & POLLIN )
  117.             {
  118.                 unsigned char buff[256];
  119.                 int res = read(fd, buff, sizeof(buff) );
  120.                 if (res > 0)
  121.                 {
  122.                     //printf("res = %d\n", res);
  123.                 }
  124.             }
  125.         }
  126.     }
  127. }
  128.  
  129. void readSelectLoop(int fd)
  130. {
  131.     fd_set rfds;
  132.     struct timeval tv;
  133.     int retval;
  134.     printf("starting readSelectLoop\n");
  135.     while(1)
  136.     {
  137.         tv.tv_sec = 1;
  138.         tv.tv_usec = 0;
  139.         FD_ZERO(&rfds);
  140.         FD_SET(fd, &rfds);
  141.         retval = select(fd + 1, &rfds, NULL, NULL, &tv);
  142.         //printf("retval = %d\n", retval);
  143.         if (retval < 0)
  144.         {
  145.             perror("select()");
  146.         }
  147.         else if (retval > 0)
  148.         {
  149.             unsigned char buff[256];
  150.             int res = read(fd, buff, sizeof(buff) );
  151.             if (res > 0)
  152.             {
  153.                 //printf("res = %d\n", res);
  154.             }
  155.         }
  156.     }
  157. }
  158.  
  159. int main(int argc, char* argv[])
  160. {
  161.     int fd1 = open("/dev/ttyACM0");
  162.     int fd2 = open("/dev/ttyACM1");
  163.     if(fd1 < 0 || fd2 < 0)
  164.     {
  165.         return -1;
  166.     }
  167.     //readSleepLoop(fd1, fd2);
  168.     readPollLoop(fd1);
  169.     //readSelectLoop(fd1);
  170.     close(fd1);
  171.     close(fd2);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement