Advertisement
AntonioVillanueva

ttyACM0 terminal .... Arduino

Mar 4th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <termios.h>
  6. #include <string.h> // needed for memset
  7. #define BPS B9600 //B115200
  8. #define PORTNAME "/dev/ttyACM0"
  9.  
  10. int main(int argc,char** argv)
  11. {
  12.         struct termios tio;
  13.         struct termios stdio;
  14.         int tty_fd;
  15.  
  16.         unsigned char c='D';
  17.  
  18.         memset(&stdio,0,sizeof(stdio));
  19.         stdio.c_iflag=0;
  20.         stdio.c_oflag=0;
  21.         stdio.c_cflag=0;
  22.         stdio.c_lflag=0;
  23.         stdio.c_cc[VMIN]=1;
  24.         stdio.c_cc[VTIME]=0;
  25.         tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
  26.         tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
  27.         fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);       // make the reads non-blocking
  28.  
  29.         memset(&tio,0,sizeof(tio));
  30.         tio.c_iflag=0;
  31.         tio.c_oflag=0;
  32.         tio.c_cflag=CS8|CREAD|CLOCAL;           // 8N1
  33.         tio.c_lflag=0;
  34.         tio.c_cc[VMIN]=1;
  35.         tio.c_cc[VTIME]=5;
  36.  
  37.         tty_fd=open(argc==1 ? PORTNAME :argv[1], O_RDWR | O_NONBLOCK);        // O_NONBLOCK might override VMIN and VTIME, so read() may return immediately.
  38.         cfsetospeed(&tio,BPS);            // 9600
  39.         cfsetispeed(&tio,BPS);      
  40.  
  41.         tcsetattr(tty_fd,TCSANOW,&tio);
  42.         while (c!='q')
  43.         {
  44.                 if (read(tty_fd,&c,1)>0)        write(STDOUT_FILENO,&c,1);              // if new data is available on the serial port, print it out
  45.                 if (read(STDIN_FILENO,&c,1)>0)  write(tty_fd,&c,1);                     // if new data is available on the console, send it to the serial port
  46.         }
  47.  
  48.         close(tty_fd);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement