Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>   /* Standard input/output definitions */
  2. #include <string.h>  /* String function definitions */
  3. #include <unistd.h>  /* UNIX standard function definitions */
  4. #include <fcntl.h>   /* File control definitions */
  5. #include <errno.h>   /* Error number definitions */
  6. #include <termios.h> /* POSIX terminal control definitions */
  7.  
  8. int main (void)
  9. {
  10.     //open serial device
  11.     int fd;
  12.     fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
  13.     if (fd == -1)
  14.     {
  15.         perror("open_port: Unable to open /dev/ttyS0 - ");
  16.     }
  17.     else
  18.         fcntl(fd, F_SETFL, 0);
  19.     //Set baudrate to 19200
  20.     struct termios options;
  21.     tcgetattr(fd, &options);
  22.     cfsetispeed(&options, B19200);
  23.     cfsetospeed(&options, B19200);
  24.     options.c_cflag |= (CLOCAL | CREAD);
  25.     tcsetattr(fd, TCSANOW, &options);
  26.    
  27.     //Write text
  28.     int n;
  29.     n = write(fd, "Hello world!\r", 13);
  30.     if (n < 0)
  31.         fputs("write() failed!\n", stderr);
  32.  
  33.     n = write(fd, "Printed using the VoCore v1.0\r", 30);
  34.     if (n < 0)
  35.         fputs("write() failed!\n", stderr);
  36.  
  37.     n = write(fd, ":)\r", 3);
  38.     if (n < 0)
  39.         fputs("write() failed!\n", stderr);
  40.  
  41.     //close connection
  42.     close(fd);
  43.     return printf("End\n");
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement