Commit 3540cd74f212f5733eecabea84e57da1589e551e
1 parent
9798adc7
Remove one function call overhead (read_reg_response).
Showing
1 changed file
with
16 additions
and
26 deletions
modbus/modbus.c
| @@ -1026,6 +1026,7 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, | @@ -1026,6 +1026,7 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, | ||
| 1026 | int ret; | 1026 | int ret; |
| 1027 | int query_length; | 1027 | int query_length; |
| 1028 | uint8_t query[MIN_QUERY_LENGTH]; | 1028 | uint8_t query[MIN_QUERY_LENGTH]; |
| 1029 | + uint8_t response[MAX_MESSAGE_LENGTH]; | ||
| 1029 | 1030 | ||
| 1030 | if (nb > MAX_REGISTERS) { | 1031 | if (nb > MAX_REGISTERS) { |
| 1031 | printf("EROOR Too many holding registers requested (%d > %d)\n", | 1032 | printf("EROOR Too many holding registers requested (%d > %d)\n", |
| @@ -1037,8 +1038,21 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, | @@ -1037,8 +1038,21 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, | ||
| 1037 | start_addr, nb, query); | 1038 | start_addr, nb, query); |
| 1038 | 1039 | ||
| 1039 | ret = modbus_send(mb_param, query, query_length); | 1040 | ret = modbus_send(mb_param, query, query_length); |
| 1040 | - if (ret > 0) | ||
| 1041 | - ret = read_reg_response(mb_param, data_dest, query); | 1041 | + if (ret > 0) { |
| 1042 | + int offset; | ||
| 1043 | + int i; | ||
| 1044 | + | ||
| 1045 | + ret = modbus_check_response(mb_param, query, response); | ||
| 1046 | + | ||
| 1047 | + offset = mb_param->header_length; | ||
| 1048 | + | ||
| 1049 | + /* If response_ret is negative, the loop is jumped ! */ | ||
| 1050 | + for (i = 0; i < ret; i++) { | ||
| 1051 | + /* shift reg hi_byte to temp OR with lo_byte */ | ||
| 1052 | + data_dest[i] = response[offset + 3 + (i << 1)] << 8 | | ||
| 1053 | + response[offset + 4 + (i << 1)]; | ||
| 1054 | + } | ||
| 1055 | + } | ||
| 1042 | 1056 | ||
| 1043 | return ret; | 1057 | return ret; |
| 1044 | } | 1058 | } |
| @@ -1080,30 +1094,6 @@ int read_input_registers(modbus_param_t *mb_param, int slave, | @@ -1080,30 +1094,6 @@ int read_input_registers(modbus_param_t *mb_param, int slave, | ||
| 1080 | return status; | 1094 | return status; |
| 1081 | } | 1095 | } |
| 1082 | 1096 | ||
| 1083 | -/* Reads the response data from a slave and puts the data into an | ||
| 1084 | - array */ | ||
| 1085 | -static int read_reg_response(modbus_param_t *mb_param, uint16_t *data_dest, | ||
| 1086 | - uint8_t *query) | ||
| 1087 | -{ | ||
| 1088 | - uint8_t response[MAX_MESSAGE_LENGTH]; | ||
| 1089 | - int response_ret; | ||
| 1090 | - int offset; | ||
| 1091 | - int i; | ||
| 1092 | - | ||
| 1093 | - response_ret = modbus_check_response(mb_param, query, response); | ||
| 1094 | - | ||
| 1095 | - offset = mb_param->header_length; | ||
| 1096 | - | ||
| 1097 | - /* If response_ret is negative, the loop is jumped ! */ | ||
| 1098 | - for (i = 0; i < response_ret; i++) { | ||
| 1099 | - /* shift reg hi_byte to temp OR with lo_byte */ | ||
| 1100 | - data_dest[i] = response[offset + 3 + (i << 1)] << 8 | | ||
| 1101 | - response[offset + 4 + (i << 1)]; | ||
| 1102 | - } | ||
| 1103 | - | ||
| 1104 | - return response_ret; | ||
| 1105 | -} | ||
| 1106 | - | ||
| 1107 | /* Gets the raw data from the input stream */ | 1097 | /* Gets the raw data from the input stream */ |
| 1108 | static int preset_response(modbus_param_t *mb_param, uint8_t *query) | 1098 | static int preset_response(modbus_param_t *mb_param, uint8_t *query) |
| 1109 | { | 1099 | { |