Commit 0a80018a648804bdccc885456b310971fe820847
1 parent
0ef574f6
Minor s/count/nb/ in modbus.[ch]
Showing
2 changed files
with
97 additions
and
97 deletions
modbus/modbus.c
| @@ -175,8 +175,8 @@ static unsigned int compute_response_length(modbus_param_t *mb_param, | @@ -175,8 +175,8 @@ static unsigned int compute_response_length(modbus_param_t *mb_param, | ||
| 175 | case FC_READ_COIL_STATUS: | 175 | case FC_READ_COIL_STATUS: |
| 176 | case FC_READ_INPUT_STATUS: { | 176 | case FC_READ_INPUT_STATUS: { |
| 177 | /* Header + nb values (code from force_multiple_coils) */ | 177 | /* Header + nb values (code from force_multiple_coils) */ |
| 178 | - int nb_points = (query[offset + 4] << 8) | query[offset + 5]; | ||
| 179 | - resp_length = 3 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0); | 178 | + int nb = (query[offset + 4] << 8) | query[offset + 5]; |
| 179 | + resp_length = 3 + (nb / 8) + ((nb % 8) ? 1 : 0); | ||
| 180 | } | 180 | } |
| 181 | break; | 181 | break; |
| 182 | case FC_READ_HOLDING_REGISTERS: | 182 | case FC_READ_HOLDING_REGISTERS: |
| @@ -199,22 +199,22 @@ static unsigned int compute_response_length(modbus_param_t *mb_param, | @@ -199,22 +199,22 @@ static unsigned int compute_response_length(modbus_param_t *mb_param, | ||
| 199 | 199 | ||
| 200 | /* Builds a RTU query header */ | 200 | /* Builds a RTU query header */ |
| 201 | static int build_query_basis_rtu(int slave, int function, | 201 | static int build_query_basis_rtu(int slave, int function, |
| 202 | - int start_addr, int count, | 202 | + int start_addr, int nb, |
| 203 | uint8_t *query) | 203 | uint8_t *query) |
| 204 | { | 204 | { |
| 205 | query[0] = slave; | 205 | query[0] = slave; |
| 206 | query[1] = function; | 206 | query[1] = function; |
| 207 | query[2] = start_addr >> 8; | 207 | query[2] = start_addr >> 8; |
| 208 | query[3] = start_addr & 0x00ff; | 208 | query[3] = start_addr & 0x00ff; |
| 209 | - query[4] = count >> 8; | ||
| 210 | - query[5] = count & 0x00ff; | 209 | + query[4] = nb >> 8; |
| 210 | + query[5] = nb & 0x00ff; | ||
| 211 | 211 | ||
| 212 | return PRESET_QUERY_LENGTH_RTU; | 212 | return PRESET_QUERY_LENGTH_RTU; |
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | /* Builds a TCP query header */ | 215 | /* Builds a TCP query header */ |
| 216 | static int build_query_basis_tcp(int slave, int function, | 216 | static int build_query_basis_tcp(int slave, int function, |
| 217 | - int start_addr, int count, | 217 | + int start_addr, int nb, |
| 218 | uint8_t *query) | 218 | uint8_t *query) |
| 219 | { | 219 | { |
| 220 | 220 | ||
| @@ -244,22 +244,22 @@ static int build_query_basis_tcp(int slave, int function, | @@ -244,22 +244,22 @@ static int build_query_basis_tcp(int slave, int function, | ||
| 244 | query[7] = function; | 244 | query[7] = function; |
| 245 | query[8] = start_addr >> 8; | 245 | query[8] = start_addr >> 8; |
| 246 | query[9] = start_addr & 0x00ff; | 246 | query[9] = start_addr & 0x00ff; |
| 247 | - query[10] = count >> 8; | ||
| 248 | - query[11] = count & 0x00ff; | 247 | + query[10] = nb >> 8; |
| 248 | + query[11] = nb & 0x00ff; | ||
| 249 | 249 | ||
| 250 | return PRESET_QUERY_LENGTH_TCP; | 250 | return PRESET_QUERY_LENGTH_TCP; |
| 251 | } | 251 | } |
| 252 | 252 | ||
| 253 | static int build_query_basis(modbus_param_t *mb_param, int slave, | 253 | static int build_query_basis(modbus_param_t *mb_param, int slave, |
| 254 | int function, int start_addr, | 254 | int function, int start_addr, |
| 255 | - int count, uint8_t *query) | 255 | + int nb, uint8_t *query) |
| 256 | { | 256 | { |
| 257 | if (mb_param->type_com == RTU) | 257 | if (mb_param->type_com == RTU) |
| 258 | return build_query_basis_rtu(slave, function, start_addr, | 258 | return build_query_basis_rtu(slave, function, start_addr, |
| 259 | - count, query); | 259 | + nb, query); |
| 260 | else | 260 | else |
| 261 | return build_query_basis_tcp(slave, function, start_addr, | 261 | return build_query_basis_tcp(slave, function, start_addr, |
| 262 | - count, query); | 262 | + nb, query); |
| 263 | } | 263 | } |
| 264 | 264 | ||
| 265 | /* Builds a RTU response header */ | 265 | /* Builds a RTU response header */ |
| @@ -688,7 +688,7 @@ static int modbus_check_response(modbus_param_t *mb_param, | @@ -688,7 +688,7 @@ static int modbus_check_response(modbus_param_t *mb_param, | ||
| 688 | return response_length; | 688 | return response_length; |
| 689 | } | 689 | } |
| 690 | 690 | ||
| 691 | -static int response_io_status(int address, int count, | 691 | +static int response_io_status(int address, int nb, |
| 692 | uint8_t *tab_io_status, | 692 | uint8_t *tab_io_status, |
| 693 | uint8_t *response, int offset) | 693 | uint8_t *response, int offset) |
| 694 | { | 694 | { |
| @@ -696,7 +696,7 @@ static int response_io_status(int address, int count, | @@ -696,7 +696,7 @@ static int response_io_status(int address, int count, | ||
| 696 | int byte = 0; | 696 | int byte = 0; |
| 697 | int i; | 697 | int i; |
| 698 | 698 | ||
| 699 | - for (i = address; i < address+count; i++) { | 699 | + for (i = address; i < address+nb; i++) { |
| 700 | byte |= tab_io_status[i] << shift; | 700 | byte |= tab_io_status[i] << shift; |
| 701 | if (shift == 7) { | 701 | if (shift == 7) { |
| 702 | /* Byte is full */ | 702 | /* Byte is full */ |
| @@ -753,17 +753,17 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -753,17 +753,17 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 753 | 753 | ||
| 754 | switch (function) { | 754 | switch (function) { |
| 755 | case FC_READ_COIL_STATUS: { | 755 | case FC_READ_COIL_STATUS: { |
| 756 | - int count = (query[offset+4] << 8) + query[offset+5]; | 756 | + int nb = (query[offset+4] << 8) + query[offset+5]; |
| 757 | 757 | ||
| 758 | - if ((address + count) > mb_mapping->nb_coil_status) { | 758 | + if ((address + nb) > mb_mapping->nb_coil_status) { |
| 759 | printf("Illegal data address %0X in read_coil_status\n", | 759 | printf("Illegal data address %0X in read_coil_status\n", |
| 760 | - address + count); | 760 | + address + nb); |
| 761 | resp_length = response_exception(mb_param, &sft, | 761 | resp_length = response_exception(mb_param, &sft, |
| 762 | ILLEGAL_DATA_ADDRESS, response); | 762 | ILLEGAL_DATA_ADDRESS, response); |
| 763 | } else { | 763 | } else { |
| 764 | resp_length = build_response_basis(mb_param, &sft, response); | 764 | resp_length = build_response_basis(mb_param, &sft, response); |
| 765 | - response[resp_length++] = (count / 8) + ((count % 8) ? 1 : 0); | ||
| 766 | - resp_length = response_io_status(address, count, | 765 | + response[resp_length++] = (nb / 8) + ((nb % 8) ? 1 : 0); |
| 766 | + resp_length = response_io_status(address, nb, | ||
| 767 | mb_mapping->tab_coil_status, | 767 | mb_mapping->tab_coil_status, |
| 768 | response, resp_length); | 768 | response, resp_length); |
| 769 | } | 769 | } |
| @@ -771,36 +771,36 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -771,36 +771,36 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 771 | break; | 771 | break; |
| 772 | case FC_READ_INPUT_STATUS: { | 772 | case FC_READ_INPUT_STATUS: { |
| 773 | /* Similar to coil status */ | 773 | /* Similar to coil status */ |
| 774 | - int count = (query[offset+4] << 8) + query[offset+5]; | 774 | + int nb = (query[offset+4] << 8) + query[offset+5]; |
| 775 | 775 | ||
| 776 | - if ((address + count) > mb_mapping->nb_input_status) { | 776 | + if ((address + nb) > mb_mapping->nb_input_status) { |
| 777 | printf("Illegal data address %0X in read_input_status\n", | 777 | printf("Illegal data address %0X in read_input_status\n", |
| 778 | - address + count); | 778 | + address + nb); |
| 779 | resp_length = response_exception(mb_param, &sft, | 779 | resp_length = response_exception(mb_param, &sft, |
| 780 | ILLEGAL_DATA_ADDRESS, response); | 780 | ILLEGAL_DATA_ADDRESS, response); |
| 781 | } else { | 781 | } else { |
| 782 | resp_length = build_response_basis(mb_param, &sft, response); | 782 | resp_length = build_response_basis(mb_param, &sft, response); |
| 783 | - response[resp_length++] = (count / 8) + ((count % 8) ? 1 : 0); | ||
| 784 | - resp_length = response_io_status(address, count, | 783 | + response[resp_length++] = (nb / 8) + ((nb % 8) ? 1 : 0); |
| 784 | + resp_length = response_io_status(address, nb, | ||
| 785 | mb_mapping->tab_input_status, | 785 | mb_mapping->tab_input_status, |
| 786 | response, resp_length); | 786 | response, resp_length); |
| 787 | } | 787 | } |
| 788 | } | 788 | } |
| 789 | break; | 789 | break; |
| 790 | case FC_READ_HOLDING_REGISTERS: { | 790 | case FC_READ_HOLDING_REGISTERS: { |
| 791 | - int count = (query[offset+4] << 8) + query[offset+5]; | 791 | + int nb = (query[offset+4] << 8) + query[offset+5]; |
| 792 | 792 | ||
| 793 | - if ((address + count) > mb_mapping->nb_holding_registers) { | 793 | + if ((address + nb) > mb_mapping->nb_holding_registers) { |
| 794 | printf("Illegal data address %0X in read_holding_registers\n", | 794 | printf("Illegal data address %0X in read_holding_registers\n", |
| 795 | - address + count); | 795 | + address + nb); |
| 796 | resp_length = response_exception(mb_param, &sft, | 796 | resp_length = response_exception(mb_param, &sft, |
| 797 | ILLEGAL_DATA_ADDRESS, response); | 797 | ILLEGAL_DATA_ADDRESS, response); |
| 798 | } else { | 798 | } else { |
| 799 | int i; | 799 | int i; |
| 800 | 800 | ||
| 801 | resp_length = build_response_basis(mb_param, &sft, response); | 801 | resp_length = build_response_basis(mb_param, &sft, response); |
| 802 | - response[resp_length++] = count << 1; | ||
| 803 | - for (i = address; i < address + count; i++) { | 802 | + response[resp_length++] = nb << 1; |
| 803 | + for (i = address; i < address + nb; i++) { | ||
| 804 | response[resp_length++] = mb_mapping->tab_holding_registers[i] >> 8; | 804 | response[resp_length++] = mb_mapping->tab_holding_registers[i] >> 8; |
| 805 | response[resp_length++] = mb_mapping->tab_holding_registers[i] & 0xFF; | 805 | response[resp_length++] = mb_mapping->tab_holding_registers[i] & 0xFF; |
| 806 | } | 806 | } |
| @@ -809,19 +809,19 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -809,19 +809,19 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 809 | break; | 809 | break; |
| 810 | case FC_READ_INPUT_REGISTERS: { | 810 | case FC_READ_INPUT_REGISTERS: { |
| 811 | /* Similar to holding registers */ | 811 | /* Similar to holding registers */ |
| 812 | - int count = (query[offset+4] << 8) + query[offset+5]; | 812 | + int nb = (query[offset+4] << 8) + query[offset+5]; |
| 813 | 813 | ||
| 814 | - if ((address + count) > mb_mapping->nb_input_registers) { | 814 | + if ((address + nb) > mb_mapping->nb_input_registers) { |
| 815 | printf("Illegal data address %0X in read_input_registers\n", | 815 | printf("Illegal data address %0X in read_input_registers\n", |
| 816 | - address + count); | 816 | + address + nb); |
| 817 | resp_length = response_exception(mb_param, &sft, | 817 | resp_length = response_exception(mb_param, &sft, |
| 818 | ILLEGAL_DATA_ADDRESS, response); | 818 | ILLEGAL_DATA_ADDRESS, response); |
| 819 | } else { | 819 | } else { |
| 820 | int i; | 820 | int i; |
| 821 | 821 | ||
| 822 | resp_length = build_response_basis(mb_param, &sft, response); | 822 | resp_length = build_response_basis(mb_param, &sft, response); |
| 823 | - response[resp_length++] = count << 1; | ||
| 824 | - for (i = address; i < address + count; i++) { | 823 | + response[resp_length++] = nb << 1; |
| 824 | + for (i = address; i < address + nb; i++) { | ||
| 825 | response[resp_length++] = mb_mapping->tab_input_registers[i] >> 8; | 825 | response[resp_length++] = mb_mapping->tab_input_registers[i] >> 8; |
| 826 | response[resp_length++] = mb_mapping->tab_input_registers[i] & 0xFF; | 826 | response[resp_length++] = mb_mapping->tab_input_registers[i] & 0xFF; |
| 827 | } | 827 | } |
| @@ -865,11 +865,11 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -865,11 +865,11 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 865 | } | 865 | } |
| 866 | break; | 866 | break; |
| 867 | case FC_FORCE_MULTIPLE_COILS: { | 867 | case FC_FORCE_MULTIPLE_COILS: { |
| 868 | - int count = (query[offset+4] << 8) + query[offset+5]; | 868 | + int nb = (query[offset+4] << 8) + query[offset+5]; |
| 869 | 869 | ||
| 870 | - if ((address + count) > mb_mapping->nb_coil_status) { | 870 | + if ((address + nb) > mb_mapping->nb_coil_status) { |
| 871 | printf("Illegal data address %0X in force_multiple_coils\n", | 871 | printf("Illegal data address %0X in force_multiple_coils\n", |
| 872 | - address + count); | 872 | + address + nb); |
| 873 | resp_length = response_exception(mb_param, &sft, | 873 | resp_length = response_exception(mb_param, &sft, |
| 874 | ILLEGAL_DATA_ADDRESS, response); | 874 | ILLEGAL_DATA_ADDRESS, response); |
| 875 | } else { | 875 | } else { |
| @@ -881,11 +881,11 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -881,11 +881,11 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 881 | } | 881 | } |
| 882 | break; | 882 | break; |
| 883 | case FC_PRESET_MULTIPLE_REGISTERS: { | 883 | case FC_PRESET_MULTIPLE_REGISTERS: { |
| 884 | - int count = (query[offset+4] << 8) + query[offset+5]; | 884 | + int nb = (query[offset+4] << 8) + query[offset+5]; |
| 885 | 885 | ||
| 886 | - if ((address + count) > mb_mapping->nb_holding_registers) { | 886 | + if ((address + nb) > mb_mapping->nb_holding_registers) { |
| 887 | printf("Illegal data address %0X in preset_multiple_registers\n", | 887 | printf("Illegal data address %0X in preset_multiple_registers\n", |
| 888 | - address + count); | 888 | + address + nb); |
| 889 | resp_length = response_exception(mb_param, &sft, | 889 | resp_length = response_exception(mb_param, &sft, |
| 890 | ILLEGAL_DATA_ADDRESS, response); | 890 | ILLEGAL_DATA_ADDRESS, response); |
| 891 | } else { | 891 | } else { |
| @@ -924,7 +924,7 @@ int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length) | @@ -924,7 +924,7 @@ int modbus_listen(modbus_param_t *mb_param, uint8_t *query, int *query_length) | ||
| 924 | 924 | ||
| 925 | /* Reads IO status */ | 925 | /* Reads IO status */ |
| 926 | static int read_io_status(modbus_param_t *mb_param, int slave, int function, | 926 | static int read_io_status(modbus_param_t *mb_param, int slave, int function, |
| 927 | - int start_addr, int count, uint8_t *data_dest) | 927 | + int start_addr, int nb, uint8_t *data_dest) |
| 928 | { | 928 | { |
| 929 | int query_length; | 929 | int query_length; |
| 930 | int query_ret; | 930 | int query_ret; |
| @@ -934,7 +934,7 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function, | @@ -934,7 +934,7 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function, | ||
| 934 | uint8_t response[MAX_MESSAGE_LENGTH]; | 934 | uint8_t response[MAX_MESSAGE_LENGTH]; |
| 935 | 935 | ||
| 936 | query_length = build_query_basis(mb_param, slave, function, | 936 | query_length = build_query_basis(mb_param, slave, function, |
| 937 | - start_addr, count, query); | 937 | + start_addr, nb, query); |
| 938 | 938 | ||
| 939 | query_ret = modbus_send(mb_param, query, query_length); | 939 | query_ret = modbus_send(mb_param, query, query_length); |
| 940 | if (query_ret > 0) { | 940 | if (query_ret > 0) { |
| @@ -952,7 +952,7 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function, | @@ -952,7 +952,7 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function, | ||
| 952 | /* Shift reg hi_byte to temp */ | 952 | /* Shift reg hi_byte to temp */ |
| 953 | temp = response[3 + i]; | 953 | temp = response[3 + i]; |
| 954 | 954 | ||
| 955 | - for (bit = 0x01; (bit & 0xff) && (processed < count);) { | 955 | + for (bit = 0x01; (bit & 0xff) && (processed < nb);) { |
| 956 | data_dest[pos++] = (temp & bit) ? TRUE : FALSE; | 956 | data_dest[pos++] = (temp & bit) ? TRUE : FALSE; |
| 957 | processed++; | 957 | processed++; |
| 958 | bit = bit << 1; | 958 | bit = bit << 1; |
| @@ -969,21 +969,21 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function, | @@ -969,21 +969,21 @@ static int read_io_status(modbus_param_t *mb_param, int slave, int function, | ||
| 969 | /* Reads the boolean status of coils and sets the array elements | 969 | /* Reads the boolean status of coils and sets the array elements |
| 970 | in the destination to TRUE or FALSE. */ | 970 | in the destination to TRUE or FALSE. */ |
| 971 | int read_coil_status(modbus_param_t *mb_param, int slave, int start_addr, | 971 | int read_coil_status(modbus_param_t *mb_param, int slave, int start_addr, |
| 972 | - int count, uint8_t *data_dest) | 972 | + int nb, uint8_t *data_dest) |
| 973 | { | 973 | { |
| 974 | int status; | 974 | int status; |
| 975 | 975 | ||
| 976 | - if (count > MAX_STATUS) { | 976 | + if (nb > MAX_STATUS) { |
| 977 | printf("ERROR Too many coils status requested (%d > %d)\n", | 977 | printf("ERROR Too many coils status requested (%d > %d)\n", |
| 978 | - count, MAX_STATUS); | 978 | + nb, MAX_STATUS); |
| 979 | return TOO_MANY_DATA; | 979 | return TOO_MANY_DATA; |
| 980 | } | 980 | } |
| 981 | 981 | ||
| 982 | status = read_io_status(mb_param, slave, FC_READ_COIL_STATUS, | 982 | status = read_io_status(mb_param, slave, FC_READ_COIL_STATUS, |
| 983 | - start_addr, count, data_dest); | 983 | + start_addr, nb, data_dest); |
| 984 | 984 | ||
| 985 | if (status > 0) | 985 | if (status > 0) |
| 986 | - status = count; | 986 | + status = nb; |
| 987 | 987 | ||
| 988 | return status; | 988 | return status; |
| 989 | } | 989 | } |
| @@ -991,42 +991,42 @@ int read_coil_status(modbus_param_t *mb_param, int slave, int start_addr, | @@ -991,42 +991,42 @@ int read_coil_status(modbus_param_t *mb_param, int slave, int start_addr, | ||
| 991 | 991 | ||
| 992 | /* Same as read_coil_status but reads the slaves input table */ | 992 | /* Same as read_coil_status but reads the slaves input table */ |
| 993 | int read_input_status(modbus_param_t *mb_param, int slave, int start_addr, | 993 | int read_input_status(modbus_param_t *mb_param, int slave, int start_addr, |
| 994 | - int count, uint8_t *data_dest) | 994 | + int nb, uint8_t *data_dest) |
| 995 | { | 995 | { |
| 996 | int status; | 996 | int status; |
| 997 | 997 | ||
| 998 | - if (count > MAX_STATUS) { | ||
| 999 | - printf("ERROR Too many inputs status requested (%d > %d)\n", | ||
| 1000 | - count, MAX_STATUS); | 998 | + if (nb > MAX_STATUS) { |
| 999 | + printf("ERROR Too many input status requested (%d > %d)\n", | ||
| 1000 | + nb, MAX_STATUS); | ||
| 1001 | return TOO_MANY_DATA; | 1001 | return TOO_MANY_DATA; |
| 1002 | } | 1002 | } |
| 1003 | 1003 | ||
| 1004 | status = read_io_status(mb_param, slave, FC_READ_INPUT_STATUS, | 1004 | status = read_io_status(mb_param, slave, FC_READ_INPUT_STATUS, |
| 1005 | - start_addr, count, data_dest); | 1005 | + start_addr, nb, data_dest); |
| 1006 | 1006 | ||
| 1007 | if (status > 0) | 1007 | if (status > 0) |
| 1008 | - status = count; | 1008 | + status = nb; |
| 1009 | 1009 | ||
| 1010 | return status; | 1010 | return status; |
| 1011 | } | 1011 | } |
| 1012 | 1012 | ||
| 1013 | /* Reads the data from a modbus slave and put that data into an array */ | 1013 | /* Reads the data from a modbus slave and put that data into an array */ |
| 1014 | static int read_registers(modbus_param_t *mb_param, int slave, int function, | 1014 | static int read_registers(modbus_param_t *mb_param, int slave, int function, |
| 1015 | - int start_addr, int count, uint16_t *data_dest) | 1015 | + int start_addr, int nb, uint16_t *data_dest) |
| 1016 | { | 1016 | { |
| 1017 | int query_length; | 1017 | int query_length; |
| 1018 | int status; | 1018 | int status; |
| 1019 | int query_ret; | 1019 | int query_ret; |
| 1020 | uint8_t query[MIN_QUERY_LENGTH]; | 1020 | uint8_t query[MIN_QUERY_LENGTH]; |
| 1021 | 1021 | ||
| 1022 | - if (count > MAX_REGISTERS) { | 1022 | + if (nb > MAX_REGISTERS) { |
| 1023 | printf("EROOR Too many holding registers requested (%d > %d)\n", | 1023 | printf("EROOR Too many holding registers requested (%d > %d)\n", |
| 1024 | - count, MAX_REGISTERS); | 1024 | + nb, MAX_REGISTERS); |
| 1025 | return TOO_MANY_DATA; | 1025 | return TOO_MANY_DATA; |
| 1026 | } | 1026 | } |
| 1027 | 1027 | ||
| 1028 | query_length = build_query_basis(mb_param, slave, function, | 1028 | query_length = build_query_basis(mb_param, slave, function, |
| 1029 | - start_addr, count, query); | 1029 | + start_addr, nb, query); |
| 1030 | 1030 | ||
| 1031 | query_ret = modbus_send(mb_param, query, query_length); | 1031 | query_ret = modbus_send(mb_param, query, query_length); |
| 1032 | if (query_ret > 0) | 1032 | if (query_ret > 0) |
| @@ -1040,36 +1040,36 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, | @@ -1040,36 +1040,36 @@ static int read_registers(modbus_param_t *mb_param, int slave, int function, | ||
| 1040 | /* Reads the holding registers in a slave and put the data into an | 1040 | /* Reads the holding registers in a slave and put the data into an |
| 1041 | array */ | 1041 | array */ |
| 1042 | int read_holding_registers(modbus_param_t *mb_param, int slave, | 1042 | int read_holding_registers(modbus_param_t *mb_param, int slave, |
| 1043 | - int start_addr, int count, uint16_t *data_dest) | 1043 | + int start_addr, int nb, uint16_t *data_dest) |
| 1044 | { | 1044 | { |
| 1045 | int status; | 1045 | int status; |
| 1046 | 1046 | ||
| 1047 | - if (count > MAX_REGISTERS) { | 1047 | + if (nb > MAX_REGISTERS) { |
| 1048 | printf("ERROR Too many holding registers requested (%d > %d)\n", | 1048 | printf("ERROR Too many holding registers requested (%d > %d)\n", |
| 1049 | - count, MAX_REGISTERS); | 1049 | + nb, MAX_REGISTERS); |
| 1050 | return TOO_MANY_DATA; | 1050 | return TOO_MANY_DATA; |
| 1051 | } | 1051 | } |
| 1052 | 1052 | ||
| 1053 | status = read_registers(mb_param, slave, FC_READ_HOLDING_REGISTERS, | 1053 | status = read_registers(mb_param, slave, FC_READ_HOLDING_REGISTERS, |
| 1054 | - start_addr, count, data_dest); | 1054 | + start_addr, nb, data_dest); |
| 1055 | return status; | 1055 | return status; |
| 1056 | } | 1056 | } |
| 1057 | 1057 | ||
| 1058 | /* Reads the input registers in a slave and put the data into | 1058 | /* Reads the input registers in a slave and put the data into |
| 1059 | an array */ | 1059 | an array */ |
| 1060 | int read_input_registers(modbus_param_t *mb_param, int slave, | 1060 | int read_input_registers(modbus_param_t *mb_param, int slave, |
| 1061 | - int start_addr, int count, uint16_t *data_dest) | 1061 | + int start_addr, int nb, uint16_t *data_dest) |
| 1062 | { | 1062 | { |
| 1063 | int status; | 1063 | int status; |
| 1064 | 1064 | ||
| 1065 | - if (count > MAX_REGISTERS) { | 1065 | + if (nb > MAX_REGISTERS) { |
| 1066 | printf("ERROR Too many input registers requested (%d > %d)\n", | 1066 | printf("ERROR Too many input registers requested (%d > %d)\n", |
| 1067 | - count, MAX_REGISTERS); | 1067 | + nb, MAX_REGISTERS); |
| 1068 | return TOO_MANY_DATA; | 1068 | return TOO_MANY_DATA; |
| 1069 | } | 1069 | } |
| 1070 | 1070 | ||
| 1071 | status = read_registers(mb_param, slave, FC_READ_INPUT_REGISTERS, | 1071 | status = read_registers(mb_param, slave, FC_READ_INPUT_REGISTERS, |
| 1072 | - start_addr, count, data_dest); | 1072 | + start_addr, nb, data_dest); |
| 1073 | 1073 | ||
| 1074 | return status; | 1074 | return status; |
| 1075 | } | 1075 | } |
| @@ -1160,7 +1160,7 @@ int preset_single_register(modbus_param_t *mb_param, int slave, | @@ -1160,7 +1160,7 @@ int preset_single_register(modbus_param_t *mb_param, int slave, | ||
| 1160 | 1160 | ||
| 1161 | /* Sets/resets the coils in the slave from an array in argument */ | 1161 | /* Sets/resets the coils in the slave from an array in argument */ |
| 1162 | int force_multiple_coils(modbus_param_t *mb_param, int slave, | 1162 | int force_multiple_coils(modbus_param_t *mb_param, int slave, |
| 1163 | - int start_addr, int nb_points, | 1163 | + int start_addr, int nb, |
| 1164 | uint8_t *data_src) | 1164 | uint8_t *data_src) |
| 1165 | { | 1165 | { |
| 1166 | int i; | 1166 | int i; |
| @@ -1173,16 +1173,16 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, | @@ -1173,16 +1173,16 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, | ||
| 1173 | 1173 | ||
| 1174 | uint8_t query[MAX_MESSAGE_LENGTH]; | 1174 | uint8_t query[MAX_MESSAGE_LENGTH]; |
| 1175 | 1175 | ||
| 1176 | - if (nb_points > MAX_STATUS) { | 1176 | + if (nb > MAX_STATUS) { |
| 1177 | printf("ERROR Writing to too many coils (%d > %d)\n", | 1177 | printf("ERROR Writing to too many coils (%d > %d)\n", |
| 1178 | - nb_points, MAX_STATUS); | 1178 | + nb, MAX_STATUS); |
| 1179 | return TOO_MANY_DATA; | 1179 | return TOO_MANY_DATA; |
| 1180 | } | 1180 | } |
| 1181 | 1181 | ||
| 1182 | query_length = build_query_basis(mb_param, slave, | 1182 | query_length = build_query_basis(mb_param, slave, |
| 1183 | FC_FORCE_MULTIPLE_COILS, | 1183 | FC_FORCE_MULTIPLE_COILS, |
| 1184 | - start_addr, nb_points, query); | ||
| 1185 | - byte_count = (nb_points / 8) + ((nb_points % 8) ? 1 : 0); | 1184 | + start_addr, nb, query); |
| 1185 | + byte_count = (nb / 8) + ((nb % 8) ? 1 : 0); | ||
| 1186 | query[query_length++] = byte_count; | 1186 | query[query_length++] = byte_count; |
| 1187 | 1187 | ||
| 1188 | for (i = 0; i < byte_count; i++) { | 1188 | for (i = 0; i < byte_count; i++) { |
| @@ -1191,7 +1191,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, | @@ -1191,7 +1191,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, | ||
| 1191 | bit = 0x01; | 1191 | bit = 0x01; |
| 1192 | query[query_length] = 0; | 1192 | query[query_length] = 0; |
| 1193 | 1193 | ||
| 1194 | - while ((bit & 0xFF) && (coil_check++ < nb_points)) { | 1194 | + while ((bit & 0xFF) && (coil_check++ < nb)) { |
| 1195 | if (data_src[pos++]) | 1195 | if (data_src[pos++]) |
| 1196 | query[query_length] |= bit; | 1196 | query[query_length] |= bit; |
| 1197 | else | 1197 | else |
| @@ -1213,7 +1213,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, | @@ -1213,7 +1213,7 @@ int force_multiple_coils(modbus_param_t *mb_param, int slave, | ||
| 1213 | 1213 | ||
| 1214 | /* Copies the values in the slave from the array given in argument */ | 1214 | /* Copies the values in the slave from the array given in argument */ |
| 1215 | int preset_multiple_registers(modbus_param_t *mb_param, int slave, | 1215 | int preset_multiple_registers(modbus_param_t *mb_param, int slave, |
| 1216 | - int start_addr, int nb_points, uint16_t *data_src) | 1216 | + int start_addr, int nb, uint16_t *data_src) |
| 1217 | { | 1217 | { |
| 1218 | int i; | 1218 | int i; |
| 1219 | int query_length; | 1219 | int query_length; |
| @@ -1223,19 +1223,19 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, | @@ -1223,19 +1223,19 @@ int preset_multiple_registers(modbus_param_t *mb_param, int slave, | ||
| 1223 | 1223 | ||
| 1224 | uint8_t query[MAX_MESSAGE_LENGTH]; | 1224 | uint8_t query[MAX_MESSAGE_LENGTH]; |
| 1225 | 1225 | ||
| 1226 | - if (nb_points > MAX_REGISTERS) { | 1226 | + if (nb > MAX_REGISTERS) { |
| 1227 | printf("ERROR Trying to write to too many registers (%d > %d)\n", | 1227 | printf("ERROR Trying to write to too many registers (%d > %d)\n", |
| 1228 | - nb_points, MAX_REGISTERS); | 1228 | + nb, MAX_REGISTERS); |
| 1229 | return TOO_MANY_DATA; | 1229 | return TOO_MANY_DATA; |
| 1230 | } | 1230 | } |
| 1231 | 1231 | ||
| 1232 | query_length = build_query_basis(mb_param, slave, | 1232 | query_length = build_query_basis(mb_param, slave, |
| 1233 | FC_PRESET_MULTIPLE_REGISTERS, | 1233 | FC_PRESET_MULTIPLE_REGISTERS, |
| 1234 | - start_addr, nb_points, query); | ||
| 1235 | - byte_count = nb_points * 2; | 1234 | + start_addr, nb, query); |
| 1235 | + byte_count = nb * 2; | ||
| 1236 | query[query_length++] = byte_count; | 1236 | query[query_length++] = byte_count; |
| 1237 | 1237 | ||
| 1238 | - for (i = 0; i < nb_points; i++) { | 1238 | + for (i = 0; i < nb; i++) { |
| 1239 | query[query_length++] = data_src[i] >> 8; | 1239 | query[query_length++] = data_src[i] >> 8; |
| 1240 | query[query_length++] = data_src[i] & 0x00FF; | 1240 | query[query_length++] = data_src[i] & 0x00FF; |
| 1241 | } | 1241 | } |
| @@ -1827,8 +1827,8 @@ int modbus_init_listen_tcp(modbus_param_t *mb_param) | @@ -1827,8 +1827,8 @@ int modbus_init_listen_tcp(modbus_param_t *mb_param) | ||
| 1827 | 1827 | ||
| 1828 | /** Utils **/ | 1828 | /** Utils **/ |
| 1829 | 1829 | ||
| 1830 | -/* Sets many inputs/coils from a single byte value (all 8 bits of the | ||
| 1831 | - byte value are setted) */ | 1830 | +/* Sets many input/coil status from a single byte value (all 8 bits of |
| 1831 | + the byte value are setted) */ | ||
| 1832 | void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value) | 1832 | void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value) |
| 1833 | { | 1833 | { |
| 1834 | int i; | 1834 | int i; |
| @@ -1838,15 +1838,15 @@ void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value) | @@ -1838,15 +1838,15 @@ void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value) | ||
| 1838 | } | 1838 | } |
| 1839 | } | 1839 | } |
| 1840 | 1840 | ||
| 1841 | -/* Sets many inputs/coils from a table of bytes (only the bits between | ||
| 1842 | - address and address + nb_points are setted) */ | ||
| 1843 | -void set_bits_from_bytes(uint8_t *dest, int address, int nb_points, | 1841 | +/* Sets many input/coil status from a table of bytes (only the bits |
| 1842 | + between address and address + nb_bits are setted) */ | ||
| 1843 | +void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits, | ||
| 1844 | const uint8_t tab_byte[]) | 1844 | const uint8_t tab_byte[]) |
| 1845 | { | 1845 | { |
| 1846 | int i; | 1846 | int i; |
| 1847 | int shift = 0; | 1847 | int shift = 0; |
| 1848 | 1848 | ||
| 1849 | - for (i=address; i < address + nb_points; i++) { | 1849 | + for (i = address; i < address + nb_bits; i++) { |
| 1850 | dest[i] = tab_byte[(i - address) / 8] & (1 << shift) ? ON : OFF; | 1850 | dest[i] = tab_byte[(i - address) / 8] & (1 << shift) ? ON : OFF; |
| 1851 | /* gcc doesn't like: shift = (++shift) % 8; */ | 1851 | /* gcc doesn't like: shift = (++shift) % 8; */ |
| 1852 | shift++; | 1852 | shift++; |
| @@ -1854,8 +1854,8 @@ void set_bits_from_bytes(uint8_t *dest, int address, int nb_points, | @@ -1854,8 +1854,8 @@ void set_bits_from_bytes(uint8_t *dest, int address, int nb_points, | ||
| 1854 | } | 1854 | } |
| 1855 | } | 1855 | } |
| 1856 | 1856 | ||
| 1857 | -/* Gets the byte value from many inputs/coils. | ||
| 1858 | - To obtain a full byte, set nb_points to 8. */ | 1857 | +/* Gets the byte value from many input/coil status. |
| 1858 | + To obtain a full byte, set nb_bits to 8. */ | ||
| 1859 | uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) | 1859 | uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) |
| 1860 | { | 1860 | { |
| 1861 | int i; | 1861 | int i; |
| @@ -1866,7 +1866,7 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) | @@ -1866,7 +1866,7 @@ uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits) | ||
| 1866 | nb_bits = 8; | 1866 | nb_bits = 8; |
| 1867 | } | 1867 | } |
| 1868 | 1868 | ||
| 1869 | - for (i=0; i<nb_bits; i++) { | 1869 | + for (i=0; i < nb_bits; i++) { |
| 1870 | value |= (src[address+i] << i); | 1870 | value |= (src[address+i] << i); |
| 1871 | } | 1871 | } |
| 1872 | 1872 |
modbus/modbus.h
| @@ -171,21 +171,21 @@ typedef struct { | @@ -171,21 +171,21 @@ typedef struct { | ||
| 171 | /* Reads the boolean status of coils and sets the array elements in | 171 | /* Reads the boolean status of coils and sets the array elements in |
| 172 | the destination to TRUE or FALSE */ | 172 | the destination to TRUE or FALSE */ |
| 173 | int read_coil_status(modbus_param_t *mb_param, int slave, | 173 | int read_coil_status(modbus_param_t *mb_param, int slave, |
| 174 | - int start_addr, int count, uint8_t *dest); | 174 | + int start_addr, int nb, uint8_t *dest); |
| 175 | 175 | ||
| 176 | /* Same as read_coil_status but reads the slaves input table */ | 176 | /* Same as read_coil_status but reads the slaves input table */ |
| 177 | int read_input_status(modbus_param_t *mb_param, int slave, | 177 | int read_input_status(modbus_param_t *mb_param, int slave, |
| 178 | - int start_addr, int count, uint8_t *dest); | 178 | + int start_addr, int nb, uint8_t *dest); |
| 179 | 179 | ||
| 180 | /* Reads the holding registers in a slave and put the data into an | 180 | /* Reads the holding registers in a slave and put the data into an |
| 181 | array */ | 181 | array */ |
| 182 | int read_holding_registers(modbus_param_t *mb_param, int slave, | 182 | int read_holding_registers(modbus_param_t *mb_param, int slave, |
| 183 | - int start_addr, int count, uint16_t *dest); | 183 | + int start_addr, int nb, uint16_t *dest); |
| 184 | 184 | ||
| 185 | /* Reads the input registers in a slave and put the data into an | 185 | /* Reads the input registers in a slave and put the data into an |
| 186 | array */ | 186 | array */ |
| 187 | int read_input_registers(modbus_param_t *mb_param, int slave, | 187 | int read_input_registers(modbus_param_t *mb_param, int slave, |
| 188 | - int start_addr, int count, uint16_t *dest); | 188 | + int start_addr, int nb, uint16_t *dest); |
| 189 | 189 | ||
| 190 | /* Turns ON or OFF a single coil in the slave device */ | 190 | /* Turns ON or OFF a single coil in the slave device */ |
| 191 | int force_single_coil(modbus_param_t *mb_param, int slave, | 191 | int force_single_coil(modbus_param_t *mb_param, int slave, |
| @@ -197,11 +197,11 @@ int preset_single_register(modbus_param_t *mb_param, int slave, | @@ -197,11 +197,11 @@ int preset_single_register(modbus_param_t *mb_param, int slave, | ||
| 197 | 197 | ||
| 198 | /* Sets/resets the coils in the slave from an array in argument */ | 198 | /* Sets/resets the coils in the slave from an array in argument */ |
| 199 | int force_multiple_coils(modbus_param_t *mb_param, int slave, | 199 | int force_multiple_coils(modbus_param_t *mb_param, int slave, |
| 200 | - int start_addr, int nb_points, uint8_t *data); | 200 | + int start_addr, int nb, uint8_t *data); |
| 201 | 201 | ||
| 202 | /* Copies the values in the slave from the array given in argument */ | 202 | /* Copies the values in the slave from the array given in argument */ |
| 203 | int preset_multiple_registers(modbus_param_t *mb_param, int slave, | 203 | int preset_multiple_registers(modbus_param_t *mb_param, int slave, |
| 204 | - int start_addr, int nb_points, uint16_t *data); | 204 | + int start_addr, int nb, uint16_t *data); |
| 205 | 205 | ||
| 206 | /* Returns the slave id! */ | 206 | /* Returns the slave id! */ |
| 207 | int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest); | 207 | int report_slave_id(modbus_param_t *mb_param, int slave, uint8_t *dest); |
| @@ -289,16 +289,16 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | @@ -289,16 +289,16 @@ void manage_query(modbus_param_t *mb_param, uint8_t *query, | ||
| 289 | * UTILS FUNCTIONS | 289 | * UTILS FUNCTIONS |
| 290 | **/ | 290 | **/ |
| 291 | 291 | ||
| 292 | -/* Sets many inputs/coils from a single byte value (all 8 bits of the | ||
| 293 | - byte value are setted) */ | 292 | +/* Sets many input/coil status from a single byte value (all 8 bits of |
| 293 | + the byte value are setted) */ | ||
| 294 | void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value); | 294 | void set_bits_from_byte(uint8_t *dest, int address, const uint8_t value); |
| 295 | 295 | ||
| 296 | -/* Sets many inputs/coils from a table of bytes (only the bits between | ||
| 297 | - address and address + nb_bits are setted) */ | 296 | +/* Sets many input/coil status from a table of bytes (only the bits |
| 297 | + between address and address + nb_bits are setted) */ | ||
| 298 | void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits, | 298 | void set_bits_from_bytes(uint8_t *dest, int address, int nb_bits, |
| 299 | const uint8_t *tab_byte); | 299 | const uint8_t *tab_byte); |
| 300 | 300 | ||
| 301 | -/* Gets the byte value from many inputs/coils. | 301 | +/* Gets the byte value from many input/coil status. |
| 302 | To obtain a full byte, set nb_bits to 8. */ | 302 | To obtain a full byte, set nb_bits to 8. */ |
| 303 | uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits); | 303 | uint8_t get_byte_from_bits(const uint8_t *src, int address, int nb_bits); |
| 304 | 304 |