Commit 1d058c8e5e5cb268d5f75da26c8ebbc8aa703931
1 parent
fa4f566b
Major changes to test-master-random
- Use only addresses between address_start and address_end - Better memory usage
Showing
1 changed file
with
133 additions
and
128 deletions
tests/test-master-random.c
| ... | ... | @@ -35,25 +35,27 @@ |
| 35 | 35 | - read_holding_registers |
| 36 | 36 | |
| 37 | 37 | All these functions are called with random values on a address |
| 38 | - range defined by following defines. | |
| 39 | - | |
| 40 | - This program is also really useful to test your remote target unit. | |
| 38 | + range defined by the following defines. | |
| 41 | 39 | */ |
| 42 | -#define LOOP 1 | |
| 43 | -#define SLAVE 0x11 | |
| 44 | -#define ADDR_MIN 0 | |
| 45 | -#define ADDR_MAX 499 | |
| 46 | -#define FIELDS 500 | |
| 40 | +#define LOOP 1 | |
| 41 | +#define SLAVE 0x11 | |
| 42 | +#define ADDRESS_START 0 | |
| 43 | +#define ADDRESS_END 499 | |
| 47 | 44 | |
| 45 | +/* At each loop, the program works in the range ADDRESS_START to | |
| 46 | + * ADDRESS_END then ADDRESS_START + 1 to ADDRESS_END and so on. | |
| 47 | + */ | |
| 48 | 48 | int main(void) |
| 49 | 49 | { |
| 50 | - int ok, fail; | |
| 51 | - int loop_nb; | |
| 50 | + int ret; | |
| 51 | + int nb_fail; | |
| 52 | + int nb_loop; | |
| 52 | 53 | int addr; |
| 53 | - int field_nb; | |
| 54 | - int *tab_rq; | |
| 55 | - int *tab_rq_bits; | |
| 56 | - int *tab_rp; | |
| 54 | + int nb_points_total; | |
| 55 | + uint8_t *tab_rq_status; | |
| 56 | + uint8_t *tab_rp_status; | |
| 57 | + uint16_t *tab_rq_registers; | |
| 58 | + uint16_t *tab_rp_registers; | |
| 57 | 59 | modbus_param_t mb_param; |
| 58 | 60 | |
| 59 | 61 | /* RTU parity : none, even, odd */ |
| ... | ... | @@ -66,141 +68,144 @@ int main(void) |
| 66 | 68 | modbus_connect(&mb_param); |
| 67 | 69 | |
| 68 | 70 | /* Allocate and initialize the different memory spaces */ |
| 69 | - tab_rq = (int *) malloc(FIELDS * sizeof(int)); | |
| 70 | - memset(tab_rq, 0, FIELDS * sizeof(int)); | |
| 71 | - | |
| 72 | - tab_rq_bits = (int *) malloc(FIELDS * sizeof(int)); | |
| 73 | - memset(tab_rq_bits, 0, FIELDS * sizeof(int)); | |
| 74 | - | |
| 75 | - tab_rp = (int *) malloc(FIELDS * sizeof(int)); | |
| 76 | - memset(tab_rp, 0, FIELDS * sizeof(int)); | |
| 77 | - | |
| 78 | - loop_nb = ok = fail = 0; | |
| 79 | - while (loop_nb++ < LOOP) { | |
| 80 | - for (addr=ADDR_MIN; addr <= ADDR_MAX; addr++) { | |
| 81 | - for (field_nb=1; field_nb<=FIELDS; field_nb++) { | |
| 82 | - int i; | |
| 83 | - | |
| 84 | - /* Random numbers (short) */ | |
| 85 | - for (i=0; i<field_nb; i++) { | |
| 86 | - tab_rq[i] = (int) (16536.0*rand()/(RAND_MAX+1.0)); | |
| 87 | - tab_rq_bits[i] = (i) % 2; | |
| 71 | + nb_points_total = ADDRESS_END - ADDRESS_START; | |
| 72 | + | |
| 73 | + tab_rq_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t)); | |
| 74 | + memset(tab_rq_status, 0, nb_points_total * sizeof(uint8_t)); | |
| 75 | + tab_rp_status = (uint8_t *) malloc(nb_points_total * sizeof(uint8_t)); | |
| 76 | + memset(tab_rp_status, 0, nb_points_total * sizeof(uint8_t)); | |
| 77 | + | |
| 78 | + tab_rq_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t)); | |
| 79 | + memset(tab_rq_registers, 0, nb_points_total * sizeof(uint16_t)); | |
| 80 | + tab_rp_registers = (uint16_t *) malloc(nb_points_total * sizeof(uint16_t)); | |
| 81 | + memset(tab_rp_status, 0, nb_points_total * sizeof(uint16_t)); | |
| 82 | + | |
| 83 | + nb_loop = nb_fail = 0; | |
| 84 | + while (nb_loop++ < LOOP) { | |
| 85 | + for (addr = ADDRESS_START; addr <= ADDRESS_END; addr++) { | |
| 86 | + int i; | |
| 87 | + int nb_points; | |
| 88 | + | |
| 89 | + /* Random numbers (short) */ | |
| 90 | + for (i=0; i<nb_points_total; i++) { | |
| 91 | + tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0)); | |
| 92 | + tab_rq_status[i] = tab_rq_registers[i] % 2; | |
| 93 | + } | |
| 94 | + nb_points = ADDRESS_END - addr; | |
| 95 | + | |
| 96 | + /* SINGLE COIL */ | |
| 97 | + ret = force_single_coil(&mb_param, SLAVE, addr, tab_rq_status[0]); | |
| 98 | + if (ret != 1) { | |
| 99 | + printf("ERROR force_single_coil (%d)\n", ret); | |
| 100 | + printf("Slave = %d, address = %d, value = %d\n", | |
| 101 | + SLAVE, addr, tab_rq_status[0]); | |
| 102 | + nb_fail++; | |
| 103 | + } else { | |
| 104 | + ret = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp_status); | |
| 105 | + if (ret != 1 || tab_rq_status[0] != tab_rp_status[0]) { | |
| 106 | + printf("ERROR read_coil_status single (%d)\n", ret); | |
| 107 | + printf("Slave = %d, address = %d\n", | |
| 108 | + SLAVE, addr); | |
| 109 | + nb_fail++; | |
| 88 | 110 | } |
| 111 | + } | |
| 89 | 112 | |
| 90 | - /* SINGLE COIL */ | |
| 91 | - ok = force_single_coil(&mb_param, SLAVE, addr, tab_rq_bits[0]); | |
| 92 | - if (ok != 1) { | |
| 93 | - printf("ERROR force_single_coil (%d)\n", ok); | |
| 94 | - printf("Slave = %d, address = %d, value = %d\n", | |
| 95 | - SLAVE, addr, tab_rq_bits[0]); | |
| 96 | - fail++; | |
| 113 | + /* MULTIPLE COILS */ | |
| 114 | + ret = force_multiple_coils(&mb_param, SLAVE, addr, nb_points, tab_rq_status); | |
| 115 | + if (ret != nb_points) { | |
| 116 | + printf("ERROR force_multiple_coils (%d)\n", ret); | |
| 117 | + printf("Slave = %d, address = %d, nb_points = %d\n", | |
| 118 | + SLAVE, addr, nb_points); | |
| 119 | + nb_fail++; | |
| 120 | + } else { | |
| 121 | + ret = read_coil_status(&mb_param, SLAVE, addr, nb_points, tab_rp_status); | |
| 122 | + if (ret != nb_points) { | |
| 123 | + printf("ERROR read_coil_status\n"); | |
| 124 | + printf("Slave = %d, address = %d, nb_points = %d\n", | |
| 125 | + SLAVE, addr, nb_points); | |
| 126 | + nb_fail++; | |
| 97 | 127 | } else { |
| 98 | - ok = read_coil_status(&mb_param, SLAVE, addr, 1, tab_rp); | |
| 99 | - if (ok != 1 || tab_rq_bits[0] != tab_rp[0]) { | |
| 100 | - printf("ERROR read_coil_status single (%d)\n", ok); | |
| 101 | - printf("Slave = %d, address = %d\n", | |
| 102 | - SLAVE, addr); | |
| 103 | - fail++; | |
| 128 | + for (i=0; i<nb_points; i++) { | |
| 129 | + if (tab_rp_status[i] != tab_rq_status[i]) { | |
| 130 | + printf("ERROR read_coil_status "); | |
| 131 | + printf("(%d != %d)\n", tab_rp_status[i], tab_rq_status[i]); | |
| 132 | + printf("Slave = %d, address = %d\n", | |
| 133 | + SLAVE, addr); | |
| 134 | + nb_fail++; | |
| 135 | + } | |
| 104 | 136 | } |
| 105 | 137 | } |
| 138 | + } | |
| 106 | 139 | |
| 107 | - /* MULTIPLE COILS */ | |
| 108 | - ok = force_multiple_coils(&mb_param, SLAVE, addr, field_nb, tab_rq_bits); | |
| 109 | - if (ok != field_nb) { | |
| 110 | - printf("ERROR force_multiple_coils (%d)\n", ok); | |
| 111 | - printf("Slave = %d, address = %d, field_nb = %d\n", | |
| 112 | - SLAVE, addr, field_nb); | |
| 113 | - fail++; | |
| 140 | + /* SINGLE REGISTER */ | |
| 141 | + ret = preset_single_register(&mb_param, SLAVE, addr, tab_rq_registers[0]); | |
| 142 | + if (ret != 1) { | |
| 143 | + printf("ERROR preset_single_register (%d)\n", ret); | |
| 144 | + printf("Slave = %d, address = %d, value = %d\n", | |
| 145 | + SLAVE, addr, tab_rq_registers[0]); | |
| 146 | + nb_fail++; | |
| 147 | + } else { | |
| 148 | + ret = read_holding_registers(&mb_param, SLAVE, | |
| 149 | + addr, 1, tab_rp_registers); | |
| 150 | + if (ret != 1) { | |
| 151 | + printf("ERROR read_holding_registers single (%d)\n", ret); | |
| 152 | + printf("Slave = %d, address = %d\n", | |
| 153 | + SLAVE, addr); | |
| 154 | + nb_fail++; | |
| 114 | 155 | } else { |
| 115 | - ok = read_coil_status(&mb_param, SLAVE, addr, | |
| 116 | - field_nb, tab_rp); | |
| 117 | - if (ok != field_nb) { | |
| 118 | - printf("ERROR read_coil_status\n"); | |
| 119 | - printf("Slave = %d, address = %d, field_nb = %d\n", | |
| 120 | - SLAVE, addr, field_nb); | |
| 121 | - fail++; | |
| 122 | - } else { | |
| 123 | - for (i=0; i<field_nb; i++) { | |
| 124 | - if (tab_rp[i] != tab_rq_bits[i]) { | |
| 125 | - printf("ERROR read_coil_status "); | |
| 126 | - printf("(%d != %d)\n", tab_rp[i], tab_rq_bits[i]); | |
| 127 | - printf("Slave = %d, address = %d\n", | |
| 128 | - SLAVE, addr); | |
| 129 | - fail++; | |
| 130 | - } | |
| 131 | - } | |
| 156 | + if (tab_rq_registers[0] != tab_rp_registers[0]) { | |
| 157 | + printf("ERROR read_holding_registers single "); | |
| 158 | + printf("(%d != %d)\n", | |
| 159 | + tab_rq_registers[0], tab_rp_registers[0]); | |
| 160 | + printf("Slave = %d, address = %d\n", | |
| 161 | + SLAVE, addr); | |
| 162 | + nb_fail++; | |
| 132 | 163 | } |
| 133 | 164 | } |
| 134 | - | |
| 135 | - /* SINGLE REGISTER */ | |
| 136 | - ok = preset_single_register(&mb_param, SLAVE, addr, tab_rq[0]); | |
| 137 | - if (ok != 1) { | |
| 138 | - printf("ERROR preset_single_register (%d)\n", ok); | |
| 139 | - printf("Slave = %d, address = %d, value = %d\n", | |
| 140 | - SLAVE, addr, tab_rq[0]); | |
| 141 | - fail++; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /* MULTIPLE REGISTERS */ | |
| 168 | + ret = preset_multiple_registers(&mb_param, SLAVE, | |
| 169 | + addr, nb_points, tab_rq_registers); | |
| 170 | + if (ret != nb_points) { | |
| 171 | + printf("ERROR preset_multiple_registers (%d)\n", ret); | |
| 172 | + printf("Slave = %d, address = %d, nb_points = %d\n", | |
| 173 | + SLAVE, addr, nb_points); | |
| 174 | + nb_fail++; | |
| 175 | + } else { | |
| 176 | + ret = read_holding_registers(&mb_param, SLAVE, | |
| 177 | + addr, nb_points, tab_rp_registers); | |
| 178 | + if (ret != nb_points) { | |
| 179 | + printf("ERROR read_holding_registers (%d)\n", ret); | |
| 180 | + printf("Slave = %d, address = %d, nb_points = %d\n", | |
| 181 | + SLAVE, addr, nb_points); | |
| 182 | + nb_fail++; | |
| 142 | 183 | } else { |
| 143 | - ok = read_holding_registers(&mb_param, SLAVE, | |
| 144 | - addr, 1, tab_rp); | |
| 145 | - if (ok != 1) { | |
| 146 | - printf("ERROR read_holding_registers single (%d)\n", ok); | |
| 147 | - printf("Slave = %d, address = %d\n", | |
| 148 | - SLAVE, addr); | |
| 149 | - fail++; | |
| 150 | - } else { | |
| 151 | - if (tab_rq[0] != tab_rp[0]) { | |
| 152 | - printf("ERROR read_holding_registers single "); | |
| 184 | + for (i=0; i<nb_points; i++) { | |
| 185 | + if (tab_rq_registers[i] != tab_rp_registers[i]) { | |
| 186 | + printf("ERROR read_holding_registers "); | |
| 153 | 187 | printf("(%d != %d)\n", |
| 154 | - tab_rq[0], tab_rp[0]); | |
| 188 | + tab_rq_registers[i], tab_rp_registers[i]); | |
| 155 | 189 | printf("Slave = %d, address = %d\n", |
| 156 | 190 | SLAVE, addr); |
| 157 | - fail++; | |
| 158 | - } | |
| 159 | - } | |
| 160 | - } | |
| 161 | - | |
| 162 | - /* MULTIPLE REGISTERS */ | |
| 163 | - ok = preset_multiple_registers(&mb_param, SLAVE, | |
| 164 | - addr, field_nb, tab_rq); | |
| 165 | - if (ok != field_nb) { | |
| 166 | - printf("ERROR preset_multiple_registers (%d)\n", ok); | |
| 167 | - printf("Slave = %d, address = %d, field_nb = %d\n", | |
| 168 | - SLAVE, addr, field_nb); | |
| 169 | - fail++; | |
| 170 | - } else { | |
| 171 | - ok = read_holding_registers(&mb_param, SLAVE, | |
| 172 | - addr, field_nb, tab_rp); | |
| 173 | - if (ok != field_nb) { | |
| 174 | - printf("ERROR read_holding_registers (%d)\n", ok); | |
| 175 | - printf("Slave = %d, address = %d, field_nb = %d\n", | |
| 176 | - SLAVE, addr, field_nb); | |
| 177 | - fail++; | |
| 178 | - } else { | |
| 179 | - for (i=0; i<field_nb; i++) { | |
| 180 | - if (tab_rq[i] != tab_rp[i]) { | |
| 181 | - printf("ERROR read_holding_registers "); | |
| 182 | - printf("(%d != %d)\n", | |
| 183 | - tab_rq[i], tab_rp[i]); | |
| 184 | - printf("Slave = %d, address = %d\n", | |
| 185 | - SLAVE, addr); | |
| 186 | - fail++; | |
| 187 | - } | |
| 191 | + nb_fail++; | |
| 188 | 192 | } |
| 189 | 193 | } |
| 190 | 194 | } |
| 191 | 195 | } |
| 192 | - | |
| 193 | - if (fail) | |
| 194 | - printf("Address : %d - Fails sum : %d\n", addr, fail); | |
| 195 | - else | |
| 196 | - printf("Address : %d - OK\n", addr); | |
| 197 | 196 | } |
| 197 | + | |
| 198 | + if (nb_fail) | |
| 199 | + printf("Address: %d - nb of fails: %d\n", addr, nb_fail); | |
| 200 | + else | |
| 201 | + printf("Address: %d - OK\n", addr); | |
| 198 | 202 | } |
| 199 | 203 | |
| 200 | 204 | /* Free the memory */ |
| 201 | - free(tab_rp); | |
| 202 | - free(tab_rq); | |
| 203 | - free(tab_rq_bits); | |
| 205 | + free(tab_rp_status); | |
| 206 | + free(tab_rq_status); | |
| 207 | + free(tab_rp_registers); | |
| 208 | + free(tab_rq_registers); | |
| 204 | 209 | |
| 205 | 210 | /* Close the connection */ |
| 206 | 211 | modbus_close(&mb_param); | ... | ... |