Commit 02f16fd439f81de159fa2abde4e030ddc1ac6ccc

Authored by Stéphane Raimbault
1 parent 14749913

Common error handling for RTU and TCP.

modbus/modbus.c
... ... @@ -140,13 +140,12 @@ static void error_treat(int code, const char *string, modbus_param_t *mb_param)
140 140 perror(string);
141 141 printf("\n\nERROR %s\n\n", string);
142 142  
143   - // FIXME Create a new FLUSH_OR_RECONNECT_ON_ERROR
144 143 // FIXME Filter on code
145 144  
146   - if (mb_param->type_com == RTU) {
147   - tcflush(mb_param->fd, TCIOFLUSH);
148   - } else {
149   - if (mb_param->error_handling == RECONNECT_ON_ERROR) {
  145 + if (mb_param->error_handling == FLUSH_OR_RECONNECT_ON_ERROR) {
  146 + if (mb_param->type_com == RTU) {
  147 + tcflush(mb_param->fd, TCIOFLUSH);
  148 + } else {
150 149 modbus_close(mb_param);
151 150 modbus_connect(mb_param);
152 151 }
... ... @@ -1224,23 +1223,22 @@ void modbus_init_tcp(modbus_param_t *mb_param, char *ip, uint16_t port)
1224 1223 mb_param->type_com = TCP;
1225 1224 mb_param->header_length = HEADER_LENGTH_TCP;
1226 1225 mb_param->checksum_size = CHECKSUM_SIZE_TCP;
1227   - mb_param->error_handling = RECONNECT_ON_ERROR;
  1226 + mb_param->error_handling = FLUSH_OR_RECONNECT_ON_ERROR;
1228 1227 }
1229 1228  
1230   -/* By default, the error handling mode used is RECONNECT_ON_ERROR.
  1229 +/* By default, the error handling mode used is FLUSH_OR_RECONNECT_ON_ERROR.
1231 1230  
1232   - With RECONNECT_ON_ERROR, the library will attempt an immediate
1233   - reconnection which may hang for several seconds if the network to
1234   - the remote target unit is down.
  1231 + With FLUSH_OR_RECONNECT_ON_ERROR, the library will flush to I/O
  1232 + port in RTU mode or attempt an immediate reconnection which may
  1233 + hang for several seconds if the network to the remote target unit
  1234 + is down in TCP mode.
1235 1235  
1236 1236 With NOP_ON_ERROR, it is expected that the application will
1237   - check for network error returns and deal with them as necessary.
1238   -
1239   - This function is only useful in TCP mode.
  1237 + check for error returns and deal with them as necessary.
1240 1238 */
1241 1239 void modbus_set_error_handling(modbus_param_t *mb_param, error_handling_t error_handling)
1242 1240 {
1243   - if (error_handling == RECONNECT_ON_ERROR ||
  1241 + if (error_handling == FLUSH_OR_RECONNECT_ON_ERROR ||
1244 1242 error_handling == NOP_ON_ERROR) {
1245 1243 mb_param->error_handling = error_handling;
1246 1244 } else {
... ...
modbus/modbus.h
... ... @@ -110,7 +110,7 @@
110 110 #define MSG_SIZE_UNDEFINED -1
111 111  
112 112 typedef enum { RTU, TCP } type_com_t;
113   -typedef enum { RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
  113 +typedef enum { FLUSH_OR_RECONNECT_ON_ERROR, NOP_ON_ERROR } error_handling_t;
114 114  
115 115 /* This structure is byte-aligned */
116 116 typedef struct {
... ...