Guest User

Untitled

a guest
Jul 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 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. /*
  9. * 'open_port()' - Open serial port 1.
  10. *
  11. * Returns the file descriptor on success or -1 on error.
  12. */
  13.  
  14. int main()
  15. {
  16. int fd; // File descriptor
  17. int n;
  18.  
  19. char buf;
  20. fd = open_port();
  21.  
  22. // Read the configureation of the port
  23.  
  24. struct termios options;
  25. tcgetattr( fd, &options );
  26.  
  27. /* SEt Baud Rate */
  28.  
  29. //cfsetispeed( &options, 2764800 );
  30. //cfsetospeed( &options, 2764800 );
  31. cfsetispeed( &options, B9600 );
  32. cfsetospeed( &options, B9600 );
  33. //I don't know what this is exactly
  34.  
  35.  
  36.  
  37. options.c_cflag |= ( CLOCAL | CREAD );
  38.  
  39. // Set the Charactor size
  40.  
  41. options.c_cflag &= ~CSIZE; /* Mask the character size bits */
  42. options.c_cflag |= CS8; /* Select 8 data bits */
  43.  
  44. // Set parity - No Parity (8N1)
  45.  
  46. options.c_cflag &= ~PARENB;
  47. options.c_cflag &= ~CSTOPB;
  48. options.c_cflag &= ~CSIZE;
  49. options.c_cflag |= CS8;
  50.  
  51. //options.c_lflag |= CRTSCTS;
  52. // Disable Hardware flowcontrol
  53.  
  54. // options.c_cflag &= ~CNEW_RTSCTS; -- not supported
  55.  
  56. // Enable Raw Input
  57.  
  58. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  59.  
  60. // Disable Software Flow control
  61.  
  62. options.c_iflag &= ~(IXON | IXOFF | IXANY);
  63.  
  64. // Chose raw (not processed) output
  65.  
  66. options.c_oflag &= ~OPOST;
  67.  
  68. /* set input mode (non-canonical, no echo,...) */
  69. //options.c_lflag = 0;
  70.  
  71. options.c_cc[VTIME] = 0; /* inter-character timer unused */
  72.  
  73. options.c_cc[VMIN] = 1; /* blocking read until 1 chars received */
  74.  
  75.  
  76. if ( tcsetattr( fd, TCSANOW, &options ) == -1 )
  77. printf ("Error with tcsetattr = %sn", strerror ( errno ) );
  78. else
  79. printf ( "%sn", "tcsetattr succeed" );
  80.  
  81. fcntl(fd, F_SETFL, FNDELAY);
  82.  
  83.  
  84.  
  85. // Write some stuff !!!
  86.  
  87. n = write(fd, "ATZATZr", 7);
  88. if (n < 0)
  89. fputs("write() of 7 bytes failed!n", stderr);
  90. else
  91. printf ("Write succeed n = %in", n );
  92.  
  93. /*for ( n = 0 ; n < 1000 ; n++)
  94. {
  95. n++;
  96. n--;
  97. }
  98. */
  99.  
  100. sleep(12);
  101.  
  102.  
  103. n = read( fd, &buf, 1 );
  104.  
  105. if ( n == -1 )
  106. printf ( "Error = %sn", strerror( errno ) );
  107.  
  108. printf ( "Number of bytes to be read = %in", n );
  109. printf ( "Buf = %cn", buf );
  110.  
  111. close( fd );
  112.  
  113. return(0);
  114.  
  115. }
  116.  
  117.  
  118.  
  119. int open_port(void)
  120. {
  121. int fd; /* File descriptor for the port */
  122.  
  123. fd = open("/dev/ttyXR0", O_RDWR | O_NOCTTY | O_NDELAY);
  124. if (fd == -1)
  125. {
  126. /*
  127. * Could not open the port.
  128. */
  129.  
  130. perror("open_port: Unable to open /dev/ttyXR0 - ");
  131. }
  132. else
  133. {
  134. fcntl(fd, F_SETFL, FNDELAY);
  135. printf("opening of device /dev/ttyXR0 succeeded !!n");
  136. }
  137.  
  138. printf ( "In Open port fd = %in", fd);
  139.  
  140. return (fd);
  141. }
Add Comment
Please, Sign In to add comment