Browse Source

lowl-vcom: vcom_set_timeout function

Luke Dashjr 11 years ago
parent
commit
4d781041ed
2 changed files with 18 additions and 0 deletions
  1. 17 0
      lowl-vcom.c
  2. 1 0
      lowl-vcom.h

+ 17 - 0
lowl-vcom.c

@@ -936,6 +936,23 @@ bool valid_baud(int baud)
 	}
 	}
 }
 }
 
 
+bool vcom_set_timeout(const int fdDev, const uint8_t timeout)
+{
+#ifdef WIN32
+	const HANDLE hSerial = (HANDLE)_get_osfhandle(fdDev);
+	// Code must specify a valid timeout value (0 means don't timeout)
+	const DWORD ctoms = ((DWORD)timeout * 100);
+	COMMTIMEOUTS cto = {ctoms, 0, ctoms, 0, ctoms};
+	SetCommTimeouts(hSerial, &cto);
+#else
+	struct termios my_termios;
+	
+	tcgetattr(fdDev, &my_termios);
+	my_termios.c_cc[VTIME] = (cc_t)timeout;
+	tcsetattr(fdDev, TCSANOW, &my_termios);
+#endif
+}
+
 /* NOTE: Linux only supports uint8_t (decisecond) timeouts; limiting it in
 /* NOTE: Linux only supports uint8_t (decisecond) timeouts; limiting it in
  *       this interface buys us warnings when bad constants are passed in.
  *       this interface buys us warnings when bad constants are passed in.
  */
  */

+ 1 - 0
lowl-vcom.h

@@ -39,6 +39,7 @@ extern ssize_t _serial_read(int fd, char *buf, size_t buflen, char *eol);
 	_serial_read(fd, buf, bufsiz, &eol)
 	_serial_read(fd, buf, bufsiz, &eol)
 extern int serial_close(int fd);
 extern int serial_close(int fd);
 
 
+extern bool vcom_set_timeout(int fd, uint8_t timeout);
 extern enum bfg_gpio_value get_serial_cts(int fd);
 extern enum bfg_gpio_value get_serial_cts(int fd);
 extern enum bfg_gpio_value set_serial_dtr(int fd, enum bfg_gpio_value dtr);
 extern enum bfg_gpio_value set_serial_dtr(int fd, enum bfg_gpio_value dtr);
 extern enum bfg_gpio_value set_serial_rts(int fd, enum bfg_gpio_value rts);
 extern enum bfg_gpio_value set_serial_rts(int fd, enum bfg_gpio_value rts);