Commit 94c9fcd94f5c0cf393406e9442fdf69ed8cb8780

Authored by Stéphane Raimbault
1 parent a38e976b

Remove a function call preset_response.

- code is easier to understand
- code is bit faster.
Showing 1 changed file with 20 additions and 23 deletions
modbus/modbus.c
... ... @@ -1094,36 +1094,29 @@ int read_input_registers(modbus_param_t *mb_param, int slave,
1094 1094 return status;
1095 1095 }
1096 1096  
1097   -/* Gets the raw data from the input stream */
1098   -static int preset_response(modbus_param_t *mb_param, uint8_t *query)
1099   -{
1100   - int ret;
1101   - uint8_t response[MAX_MESSAGE_LENGTH];
1102   -
1103   - ret = modbus_check_response(mb_param, query, response);
1104   -
1105   - return ret;
1106   -}
1107   -
1108   -/* Sends a value to a register in a slave */
  1097 +/* Sends a value to a register in a slave.
  1098 + Used by force_single_coil and preset_single_register */
1109 1099 static int set_single(modbus_param_t *mb_param, int slave, int function,
1110 1100 int addr, int value)
1111 1101 {
1112 1102 int ret;
1113 1103 int query_length;
1114   - uint8_t query[MAX_MESSAGE_LENGTH];
  1104 + uint8_t query[MIN_QUERY_LENGTH];
1115 1105  
1116 1106 query_length = build_query_basis(mb_param, slave, function,
1117 1107 addr, value, query);
1118 1108  
1119 1109 ret = modbus_send(mb_param, query, query_length);
1120   - if (ret > 0)
1121   - ret = preset_response(mb_param, query);
  1110 + if (ret > 0) {
  1111 + /* Used by force_single_coil and
  1112 + * preset_single_register */
  1113 + uint8_t response[MIN_QUERY_LENGTH];
  1114 + ret = modbus_check_response(mb_param, query, response);
  1115 + }
1122 1116  
1123 1117 return ret;
1124 1118 }
1125 1119  
1126   -
1127 1120 /* Turns ON or OFF a single coil in the slave device */
1128 1121 int force_single_coil(modbus_param_t *mb_param, int slave,
1129 1122 int coil_addr, int state)
... ... @@ -1195,8 +1188,11 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave,
1195 1188 }
1196 1189  
1197 1190 ret = modbus_send(mb_param, query, query_length);
1198   - if (ret > 0)
1199   - ret = preset_response(mb_param, query);
  1191 + if (ret > 0) {
  1192 + uint8_t response[MAX_MESSAGE_LENGTH];
  1193 + ret = modbus_check_response(mb_param, query, response);
  1194 + }
  1195 +
1200 1196  
1201 1197 return ret;
1202 1198 }
... ... @@ -1231,8 +1227,10 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave,
1231 1227 }
1232 1228  
1233 1229 ret = modbus_send(mb_param, query, query_length);
1234   - if (ret > 0)
1235   - ret = preset_response(mb_param, query);
  1230 + if (ret > 0) {
  1231 + uint8_t response[MAX_MESSAGE_LENGTH];
  1232 + ret = modbus_check_response(mb_param, query, response);
  1233 + }
1236 1234  
1237 1235 return ret;
1238 1236 }
... ... @@ -1243,14 +1241,12 @@ int report_slave_id(modbus_param_t *mb_param, int slave,
1243 1241 {
1244 1242 int ret;
1245 1243 int query_length;
1246   -
1247 1244 uint8_t query[MIN_QUERY_LENGTH];
1248   - uint8_t response[MAX_MESSAGE_LENGTH];
1249 1245  
1250 1246 query_length = build_query_basis(mb_param, slave, FC_REPORT_SLAVE_ID,
1251 1247 0, 0, query);
1252 1248  
1253   - /* start_addr and count are not used */
  1249 + /* HACKISH, start_addr and count are not used */
1254 1250 query_length -= 4;
1255 1251  
1256 1252 ret = modbus_send(mb_param, query, query_length);
... ... @@ -1258,6 +1254,7 @@ int report_slave_id(modbus_param_t *mb_param, int slave,
1258 1254 int i;
1259 1255 int offset;
1260 1256 int offset_length;
  1257 + uint8_t response[MAX_MESSAGE_LENGTH];
1261 1258  
1262 1259 /* Byte count, slave id, run indicator status,
1263 1260 additional data */
... ...