@@ -1961,7 +1961,7 @@ void upsdrv_makevartable(void)
19611961 addvar (VAR_VALUE , "porttype" , "Modbus port type (serial, tcp, default=serial)" );
19621962#endif /* defined NUT_MODBUS_HAS_USB */
19631963 addvar (VAR_VALUE , "slaveid" , "Modbus slave id (default=1)" );
1964- addvar (VAR_VALUE , "response_timeout_ms" , "Modbus response timeout in milliseconds" );
1964+ addvar (VAR_VALUE , "response_timeout_ms" , "Modbus response timeout in milliseconds (default=500, 2000 for TCP) " );
19651965
19661966 /* Serial RTU parameters */
19671967 addvar (VAR_VALUE , "baudrate" , "Modbus serial RTU communication speed in baud (default=9600)" );
@@ -2181,7 +2181,7 @@ void upsdrv_initups(void)
21812181 int rtu_databits ;
21822182 int rtu_stopbits ;
21832183 int slaveid ;
2184- uint32_t response_timeout_ms ;
2184+ uint32_t response_timeout_ms = 500 ;
21852185 char tcp_host [256 ];
21862186 char tcp_port [6 ];
21872187
@@ -2256,6 +2256,12 @@ void upsdrv_initups(void)
22562256 fatalx (EXIT_FAILURE , "failed to parse host/port" );
22572257 }
22582258
2259+ /*
2260+ * Increase the default for TCP
2261+ * See https://github.com/networkupstools/nut/pull/3414#issuecomment-4243889805
2262+ */
2263+ response_timeout_ms = 2000 ;
2264+
22592265 modbus_ctx = modbus_new_tcp_pi (tcp_host , tcp_port );
22602266 } else if (!strcasecmp (val , "serial" )) {
22612267 val = getval ("baudrate" );
@@ -2292,13 +2298,18 @@ void upsdrv_initups(void)
22922298 val = getval ("response_timeout_ms" );
22932299 if (val != NULL ) {
22942300 response_timeout_ms = (uint32_t )strtoul (val , NULL , 0 );
2301+ }
2302+
2303+ if (response_timeout_ms < 100 ) {
2304+ upslogx (LOG_WARNING , "response_timeout_ms value %u ms is very low and may cause communication problems; consider increasing it" , response_timeout_ms );
2305+ }
22952306
22962307#if (defined NUT_MODBUS_TIMEOUT_ARG_sec_usec_uint32 ) || (defined NUT_MODBUS_TIMEOUT_ARG_sec_usec_uint32_cast_timeval_fields )
2297- r = modbus_set_response_timeout (modbus_ctx , response_timeout_ms / 1000 , (response_timeout_ms % 1000 ) * 1000 );
2298- if (r < 0 ) {
2299- modbus_free (modbus_ctx );
2300- fatalx (EXIT_FAILURE , "modbus_set_response_timeout: error(%s)" , modbus_strerror (errno ));
2301- }
2308+ r = modbus_set_response_timeout (modbus_ctx , response_timeout_ms / 1000 , (response_timeout_ms % 1000 ) * 1000 );
2309+ if (r < 0 ) {
2310+ modbus_free (modbus_ctx );
2311+ fatalx (EXIT_FAILURE , "modbus_set_response_timeout: error(%s)" , modbus_strerror (errno ));
2312+ }
23022313#elif (defined NUT_MODBUS_TIMEOUT_ARG_timeval_numeric_fields )
23032314 { /* see comments above */
23042315 struct timeval to ;
@@ -2309,7 +2320,6 @@ void upsdrv_initups(void)
23092320 }
23102321/* #elif (defined NUT_MODBUS_TIMEOUT_ARG_timeval) // some un-castable type in fields */
23112322#endif /* NUT_MODBUS_TIMEOUT_ARG_* */
2312- }
23132323
23142324 if (modbus_connect (modbus_ctx ) == -1 ) {
23152325 modbus_free (modbus_ctx );
0 commit comments