X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fserial.c;h=37463573256f26c0c750d789ac931566968ecd69;hb=a42a39ac378534782d44f166b879e84cefa66ed6;hp=63ffbe2211f332c645289f36a8a7a81af80a0273;hpb=eead2782427ee5da9b793527f9484ac827a7bec5;p=libsigrok.git 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; +}