From d4aa9520f97fd5af5b5c98baca876b046dd49300 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Tue, 15 May 2018 21:17:09 +0200 Subject: [PATCH] mbus-serial-scan: Make timeout adjustable --- bin/mbus-serial-scan.c | 17 +++++++++++++---- mbus/mbus-protocol-aux.c | 9 +++++++++ mbus/mbus-protocol-aux.h | 4 +++- mbus/mbus-serial.c | 5 ++++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bin/mbus-serial-scan.c b/bin/mbus-serial-scan.c index 4785fba..a252fce 100755 --- a/bin/mbus-serial-scan.c +++ b/bin/mbus-serial-scan.c @@ -55,11 +55,11 @@ main(int argc, char **argv) { mbus_handle *handle; char *device; - int address, retries = 0; + int address, retries = 0, timeout = 0; long baudrate = 9600; int opt, ret; - while ((opt = getopt(argc, argv, "db:r:")) != -1) + while ((opt = getopt(argc, argv, "db:r:t:")) != -1) { switch (opt) { @@ -72,8 +72,11 @@ main(int argc, char **argv) case 'r': retries = atoi(optarg); break; + case 't': + timeout = atoi(optarg); + break; default: - fprintf(stderr,"usage: %s [-d] [-b BAUDRATE] [-r RETRIES] device\n", + fprintf(stderr,"usage: %s [-d] [-b BAUDRATE] [-r RETRIES] [-t TIMEOUT] device\n", argv[0]); return 0; @@ -81,7 +84,7 @@ main(int argc, char **argv) } if (optind >= argc) { - fprintf(stderr,"usage: %s [-d] [-b BAUDRATE] [-r RETRIES] device\n", + fprintf(stderr,"usage: %s [-d] [-b BAUDRATE] [-r RETRIES] [-t TIMEOUT] device\n", argv[0]); return 0; @@ -113,6 +116,12 @@ main(int argc, char **argv) return 1; } + if (mbus_context_set_option(handle, MBUS_OPTION_TIMEOUT_OFFSET, timeout) == -1) + { + fprintf(stderr,"Failed to set timeout offset\n"); + return 1; + } + if (mbus_serial_set_baudrate(handle, baudrate) == -1) { fprintf(stderr,"Failed to set baud rate.\n"); diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index 8877d62..7b5793c 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1519,6 +1519,7 @@ mbus_context_serial(const char *device) handle->max_data_retry = 3; handle->max_search_retry = 1; + handle->timeout_offset = 0; handle->is_serial = 1; handle->purge_first_frame = MBUS_FRAME_PURGE_M2S; handle->auxdata = serial_data; @@ -1567,6 +1568,7 @@ mbus_context_tcp(const char *host, uint16_t port) handle->max_data_retry = 3; handle->max_search_retry = 1; + handle->timeout_offset = 0; handle->is_serial = 0; handle->purge_first_frame = MBUS_FRAME_PURGE_M2S; handle->auxdata = tcp_data; @@ -1652,6 +1654,13 @@ mbus_context_set_option(mbus_handle * handle, mbus_context_option option, long v return 0; } break; + case MBUS_OPTION_TIMEOUT_OFFSET: + if ((value >= 0) && (value <= 100)) + { + handle->timeout_offset = value; + return 0; + } + break; case MBUS_OPTION_PURGE_FIRST_FRAME: if ((value == MBUS_FRAME_PURGE_NONE) || (value == MBUS_FRAME_PURGE_M2S) || diff --git a/mbus/mbus-protocol-aux.h b/mbus/mbus-protocol-aux.h index 4dacef4..df164fd 100755 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -87,6 +87,7 @@ typedef struct _mbus_handle { int fd; int max_data_retry; int max_search_retry; + unsigned int timeout_offset; char purge_first_frame; char is_serial; /**< _handle type (non zero for serial) */ int (*open) (struct _mbus_handle *handle); @@ -154,7 +155,8 @@ typedef struct _mbus_record { typedef enum _mbus_context_option { MBUS_OPTION_MAX_DATA_RETRY, /**< option defines the maximum attempts of data request retransmission */ MBUS_OPTION_MAX_SEARCH_RETRY, /**< option defines the maximum attempts of search request retransmission */ - MBUS_OPTION_PURGE_FIRST_FRAME /**< option controls the echo cancelation for mbus_recv_frame */ + MBUS_OPTION_PURGE_FIRST_FRAME, /**< option controls the echo cancelation for mbus_recv_frame */ + MBUS_OPTION_TIMEOUT_OFFSET, /**< option defines the additional timeout offset */ } mbus_context_option; /** diff --git a/mbus/mbus-serial.c b/mbus/mbus-serial.c index 71092f0..d265004 100755 --- a/mbus/mbus-serial.c +++ b/mbus/mbus-serial.c @@ -75,7 +75,7 @@ mbus_serial_connect(mbus_handle *handle) // For 2400Bd this means (330 + 11) / 2400 + 0.05 = 188.75 ms (added 11 bit periods to receive first byte). // I.e. timeout of 0.2s seems appropriate for 2400Bd. - term->c_cc[VTIME] = (cc_t) 2; // Timeout in 1/10 sec + term->c_cc[VTIME] = (cc_t) 2 + handle->timeout_offset; // Timeout in 1/10 sec cfsetispeed(term, B2400); cfsetospeed(term, B2400); @@ -155,6 +155,9 @@ mbus_serial_set_baudrate(mbus_handle *handle, long baudrate) return -1; // unsupported baudrate } + // Add timeout offset for additional delay + serial_data->t.c_cc[VTIME] += handle->timeout_offset; + // Set input baud rate if (cfsetispeed(&(serial_data->t), speed) != 0) {