Advertisement
brickmasterj

C Code for controlling Arduino via Serial Linux

Apr 4th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <termios.h>
  2. #include <fcntl.h>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <cstring>
  7. #include <unistd.h>
  8.  
  9. int main(int argc, char ** argv)
  10. {
  11.     struct termios tio;
  12.     memset(&tio, 0, sizeof(tio));
  13.  
  14.     tio.c_cflag = CS8 | CREAD | CLOCAL;
  15.     tio.c_cc[VMIN] = 1;
  16.     tio.c_cc[VTIME] = 5;
  17.  
  18.     int fd = open("/dev/ttyACM0", O_RDONLY);
  19.  
  20.     cfsetospeed(&tio, B9600);
  21.     cfsetispeed(&tio, B9600);
  22.     tcsetattr(fd, TCSANOW, &tio);
  23.  
  24.     while (1) {
  25.         unsigned char byte;
  26.         read(fd, &byte, 1);
  27.         std::cout << byte;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement