Advertisement
milanmetal

[Linux-C] Read serial port data from Arduino

Jan 25th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <string.h>
  4. #include <termios.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7.  
  8. /*
  9.  * Based on this answer:
  10.  * https://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c
  11. */
  12.  
  13. int
  14. set_interface_attribs (int fd, int speed, int parity)
  15. {
  16.         struct termios tty;
  17.         memset (&tty, 0, sizeof tty);
  18.         if (tcgetattr (fd, &tty) != 0)
  19.         {
  20.                 printf("error %d from tcgetattr", errno);
  21.                 return -1;
  22.         }
  23.  
  24.         cfsetospeed (&tty, speed);
  25.         cfsetispeed (&tty, speed);
  26.  
  27.         tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
  28.         // disable IGNBRK for mismatched speed tests; otherwise receive break
  29.         // as \000 chars
  30.         tty.c_iflag &= ~IGNBRK;         // disable break processing
  31.         tty.c_lflag = 0;                // no signaling chars, no echo,
  32.                                         // no canonical processing
  33.         tty.c_oflag = 0;                // no remapping, no delays
  34.         tty.c_cc[VMIN]  = 0;            // read doesn't block
  35.         tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
  36.  
  37.         tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  38.  
  39.         tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
  40.                                         // enable reading
  41.         tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
  42.         tty.c_cflag |= parity;
  43.         tty.c_cflag &= ~CSTOPB;
  44.         tty.c_cflag &= ~CRTSCTS;
  45.  
  46.         if (tcsetattr (fd, TCSANOW, &tty) != 0)
  47.         {
  48.                 printf("error %d from tcsetattr", errno);
  49.                 return -1;
  50.         }
  51.         return 0;
  52. }
  53.  
  54. void
  55. set_blocking (int fd, int should_block)
  56. {
  57.         struct termios tty;
  58.         memset (&tty, 0, sizeof tty);
  59.         if (tcgetattr (fd, &tty) != 0)
  60.         {
  61.                 printf("error %d from tggetattr", errno);
  62.                 return;
  63.         }
  64.  
  65.         tty.c_cc[VMIN]  = should_block ? 1 : 0;
  66.         tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
  67.  
  68.         if (tcsetattr (fd, TCSANOW, &tty) != 0) {
  69.                 printf("error %d setting term attributes", errno);
  70.         }
  71. }
  72.  
  73.  
  74. int main(int argc, char *argv[]) {
  75.  
  76.   char *portname = "/dev/ttyACM0";
  77.  
  78.   int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
  79.  
  80.   if (fd < 0)
  81.   {
  82.     printf("error %d opening %s: %s", errno, portname, strerror (errno));
  83.     return 0;
  84.   }
  85.  
  86.   set_interface_attribs (fd, B9600, 0);  // set speed to 115,200 bps, 8n1 (no parity)
  87.   set_blocking (fd, 0);                // set no blocking
  88.  
  89.   write (fd, "hello!\n", 7);           // send 7 character greeting
  90.  
  91.   usleep ((7 + 25) * 100);             // sleep enough to transmit the 7 plus
  92.                                        // receive 25:  approx 100 uS per char transmit
  93.   char buf [100];
  94.   int n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read
  95.  
  96.   // This will print characters read from serial port.
  97.   for(int i = 0; i < n; i++) {
  98.     printf("Char %d. is: %c \n", i, buf[i]);
  99.   }
  100.   //while(1){
  101.   //  printf("");
  102.   //}
  103.  
  104.   return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement