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.

serial-util.h 1.1 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Serial I/O helper routines
  3. *
  4. * Jim Paris <jim@jtan.com>
  5. * $Id$
  6. */
  7. #ifndef SERIAL_UTIL_H
  8. #define SERIAL_UTIL_H
  9. #include <unistd.h>
  10. /* Open serial port in raw mode, with custom baudrate if necessary */
  11. int serial_open(const char *device, int rate);
  12. /* Like read(), but restarts after EINTR, and reads until count bytes
  13. are received or a timeout occurs. */
  14. int saferead_timeout(int fd, void *buf, size_t count, int timeout_ms);
  15. static inline int saferead(int fd, void *buf, size_t count) {
  16. return saferead_timeout(fd, buf, count, -1);
  17. }
  18. /* Like write(), but restarts after EINTR */
  19. ssize_t safewrite(int fd, const void *buf, size_t count);
  20. /* Read bytes until no more are available for specified time */
  21. int drain_timeout(int fd, int msec);
  22. /* Read bytes until no more are available for 0.1 sec */
  23. static inline int drain(int fd) {
  24. return drain_timeout(fd, 100);
  25. }
  26. /* Like fprintf, but to a fd, using safewrite */
  27. int fdprintf(int fd, const char *fmt, ...);
  28. /* Like fgets, but from a fd, using saferead_timeout. */
  29. char *fdgets(char *s, int size, int fd, int timeout_ms);
  30. /* Like perl chomp. */
  31. void chomp(char *s);
  32. #endif