serial.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * (C) Copyright 2000
  3. * Murray Jensen <Murray.Jensen@csiro.au>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program 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 this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <fcntl.h>
  26. #include <sys/time.h>
  27. #include "serial.h"
  28. #if defined(__sun__) || \
  29. defined(__OpenBSD__) || \
  30. defined(__FreeBSD__) || \
  31. defined(__NetBSD__) || \
  32. defined(__APPLE__)
  33. static struct termios tios = { BRKINT, 0, B115200|CS8|CREAD, 0, { 0 } };
  34. #else
  35. static struct termios tios = { BRKINT, 0, B115200|CS8|CREAD, 0, 0 };
  36. #endif
  37. static struct speedmap {
  38. char *str;
  39. speed_t val;
  40. } speedmap[] = {
  41. { "50", B50 }, { "75", B75 }, { "110", B110 },
  42. { "134", B134 }, { "150", B150 }, { "200", B200 },
  43. { "300", B300 }, { "600", B600 }, { "1200", B1200 },
  44. { "1800", B1800 }, { "2400", B2400 }, { "4800", B4800 },
  45. { "9600", B9600 }, { "19200", B19200 }, { "38400", B38400 },
  46. { "57600", B57600 },
  47. #ifdef B76800
  48. { "76800", B76800 },
  49. #endif
  50. { "115200", B115200 },
  51. #ifdef B153600
  52. { "153600", B153600 },
  53. #endif
  54. { "230400", B230400 },
  55. #ifdef B307200
  56. { "307200", B307200 },
  57. #endif
  58. #ifdef B460800
  59. { "460800", B460800 }
  60. #endif
  61. };
  62. static int nspeeds = sizeof speedmap / sizeof speedmap[0];
  63. speed_t
  64. cvtspeed(char *str)
  65. {
  66. struct speedmap *smp = speedmap, *esmp = &speedmap[nspeeds];
  67. while (smp < esmp) {
  68. if (strcmp(str, smp->str) == 0)
  69. return (smp->val);
  70. smp++;
  71. }
  72. return B0;
  73. }
  74. int
  75. serialopen(char *device, speed_t speed)
  76. {
  77. int fd;
  78. if (cfsetospeed(&tios, speed) < 0)
  79. return -1;
  80. if ((fd = open(device, O_RDWR)) < 0)
  81. return -1;
  82. if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
  83. (void)close(fd);
  84. return -1;
  85. }
  86. return fd;
  87. }
  88. int
  89. serialreadchar(int fd, int timeout)
  90. {
  91. fd_set fds;
  92. struct timeval tv;
  93. int n;
  94. char ch;
  95. tv.tv_sec = timeout;
  96. tv.tv_usec = 0;
  97. FD_ZERO(&fds);
  98. FD_SET(fd, &fds);
  99. /* this is a fucking horrible quick hack - fix this */
  100. if ((n = select(fd + 1, &fds, 0, 0, &tv)) < 0)
  101. return SERIAL_ERROR;
  102. if (n == 0)
  103. return SERIAL_TIMEOUT;
  104. if ((n = read(fd, &ch, 1)) < 0)
  105. return SERIAL_ERROR;
  106. if (n == 0)
  107. return SERIAL_EOF;
  108. return ch;
  109. }
  110. int
  111. serialwrite(int fd, char *buf, int len)
  112. {
  113. int n;
  114. do {
  115. n = write(fd, buf, len);
  116. if (n < 0)
  117. return 1;
  118. len -= n;
  119. buf += n;
  120. } while (len > 0);
  121. return 0;
  122. }
  123. int
  124. serialclose(int fd)
  125. {
  126. return close(fd);
  127. }