You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

205 lines
4.8 KiB

  1. /*
  2. * serial.c:
  3. * Handle a serial port
  4. ***********************************************************************
  5. * This file is part of wiringPi:
  6. * https://projects.drogon.net/raspberry-pi/wiringpi/
  7. *
  8. * wiringPi is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * wiringPi is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  20. ***********************************************************************
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include <termios.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include "serial.h"
  34. /*
  35. * serialOpen:
  36. * Open and initialise the serial port, setting all the right
  37. * port parameters - or as many as are required - hopefully!
  38. *********************************************************************************
  39. */
  40. int serialOpen (char *device, int baud)
  41. {
  42. struct termios options ;
  43. speed_t myBaud ;
  44. int status, fd ;
  45. #ifdef DEBUG
  46. printf ("openSerialPort: <%s> baud: $d\n", device, baud) ;
  47. #endif
  48. switch (baud)
  49. {
  50. case 50: myBaud = B50 ; break ;
  51. case 75: myBaud = B75 ; break ;
  52. case 110: myBaud = B110 ; break ;
  53. case 134: myBaud = B134 ; break ;
  54. case 150: myBaud = B150 ; break ;
  55. case 200: myBaud = B200 ; break ;
  56. case 300: myBaud = B300 ; break ;
  57. case 600: myBaud = B600 ; break ;
  58. case 1200: myBaud = B1200 ; break ;
  59. case 1800: myBaud = B1800 ; break ;
  60. case 2400: myBaud = B2400 ; break ;
  61. case 9600: myBaud = B9600 ; break ;
  62. case 19200: myBaud = B19200 ; break ;
  63. case 38400: myBaud = B38400 ; break ;
  64. case 57600: myBaud = B57600 ; break ;
  65. case 115200: myBaud = B115200 ; break ;
  66. case 230400: myBaud = B230400 ; break ;
  67. default:
  68. return -2 ;
  69. }
  70. if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
  71. return -1 ;
  72. fcntl (fd, F_SETFL, O_RDWR) ;
  73. // Get and modify current options:
  74. tcgetattr (fd, &options) ;
  75. cfmakeraw (&options) ;
  76. cfsetispeed (&options, myBaud) ;
  77. cfsetospeed (&options, myBaud) ;
  78. options.c_cflag |= (CLOCAL | CREAD) ;
  79. options.c_cflag &= ~PARENB ;
  80. options.c_cflag &= ~CSTOPB ;
  81. options.c_cflag &= ~CSIZE ;
  82. options.c_cflag |= CS8 ;
  83. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
  84. options.c_oflag &= ~OPOST ;
  85. options.c_cc [VMIN] = 0 ;
  86. options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
  87. tcsetattr (fd, TCSANOW, &options) ;
  88. ioctl (fd, TIOCMGET, &status);
  89. status |= TIOCM_DTR ;
  90. status |= TIOCM_RTS ;
  91. ioctl (fd, TIOCMSET, &status);
  92. usleep (10000) ; // 10mS
  93. return fd ;
  94. }
  95. /*
  96. * serialClose:
  97. * Release the serial port
  98. *********************************************************************************
  99. */
  100. void serialClose (int fd)
  101. {
  102. close (fd) ;
  103. }
  104. /*
  105. * serialPutchar:
  106. * Send a single character to the serial port
  107. *********************************************************************************
  108. */
  109. void serialPutchar (int fd, uint8_t c)
  110. {
  111. write (fd, &c, 1) ;
  112. }
  113. /*
  114. * serialPuts:
  115. * Send a string to the serial port
  116. *********************************************************************************
  117. */
  118. void serialPuts (int fd, char *s)
  119. {
  120. write (fd, s, strlen (s)) ;
  121. }
  122. /*
  123. * serialPrintf:
  124. * Printf over Serial
  125. *********************************************************************************
  126. */
  127. void serialPrintf (int fd, char *message, ...)
  128. {
  129. va_list argp ;
  130. char buffer [1024] ;
  131. va_start (argp, message) ;
  132. vsnprintf (buffer, 1023, message, argp) ;
  133. va_end (argp) ;
  134. serialPuts (fd, buffer) ;
  135. }
  136. /*
  137. * serialDataAvail:
  138. * Return the number of bytes of data avalable to be read in the serial port
  139. *********************************************************************************
  140. */
  141. int serialDataAvail (int fd)
  142. {
  143. int result ;
  144. if (ioctl (fd, FIONREAD, &result) == -1)
  145. return -1 ;
  146. return result ;
  147. }
  148. /*
  149. * serialGetchar:
  150. * Get a single character from the serial device.
  151. * Note: Zero is a valid character and this function will time-out after
  152. * 10 seconds.
  153. *********************************************************************************
  154. */
  155. int serialGetchar (int fd)
  156. {
  157. uint8_t x ;
  158. if (read (fd, &x, 1) != 1)
  159. return -1 ;
  160. return ((int)x) & 0xFF ;
  161. }