wiringSerial.c 4.8 KB

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