From: Matthias Heidbrink Date: Fri, 23 May 2014 20:26:34 +0000 (+0200) Subject: serial-dmm: Implement request timeout mechanism. X-Git-Tag: libsigrok-0.4.0~1281 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=2baac44db78c9d0a5c1cc18ea53dbe7df60f7e8f;p=libsigrok.git serial-dmm: Implement request timeout mechanism. (fixes request flooding for Voltcraft ME-42 and M-3650CR) This fixes bug #345. --- diff --git a/hardware/serial-dmm/api.c b/hardware/serial-dmm/api.c index 136d2a7c..12396901 100644 --- a/hardware/serial-dmm/api.c +++ b/hardware/serial-dmm/api.c @@ -78,112 +78,112 @@ SR_PRIV struct sr_dev_driver tenma_72_7750_ser_driver_info; SR_PRIV struct dmm_info dmms[] = { { "BBC Goertz Metrawatt", "M2110", "1200/7n2", 1200, - BBCGM_M2110_PACKET_SIZE, NULL, + BBCGM_M2110_PACKET_SIZE, 0, 0, NULL, sr_m2110_packet_valid, sr_m2110_parse, NULL, &bbcgm_m2110_driver_info, receive_data_BBCGM_M2110, }, { "Digitek", "DT4000ZC", "2400/8n1/dtr=1", 2400, - FS9721_PACKET_SIZE, NULL, + FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_10_temp_c, &digitek_dt4000zc_driver_info, receive_data_DIGITEK_DT4000ZC, }, { "TekPower", "TP4000ZC", "2400/8n1/dtr=1", 2400, - FS9721_PACKET_SIZE, NULL, + FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_10_temp_c, &tekpower_tp4000zc_driver_info, receive_data_TEKPOWER_TP4000ZC, }, { "Metex", "ME-31", "600/7n2/rts=0/dtr=1", 600, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &metex_me31_driver_info, receive_data_METEX_ME31, }, { "Peaktech", "3410", "600/7n2/rts=0/dtr=1", 600, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &peaktech_3410_driver_info, receive_data_PEAKTECH_3410, }, { "MASTECH", "MAS345", "600/7n2/rts=0/dtr=1", 600, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &mastech_mas345_driver_info, receive_data_MASTECH_MAS345, }, { "V&A", "VA18B", "2400/8n1", 2400, - FS9721_PACKET_SIZE, NULL, + FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_01_temp_c, &va_va18b_driver_info, receive_data_VA_VA18B, }, { "V&A", "VA40B", "2400/8n1", 2400, - FS9721_PACKET_SIZE, NULL, + FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_max_c_min, &va_va40b_driver_info, receive_data_VA_VA40B, }, { "Metex", "M-3640D", "1200/7n2/rts=0/dtr=1", 1200, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &metex_m3640d_driver_info, receive_data_METEX_M3640D, }, { "Metex", "M-4650CR", "1200/7n2/rts=0/dtr=1", 1200, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &metex_m4650cr_driver_info, receive_data_METEX_M4650CR, }, { "PeakTech", "4370", "1200/7n2/rts=0/dtr=1", 1200, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &peaktech_4370_driver_info, receive_data_PEAKTECH_4370, }, { "PCE", "PCE-DM32", "2400/8n1", 2400, - FS9721_PACKET_SIZE, NULL, + FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_01_10_temp_f_c, &pce_pce_dm32_driver_info, receive_data_PCE_PCE_DM32, }, { "RadioShack", "22-168", "1200/7n2/rts=0/dtr=1", 1200, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &radioshack_22_168_driver_info, receive_data_RADIOSHACK_22_168, }, { "RadioShack", "22-805", "600/7n2/rts=0/dtr=1", 600, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &radioshack_22_805_driver_info, receive_data_RADIOSHACK_22_805, }, { "RadioShack", "22-812", "4800/8n1/rts=0/dtr=1", 4800, - RS9LCD_PACKET_SIZE, NULL, + RS9LCD_PACKET_SIZE, 0, 0, NULL, sr_rs9lcd_packet_valid, sr_rs9lcd_parse, NULL, &radioshack_22_812_driver_info, receive_data_RADIOSHACK_22_812, }, { "Tecpel", "DMM-8061 (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9721_PACKET_SIZE, NULL, + 2400, FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_00_temp_c, &tecpel_dmm_8061_ser_driver_info, @@ -191,35 +191,35 @@ SR_PRIV struct dmm_info dmms[] = { }, { "Voltcraft", "M-3650CR", "1200/7n2/rts=0/dtr=1", 1200, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 150, 20, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &voltcraft_m3650cr_driver_info, receive_data_VOLTCRAFT_M3650CR, }, { "Voltcraft", "M-3650D", "1200/7n2/rts=0/dtr=1", 1200, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &voltcraft_m3650d_driver_info, receive_data_VOLTCRAFT_M3650D, }, { "Voltcraft", "M-4650CR", "1200/7n2/rts=0/dtr=1", 1200, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 0, 0, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &voltcraft_m4650cr_driver_info, receive_data_VOLTCRAFT_M4650CR, }, { "Voltcraft", "ME-42", "600/7n2/rts=0/dtr=1", 600, - METEX14_PACKET_SIZE, sr_metex14_packet_request, + METEX14_PACKET_SIZE, 250, 60, sr_metex14_packet_request, sr_metex14_packet_valid, sr_metex14_parse, NULL, &voltcraft_me42_driver_info, receive_data_VOLTCRAFT_ME42, }, { "Voltcraft", "VC-820 (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9721_PACKET_SIZE, NULL, + 2400, FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, NULL, &voltcraft_vc820_ser_driver_info, @@ -232,7 +232,7 @@ SR_PRIV struct dmm_info dmms[] = { * bit "z1" to indicate "diode mode" and "voltage". */ "Voltcraft", "VC-830 (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9922_PACKET_SIZE, NULL, + 2400, FS9922_PACKET_SIZE, 0, 0, NULL, sr_fs9922_packet_valid, sr_fs9922_parse, &sr_fs9922_z1_diode, &voltcraft_vc830_ser_driver_info, @@ -240,7 +240,7 @@ SR_PRIV struct dmm_info dmms[] = { }, { "Voltcraft", "VC-840 (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9721_PACKET_SIZE, NULL, + 2400, FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_00_temp_c, &voltcraft_vc840_ser_driver_info, @@ -248,7 +248,7 @@ SR_PRIV struct dmm_info dmms[] = { }, { "UNI-T", "UT60A (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9721_PACKET_SIZE, NULL, + 2400, FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, NULL, &uni_t_ut60a_ser_driver_info, @@ -256,7 +256,7 @@ SR_PRIV struct dmm_info dmms[] = { }, { "UNI-T", "UT60E (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9721_PACKET_SIZE, NULL, + 2400, FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_00_temp_c, &uni_t_ut60e_ser_driver_info, @@ -265,47 +265,47 @@ SR_PRIV struct dmm_info dmms[] = { { /* Note: ES51986 baudrate is actually 19230! */ "UNI-T", "UT60G (UT-D02 cable)", "19200/7o1/rts=0/dtr=1", - 19200, ES519XX_11B_PACKET_SIZE, NULL, + 19200, ES519XX_11B_PACKET_SIZE, 0, 0, NULL, sr_es519xx_19200_11b_packet_valid, sr_es519xx_19200_11b_parse, NULL, &uni_t_ut60g_ser_driver_info, receive_data_UNI_T_UT60G_SER, }, { "UNI-T", "UT61B (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9922_PACKET_SIZE, NULL, + 2400, FS9922_PACKET_SIZE, 0, 0, NULL, sr_fs9922_packet_valid, sr_fs9922_parse, NULL, &uni_t_ut61b_ser_driver_info, receive_data_UNI_T_UT61B_SER, }, { "UNI-T", "UT61C (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9922_PACKET_SIZE, NULL, + 2400, FS9922_PACKET_SIZE, 0, 0, NULL, sr_fs9922_packet_valid, sr_fs9922_parse, NULL, &uni_t_ut61c_ser_driver_info, receive_data_UNI_T_UT61C_SER, }, { "UNI-T", "UT61D (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9922_PACKET_SIZE, NULL, + 2400, FS9922_PACKET_SIZE, 0, 0, NULL, sr_fs9922_packet_valid, sr_fs9922_parse, NULL, &uni_t_ut61d_ser_driver_info, receive_data_UNI_T_UT61D_SER, }, { /* Note: ES51922 baudrate is actually 19230! */ "UNI-T", "UT61E (UT-D02 cable)", "19200/7o1/rts=0/dtr=1", - 19200, ES519XX_14B_PACKET_SIZE, NULL, + 19200, ES519XX_14B_PACKET_SIZE, 0, 0, NULL, sr_es519xx_19200_14b_packet_valid, sr_es519xx_19200_14b_parse, NULL, &uni_t_ut61e_ser_driver_info, receive_data_UNI_T_UT61E_SER, }, { "ISO-TECH", "IDM103N", "2400/7o1/rts=0/dtr=1", - 2400, ES519XX_11B_PACKET_SIZE, NULL, + 2400, ES519XX_11B_PACKET_SIZE, 0, 0, NULL, sr_es519xx_2400_11b_packet_valid, sr_es519xx_2400_11b_parse, NULL, &iso_tech_idm103n_driver_info, receive_data_ISO_TECH_IDM103N, }, { "Tenma", "72-7745 (UT-D02 cable)", "2400/8n1/rts=0/dtr=1", - 2400, FS9721_PACKET_SIZE, NULL, + 2400, FS9721_PACKET_SIZE, 0, 0, NULL, sr_fs9721_packet_valid, sr_fs9721_parse, sr_fs9721_00_temp_c, &tenma_72_7745_ser_driver_info, receive_data_TENMA_72_7745_SER, @@ -313,7 +313,7 @@ SR_PRIV struct dmm_info dmms[] = { { /* Note: ES51986 baudrate is actually 19230! */ "Tenma", "72-7750 (UT-D02 cable)", "19200/7o1/rts=0/dtr=1", - 19200, ES519XX_11B_PACKET_SIZE, NULL, + 19200, ES519XX_11B_PACKET_SIZE, 0, 0, NULL, sr_es519xx_19200_11b_packet_valid, sr_es519xx_19200_11b_parse, NULL, &tenma_72_7750_ser_driver_info, receive_data_TENMA_72_7750_SER, diff --git a/hardware/serial-dmm/protocol.c b/hardware/serial-dmm/protocol.c index 36e69444..0b2472fb 100644 --- a/hardware/serial-dmm/protocol.c +++ b/hardware/serial-dmm/protocol.c @@ -68,6 +68,37 @@ static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, } } +/** Request packet, if required. */ +SR_PRIV int req_packet(struct sr_dev_inst *sdi, int dmm) +{ + struct dev_context *devc; + struct sr_serial_dev_inst *serial; + int ret; + + if (!dmms[dmm].packet_request) + return SR_OK; + + devc = sdi->priv; + serial = sdi->conn; + + if (devc->req_next_at && (devc->req_next_at > g_get_monotonic_time())) { + sr_spew("Not requesting new packet yet, %" PRIi64 " ms left.", + ((devc->req_next_at - g_get_monotonic_time()) / 1000)); + return SR_OK; + } + + ret = dmms[dmm].packet_request(serial); + if (ret < 0) { + sr_err("Failed to request packet: %d.", ret); + return ret; + } + + if (dmms[dmm].req_timeout_ms) + devc->req_next_at = g_get_monotonic_time() + (dmms[dmm].req_timeout_ms * 1000); + + return SR_OK; +} + static void handle_new_data(struct sr_dev_inst *sdi, int dmm, void *info) { struct dev_context *devc; @@ -93,6 +124,14 @@ static void handle_new_data(struct sr_dev_inst *sdi, int dmm, void *info) if (dmms[dmm].packet_valid(devc->buf + offset)) { handle_packet(devc->buf + offset, sdi, dmm, info); offset += dmms[dmm].packet_size; + + /* Request next packet, if required. */ + if (!dmms[dmm].packet_request) + break; + if (dmms[dmm].req_timeout_ms || dmms[dmm].req_delay_ms) + devc->req_next_at = g_get_monotonic_time() + + dmms[dmm].req_delay_ms * 1000; + req_packet(sdi, dmm); } else { offset++; } @@ -108,9 +147,7 @@ static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data) { struct sr_dev_inst *sdi; struct dev_context *devc; - struct sr_serial_dev_inst *serial; int64_t time; - int ret; (void)fd; @@ -120,20 +157,13 @@ static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data) if (!(devc = sdi->priv)) return TRUE; - serial = sdi->conn; - if (revents == G_IO_IN) { /* Serial data arrived. */ handle_new_data(sdi, dmm, info); } else { - /* Timeout, send another packet request (if DMM needs it). */ - if (dmms[dmm].packet_request) { - ret = dmms[dmm].packet_request(serial); - if (ret < 0) { - sr_err("Failed to request packet: %d.", ret); - return FALSE; - } - } + /* Timeout; send another packet request if DMM needs it. */ + if (dmms[dmm].packet_request && (req_packet(sdi, dmm) < 0)) + return FALSE; } if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { diff --git a/hardware/serial-dmm/protocol.h b/hardware/serial-dmm/protocol.h index f96e43da..fb5a2a33 100644 --- a/hardware/serial-dmm/protocol.h +++ b/hardware/serial-dmm/protocol.h @@ -69,6 +69,12 @@ struct dmm_info { uint32_t baudrate; /** Packet size in bytes. */ int packet_size; + /** Request timeout [ms] before request is considered lost and a new + * one is sent. Used only if device needs polling. */ + int64_t req_timeout_ms; + /** Delay between reception of packet and next request. Some DMMs + * need this. Used only if device needs polling. */ + int64_t req_delay_ms; /** Packet request function. */ int (*packet_request)(struct sr_serial_dev_inst *); /** Packet validation function. */ @@ -102,13 +108,20 @@ struct dev_context { /** The current number of already received samples. */ uint64_t num_samples; + /** The starting time of current sampling run. */ int64_t starttime; uint8_t buf[DMM_BUFSIZE]; int bufoffset; int buflen; + + /** The timestamp [µs] to send the next request. + * Used only if device needs polling. */ + int64_t req_next_at; }; +SR_PRIV int req_packet(struct sr_dev_inst *sdi, int dmm); + SR_PRIV int receive_data_BBCGM_M2110(int fd, int revents, void *cb_data); SR_PRIV int receive_data_DIGITEK_DT4000ZC(int fd, int revents, void *cb_data); SR_PRIV int receive_data_TEKPOWER_TP4000ZC(int fd, int revents, void *cb_data);