From: Bert Vermeulen Date: Mon, 6 Oct 2014 10:10:25 +0000 (+0200) Subject: serial: Add serial_timeout(). X-Git-Tag: libsigrok-0.4.0~885 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c5cfc735e50adc53e073355e5521b1c79266441e;p=libsigrok.git serial: Add serial_timeout(). This calculates a proper timeout value for blocking writes on the given serial port, for the given number of bytes. Timeout is based on a fixed 10ms OS overhead, baud rate, data bits and stop bits. --- diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index ccf75598..bed08e4c 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -656,6 +656,7 @@ SR_PRIV int serial_source_add(struct sr_session *session, SR_PRIV int serial_source_remove(struct sr_session *session, struct sr_serial_dev_inst *serial); SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id); +SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes); #endif /*--- hardware/common/ezusb.c -----------------------------------------------*/ diff --git a/src/serial.c b/src/serial.c index 63ffbe22..37463573 100644 --- a/src/serial.c +++ b/src/serial.c @@ -798,3 +798,43 @@ SR_PRIV GSList *sr_serial_find_usb(uint16_t vendor_id, uint16_t product_id) sp_free_port_list(ports); return tty_devs; } + +SR_PRIV int serial_timeout(struct sr_serial_dev_inst *port, int num_bytes) +{ + struct sp_port_config *config; + int timeout_ms, bits, baud, tmp; + + /* Default to 1s. */ + timeout_ms = 1000; + + if (sp_new_config(&config) < 0) + return timeout_ms; + + bits = baud = 0; + do { + if (sp_get_config(port->data, config) < 0) + break; + + /* Start bit. */ + bits = 1; + if (sp_get_config_bits(config, &tmp) < 0) + break; + bits += tmp; + if (sp_get_config_stopbits(config, &tmp) < 0) + break; + bits += tmp; + if (sp_get_config_baudrate(config, &tmp) < 0) + break; + baud = tmp; + } while (FALSE); + + if (bits && baud) { + /* Throw in 10ms for misc OS overhead. */ + timeout_ms = 10; + timeout_ms += ((1000.0 / baud) * bits) * num_bytes; + } + + sp_free_config(config); + + return timeout_ms; +}