Prechádzať zdrojové kódy

Fix #211460 reported by Todd Denniston
With TCP, automatic reconnect on error may not be desired.
It's now possible to adjust the error handling mode.

Stéphane Raimbault 17 rokov pred
rodič
commit
bbcc2438bc
3 zmenil súbory, kde vykonal 66 pridanie a 5 odobranie
  1. 24 3
      NEWS
  2. 26 2
      modbus/modbus.c
  3. 16 0
      modbus/modbus.h

+ 24 - 3
NEWS

@@ -1,3 +1,24 @@
+libmodbus 1.9.x
+- Slave API
+  https://blueprints.launchpad.net/libmodbus/+spec/slave-api
+- Waf build support
+  https://blueprints.launchpad.net/libmodbus/+spec/waf-support
+- MacOS X support by Matthew Butch
+  https://blueprints.launchpad.net/libmodbus/+spec/macosx-support
+- No more glib dependency
+  https://blueprints.launchpad.net/libmodbus/+spec/glib-dependency
+- Fix #159443 reported by Stefan Bisanz
+  Index of incoming data in force multiple coils function
+- Fix #161989 reported by Konstantinos Togias
+  Serial device paths more than 10 chars long (eg. /dev/ttyUSB0) don't
+  fit to modbus_param_t -> device char[11] var.
+- Fix #188189 reported by Chris Hellyar
+  Compute_response_size() no entry for read_input_status()
+- Fix #191039 reported by Todd Denniston
+  modbus.h is not installed at prefix.
+- Fix #211460 reported by Todd Denniston
+  With TCP, automatic reconnect on error may not be desired.
+
 libmodbus 1.2.4 (2008-03-14)
 - Fix #191039 reported by Todd Denniston
   modbus.h is not installed at prefix.
@@ -9,18 +30,18 @@ libmodbus 1.2.3 (2008-02-03)
   Slave address in build_request_packet_tcp() is hardcoded as 0xFF.
 
 libmodbus 1.2.2 (2007-11-12)
-- Fix #161989 (Konstantinos Togias)
+- Fix #161989 reported by Konstantinos Togias
   Serial device paths more than 10 chars long (eg. /dev/ttyUSB0) don't
   fit to modbus_param_t -> device char[11] var.
 - Structure is also bit better 'packed' to conserve memory (see the
   trunk for a real enhancement).
 
 libmodbus 1.2.1 (2007-11-02)
-- Fix #159443 (Stefan Bisanz)
+- Fix #159443 reported by Stefan Bisanz
   Index of incoming data in force multiple coils function
 - Deleted useless code in check_crc16()
 - Untabify source code
-- Changed author's email (Stéphane Raimbault)
+- Changed author's email to Stéphane Raimbault
 
 libmodbus 1.2.0 (2007-05-10)
 - FIX Compilation GCC-4.0

+ 26 - 2
modbus/modbus.c

@@ -148,8 +148,10 @@ static void error_treat(int ret, const char *string, modbus_param_t *mb_param)
         if (mb_param->type_com == RTU) {
                 tcflush(mb_param->fd, TCIOFLUSH);
         } else {
-                modbus_close(mb_param);
-                modbus_connect(mb_param);
+                if (mb_param->error_handling == RECONNECT_ON_ERROR) {
+                        modbus_close(mb_param);
+                        modbus_connect(mb_param);
+                }
         }
 }
 
@@ -1168,6 +1170,28 @@ void modbus_init_tcp(modbus_param_t *mb_param, char *ip, uint16_t port)
         mb_param->type_com = TCP;
         mb_param->header_length = HEADER_LENGTH_TCP;
         mb_param->checksum_size = CHECKSUM_SIZE_TCP;
+        mb_param->error_handling = RECONNECT_ON_ERROR;
+}
+
+/* By default, the error handling mode used is RECONNECT_ON_ERROR.
+
+   With RECONNECT_ON_ERROR, the library will attempt an immediate
+   reconnection which may hang for several seconds if the network to
+   the remote target unit is down.
+
+   With NOP_ON_ERROR, it is expected that the application will
+   check for network error returns and deal with them as necessary.
+
+   This function is only useful in TCP mode. 
+*/
+void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling)
+{
+        if (error_handling == RECONNECT_ON_ERROR ||
+            error_handling == NOP_ON_ERROR) {
+                mb_param->error_handling = error_handling;
+        } else {
+                printf("Invalid setting for error handling (not changed)\n");
+        }
 }
 
 

+ 16 - 0
modbus/modbus.h

@@ -108,6 +108,7 @@
 #define MSG_SIZE_UNDEFINED -1
 
 typedef enum { RTU, TCP } type_com_t;
+typedef enum { RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
 
 /* This structure is byte-aligned */
 typedef struct {
@@ -148,6 +149,8 @@ typedef struct {
         int header_length;
         /* Checksum size RTU = 2 and TCP = 0 */
         int checksum_size;
+        /* In error_treat with TCP, do a reconnect or just dump the error */
+        error_handling_t error_handling;
 } modbus_param_t;
 
 typedef struct {
@@ -229,6 +232,19 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device,
 */
 void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, uint16_t port);
 
+/* By default, the error handling mode used is RECONNECT_ON_ERROR.
+
+   With RECONNECT_ON_ERROR, the library will attempt an immediate
+   reconnection which may hang for several seconds if the network to
+   the remote target unit is down.
+
+   With NOP_ON_ERROR, it is expected that the application will
+   check for network error returns and deal with them as necessary.
+
+   This function is only useful in TCP mode.
+ */
+void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling);
+
 /* Sets up a serial port for RTU communications to modbus or a TCP
    connexion */
 int modbus_connect(modbus_param_t *mb_param);