From 63604ef5cc8a2f4da44f75d39a6b2363dd10eff8 Mon Sep 17 00:00:00 2001 From: Henry Bruce Date: Thu, 17 Dec 2015 15:31:48 -0800 Subject: [PATCH] uart: Added timeout support for uart read Signed-off-by: Henry Bruce Signed-off-by: Brendan Le Foll --- src/uart/uart.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/uart/uart.c b/src/uart/uart.c index 499af93..6d3973a 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -425,7 +425,25 @@ mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar) return MRAA_ERROR_INVALID_HANDLE; } - return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED; + struct termios termio; + // get current modes + if (tcgetattr(dev->fd, &termio)) { + syslog(LOG_ERR, "uart: tcgetattr() failed"); + return MRAA_ERROR_FEATURE_NOT_SUPPORTED; + } + if (read > 0) { + read = read / 100; + if (read == 0) + read = 1; + } + termio.c_lflag &= ~ICANON; /* Set non-canonical mode */ + termio.c_cc[VTIME] = read; /* Set timeout in tenth seconds */ + if (tcsetattr(dev->fd, TCSANOW, &termio) < 0) { + syslog(LOG_ERR, "uart: tcsetattr() failed"); + return MRAA_ERROR_FEATURE_NOT_SUPPORTED; + } + + return MRAA_SUCCESS; } const char*