Commit bbcc2438bc23954815c2a3cdcc848050671c2b0d
1 parent
ee012c8a
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.
Showing
3 changed files
with
66 additions
and
5 deletions
NEWS
| 1 | +libmodbus 1.9.x | |
| 2 | +- Slave API | |
| 3 | + https://blueprints.launchpad.net/libmodbus/+spec/slave-api | |
| 4 | +- Waf build support | |
| 5 | + https://blueprints.launchpad.net/libmodbus/+spec/waf-support | |
| 6 | +- MacOS X support by Matthew Butch | |
| 7 | + https://blueprints.launchpad.net/libmodbus/+spec/macosx-support | |
| 8 | +- No more glib dependency | |
| 9 | + https://blueprints.launchpad.net/libmodbus/+spec/glib-dependency | |
| 10 | +- Fix #159443 reported by Stefan Bisanz | |
| 11 | + Index of incoming data in force multiple coils function | |
| 12 | +- Fix #161989 reported by Konstantinos Togias | |
| 13 | + Serial device paths more than 10 chars long (eg. /dev/ttyUSB0) don't | |
| 14 | + fit to modbus_param_t -> device char[11] var. | |
| 15 | +- Fix #188189 reported by Chris Hellyar | |
| 16 | + Compute_response_size() no entry for read_input_status() | |
| 17 | +- Fix #191039 reported by Todd Denniston | |
| 18 | + modbus.h is not installed at prefix. | |
| 19 | +- Fix #211460 reported by Todd Denniston | |
| 20 | + With TCP, automatic reconnect on error may not be desired. | |
| 21 | + | |
| 1 | 22 | libmodbus 1.2.4 (2008-03-14) |
| 2 | 23 | - Fix #191039 reported by Todd Denniston |
| 3 | 24 | modbus.h is not installed at prefix. |
| ... | ... | @@ -9,18 +30,18 @@ libmodbus 1.2.3 (2008-02-03) |
| 9 | 30 | Slave address in build_request_packet_tcp() is hardcoded as 0xFF. |
| 10 | 31 | |
| 11 | 32 | libmodbus 1.2.2 (2007-11-12) |
| 12 | -- Fix #161989 (Konstantinos Togias) | |
| 33 | +- Fix #161989 reported by Konstantinos Togias | |
| 13 | 34 | Serial device paths more than 10 chars long (eg. /dev/ttyUSB0) don't |
| 14 | 35 | fit to modbus_param_t -> device char[11] var. |
| 15 | 36 | - Structure is also bit better 'packed' to conserve memory (see the |
| 16 | 37 | trunk for a real enhancement). |
| 17 | 38 | |
| 18 | 39 | libmodbus 1.2.1 (2007-11-02) |
| 19 | -- Fix #159443 (Stefan Bisanz) | |
| 40 | +- Fix #159443 reported by Stefan Bisanz | |
| 20 | 41 | Index of incoming data in force multiple coils function |
| 21 | 42 | - Deleted useless code in check_crc16() |
| 22 | 43 | - Untabify source code |
| 23 | -- Changed author's email (Stéphane Raimbault) | |
| 44 | +- Changed author's email to Stéphane Raimbault | |
| 24 | 45 | |
| 25 | 46 | libmodbus 1.2.0 (2007-05-10) |
| 26 | 47 | - FIX Compilation GCC-4.0 | ... | ... |
modbus/modbus.c
| ... | ... | @@ -148,8 +148,10 @@ static void error_treat(int ret, const char *string, modbus_param_t *mb_param) |
| 148 | 148 | if (mb_param->type_com == RTU) { |
| 149 | 149 | tcflush(mb_param->fd, TCIOFLUSH); |
| 150 | 150 | } else { |
| 151 | - modbus_close(mb_param); | |
| 152 | - modbus_connect(mb_param); | |
| 151 | + if (mb_param->error_handling == RECONNECT_ON_ERROR) { | |
| 152 | + modbus_close(mb_param); | |
| 153 | + modbus_connect(mb_param); | |
| 154 | + } | |
| 153 | 155 | } |
| 154 | 156 | } |
| 155 | 157 | |
| ... | ... | @@ -1168,6 +1170,28 @@ void modbus_init_tcp(modbus_param_t *mb_param, char *ip, uint16_t port) |
| 1168 | 1170 | mb_param->type_com = TCP; |
| 1169 | 1171 | mb_param->header_length = HEADER_LENGTH_TCP; |
| 1170 | 1172 | mb_param->checksum_size = CHECKSUM_SIZE_TCP; |
| 1173 | + mb_param->error_handling = RECONNECT_ON_ERROR; | |
| 1174 | +} | |
| 1175 | + | |
| 1176 | +/* By default, the error handling mode used is RECONNECT_ON_ERROR. | |
| 1177 | + | |
| 1178 | + With RECONNECT_ON_ERROR, the library will attempt an immediate | |
| 1179 | + reconnection which may hang for several seconds if the network to | |
| 1180 | + the remote target unit is down. | |
| 1181 | + | |
| 1182 | + With NOP_ON_ERROR, it is expected that the application will | |
| 1183 | + check for network error returns and deal with them as necessary. | |
| 1184 | + | |
| 1185 | + This function is only useful in TCP mode. | |
| 1186 | +*/ | |
| 1187 | +void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling) | |
| 1188 | +{ | |
| 1189 | + if (error_handling == RECONNECT_ON_ERROR || | |
| 1190 | + error_handling == NOP_ON_ERROR) { | |
| 1191 | + mb_param->error_handling = error_handling; | |
| 1192 | + } else { | |
| 1193 | + printf("Invalid setting for error handling (not changed)\n"); | |
| 1194 | + } | |
| 1171 | 1195 | } |
| 1172 | 1196 | |
| 1173 | 1197 | ... | ... |
modbus/modbus.h
| ... | ... | @@ -108,6 +108,7 @@ |
| 108 | 108 | #define MSG_SIZE_UNDEFINED -1 |
| 109 | 109 | |
| 110 | 110 | typedef enum { RTU, TCP } type_com_t; |
| 111 | +typedef enum { RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t; | |
| 111 | 112 | |
| 112 | 113 | /* This structure is byte-aligned */ |
| 113 | 114 | typedef struct { |
| ... | ... | @@ -148,6 +149,8 @@ typedef struct { |
| 148 | 149 | int header_length; |
| 149 | 150 | /* Checksum size RTU = 2 and TCP = 0 */ |
| 150 | 151 | int checksum_size; |
| 152 | + /* In error_treat with TCP, do a reconnect or just dump the error */ | |
| 153 | + error_handling_t error_handling; | |
| 151 | 154 | } modbus_param_t; |
| 152 | 155 | |
| 153 | 156 | typedef struct { |
| ... | ... | @@ -229,6 +232,19 @@ void modbus_init_rtu(modbus_param_t *mb_param, char *device, |
| 229 | 232 | */ |
| 230 | 233 | void modbus_init_tcp(modbus_param_t *mb_param, char *ip_address, uint16_t port); |
| 231 | 234 | |
| 235 | +/* By default, the error handling mode used is RECONNECT_ON_ERROR. | |
| 236 | + | |
| 237 | + With RECONNECT_ON_ERROR, the library will attempt an immediate | |
| 238 | + reconnection which may hang for several seconds if the network to | |
| 239 | + the remote target unit is down. | |
| 240 | + | |
| 241 | + With NOP_ON_ERROR, it is expected that the application will | |
| 242 | + check for network error returns and deal with them as necessary. | |
| 243 | + | |
| 244 | + This function is only useful in TCP mode. | |
| 245 | + */ | |
| 246 | +void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling); | |
| 247 | + | |
| 232 | 248 | /* Sets up a serial port for RTU communications to modbus or a TCP |
| 233 | 249 | connexion */ |
| 234 | 250 | int modbus_connect(modbus_param_t *mb_param); | ... | ... |