Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

wiringSerial.c 5.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * wiringSerial.c:
  3. * Handle a serial port
  4. ***********************************************************************
  5. * This file is part of wiringPi:
  6. * https://github.com/WiringPi/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. #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 <sys/ioctl.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include "wiringSerial.h"
  34. /*
  35. * serialOpen:
  36. * Open and initialise the serial port, setting port parameters
  37. *********************************************************************************
  38. */
  39. int serialOpen (const char *device, const int baud)
  40. {
  41. struct termios options;
  42. speed_t myBaud;
  43. int status, fd;
  44. int regularFile = 0;
  45. switch (baud)
  46. {
  47. case 50: myBaud = B50; break;
  48. case 75: myBaud = B75; break;
  49. case 110: myBaud = B110; break;
  50. case 134: myBaud = B134; break;
  51. case 150: myBaud = B150; break;
  52. case 200: myBaud = B200; break;
  53. case 300: myBaud = B300; break;
  54. case 600: myBaud = B600; break;
  55. case 1200: myBaud = B1200; break;
  56. case 1800: myBaud = B1800; break;
  57. case 2400: myBaud = B2400; break;
  58. case 4800: myBaud = B4800; break;
  59. case 9600: myBaud = B9600; break;
  60. case 19200: myBaud = B19200; break;
  61. case 38400: myBaud = B38400; break;
  62. case 57600: myBaud = B57600; break;
  63. case 115200: myBaud = B115200; break;
  64. case 230400: myBaud = B230400; break;
  65. case 460800: myBaud = B460800; break;
  66. case 500000: myBaud = B500000; break;
  67. case 576000: myBaud = B576000; break;
  68. case 921600: myBaud = B921600; break;
  69. case 1000000: myBaud = B1000000; break;
  70. case 1152000: myBaud = B1152000; break;
  71. case 1500000: myBaud = B1500000; break;
  72. case 2000000: myBaud = B2000000; break;
  73. case 2500000: myBaud = B2500000; break;
  74. case 3000000: myBaud = B3000000; break;
  75. case 3500000: myBaud = B3500000; break;
  76. case 4000000: myBaud = B4000000; break;
  77. // Undocumented: If baud is -1, treat device as a regular file!
  78. case -1: myBaud = B115200; regularFile = 1; break;
  79. default:
  80. return -2;
  81. }
  82. if (!regularFile)
  83. {
  84. if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
  85. return -1;
  86. fcntl (fd, F_SETFL, O_RDWR);
  87. // Get and modify current options:
  88. tcgetattr (fd, &options);
  89. cfmakeraw (&options);
  90. cfsetispeed (&options, myBaud);
  91. cfsetospeed (&options, myBaud);
  92. options.c_cflag |= (CLOCAL | CREAD);
  93. options.c_cflag &= ~PARENB;
  94. options.c_cflag &= ~CSTOPB;
  95. options.c_cflag &= ~CSIZE;
  96. options.c_cflag |= CS8;
  97. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  98. options.c_oflag &= ~OPOST;
  99. options.c_cc [VMIN] = 0;
  100. options.c_cc [VTIME] = 100; // Ten seconds (100 deciseconds)
  101. tcsetattr (fd, TCSANOW, &options);
  102. ioctl (fd, TIOCMGET, &status);
  103. status |= TIOCM_DTR;
  104. status |= TIOCM_RTS;
  105. ioctl (fd, TIOCMSET, &status);
  106. usleep (10000); // 10mS
  107. }
  108. else
  109. {
  110. // Want to open a regular file
  111. if ((fd = open (device, O_RDWR | O_CREAT, 0666)) == -1)
  112. return -1;
  113. }
  114. return fd;
  115. }
  116. /*
  117. * serialFlush:
  118. * Flush the serial buffers (both tx & rx)
  119. *********************************************************************************
  120. */
  121. void serialFlush (const int fd)
  122. {
  123. tcflush (fd, TCIOFLUSH);
  124. }
  125. /*
  126. * serialClose:
  127. * Release the serial port
  128. *********************************************************************************
  129. */
  130. void serialClose (const int fd)
  131. {
  132. close (fd);
  133. }
  134. /*
  135. * serialPutchar:
  136. * Send a single character to the serial port
  137. *********************************************************************************
  138. */
  139. void serialPutchar (const int fd, const unsigned char c)
  140. {
  141. write (fd, &c, 1);
  142. }
  143. /*
  144. * serialPuts:
  145. * Send a string to the serial port
  146. *********************************************************************************
  147. */
  148. void serialPuts (const int fd, const char *s)
  149. {
  150. write (fd, s, strlen (s));
  151. }
  152. /*
  153. * serialPrintf:
  154. * Printf over Serial
  155. *********************************************************************************
  156. */
  157. void serialPrintf (const int fd, const char *message, ...)
  158. {
  159. va_list argp;
  160. char buffer [1024];
  161. va_start (argp, message);
  162. vsnprintf (buffer, 1023, message, argp);
  163. va_end (argp);
  164. serialPuts (fd, buffer);
  165. }
  166. /*
  167. * serialDataAvail:
  168. * Return the number of bytes of data avalable to be read in the serial port
  169. *********************************************************************************
  170. */
  171. int serialDataAvail (const int fd)
  172. {
  173. int result;
  174. if (ioctl (fd, FIONREAD, &result) == -1)
  175. return -1;
  176. return result;
  177. }
  178. /*
  179. * serialGetchar:
  180. * Get a single character from the serial device.
  181. * Note: Zero is a valid character and this function will time-out after
  182. * 10 seconds.
  183. *********************************************************************************
  184. */
  185. int serialGetchar (const int fd)
  186. {
  187. uint8_t x;
  188. if (read (fd, &x, 1) != 1)
  189. return -1;
  190. return ((int)x) & 0xFF;
  191. }