41 #define ADDRESS_START 0 42 #define ADDRESS_END 99 57 uint16_t *tab_rq_registers;
58 uint16_t *tab_rw_rq_registers;
59 uint16_t *tab_rp_registers;
72 fprintf(stderr,
"Connection failed: %s\n",
81 tab_rq_bits = (uint8_t *) malloc(nb *
sizeof(uint8_t));
82 memset(tab_rq_bits, 0, nb *
sizeof(uint8_t));
84 tab_rp_bits = (uint8_t *) malloc(nb *
sizeof(uint8_t));
85 memset(tab_rp_bits, 0, nb *
sizeof(uint8_t));
87 tab_rq_registers = (uint16_t *) malloc(nb *
sizeof(uint16_t));
88 memset(tab_rq_registers, 0, nb *
sizeof(uint16_t));
90 tab_rp_registers = (uint16_t *) malloc(nb *
sizeof(uint16_t));
91 memset(tab_rp_registers, 0, nb *
sizeof(uint16_t));
93 tab_rw_rq_registers = (uint16_t *) malloc(nb *
sizeof(uint16_t));
94 memset(tab_rw_rq_registers, 0, nb *
sizeof(uint16_t));
96 nb_loop = nb_fail = 0;
97 while (nb_loop++ <
LOOP) {
98 for (addr = ADDRESS_START; addr <=
ADDRESS_END; addr++) {
102 for (i=0; i<nb; i++) {
103 tab_rq_registers[i] = (uint16_t) (65535.0*rand() / (RAND_MAX + 1.0));
104 tab_rw_rq_registers[i] = ~tab_rq_registers[i];
105 tab_rq_bits[i] = tab_rq_registers[i] % 2;
112 printf(
"ERROR modbus_write_bit (%d)\n", rc);
113 printf(
"Address = %d, value = %d\n", addr, tab_rq_bits[0]);
117 if (rc != 1 || tab_rq_bits[0] != tab_rp_bits[0]) {
118 printf(
"ERROR modbus_read_bits single (%d)\n", rc);
119 printf(
"address = %d\n", addr);
127 printf(
"ERROR modbus_write_bits (%d)\n", rc);
128 printf(
"Address = %d, nb = %d\n", addr, nb);
133 printf(
"ERROR modbus_read_bits\n");
134 printf(
"Address = %d, nb = %d\n", addr, nb);
137 for (i=0; i<nb; i++) {
138 if (tab_rp_bits[i] != tab_rq_bits[i]) {
139 printf(
"ERROR modbus_read_bits\n");
140 printf(
"Address = %d, value %d (0x%X) != %d (0x%X)\n",
141 addr, tab_rq_bits[i], tab_rq_bits[i],
142 tab_rp_bits[i], tab_rp_bits[i]);
152 printf(
"ERROR modbus_write_register (%d)\n", rc);
153 printf(
"Address = %d, value = %d (0x%X)\n",
154 addr, tab_rq_registers[0], tab_rq_registers[0]);
159 printf(
"ERROR modbus_read_registers single (%d)\n", rc);
160 printf(
"Address = %d\n", addr);
163 if (tab_rq_registers[0] != tab_rp_registers[0]) {
164 printf(
"ERROR modbus_read_registers single\n");
165 printf(
"Address = %d, value = %d (0x%X) != %d (0x%X)\n",
166 addr, tab_rq_registers[0], tab_rq_registers[0],
167 tab_rp_registers[0], tab_rp_registers[0]);
176 printf(
"ERROR modbus_write_registers (%d)\n", rc);
177 printf(
"Address = %d, nb = %d\n", addr, nb);
182 printf(
"ERROR modbus_read_registers (%d)\n", rc);
183 printf(
"Address = %d, nb = %d\n", addr, nb);
186 for (i=0; i<nb; i++) {
187 if (tab_rq_registers[i] != tab_rp_registers[i]) {
188 printf(
"ERROR modbus_read_registers\n");
189 printf(
"Address = %d, value %d (0x%X) != %d (0x%X)\n",
190 addr, tab_rq_registers[i], tab_rq_registers[i],
191 tab_rp_registers[i], tab_rp_registers[i]);
199 addr, nb, tab_rw_rq_registers,
200 addr, nb, tab_rp_registers);
202 printf(
"ERROR modbus_read_and_write_registers (%d)\n", rc);
203 printf(
"Address = %d, nb = %d\n", addr, nb);
206 for (i=0; i<nb; i++) {
207 if (tab_rp_registers[i] != tab_rw_rq_registers[i]) {
208 printf(
"ERROR modbus_read_and_write_registers READ\n");
209 printf(
"Address = %d, value %d (0x%X) != %d (0x%X)\n",
210 addr, tab_rp_registers[i], tab_rw_rq_registers[i],
211 tab_rp_registers[i], tab_rw_rq_registers[i]);
218 printf(
"ERROR modbus_read_registers (%d)\n", rc);
219 printf(
"Address = %d, nb = %d\n", addr, nb);
222 for (i=0; i<nb; i++) {
223 if (tab_rw_rq_registers[i] != tab_rp_registers[i]) {
224 printf(
"ERROR modbus_read_and_write_registers WRITE\n");
225 printf(
"Address = %d, value %d (0x%X) != %d (0x%X)\n",
226 addr, tab_rw_rq_registers[i], tab_rw_rq_registers[i],
227 tab_rp_registers[i], tab_rp_registers[i]);
237 printf(
"%d FAILS\n", nb_fail);
245 free(tab_rq_registers);
246 free(tab_rp_registers);
247 free(tab_rw_rq_registers);
int modbus_read_bits(modbus_t *ctx, int addr, int nb, uint8_t *dest)
void modbus_free(modbus_t *ctx)
void modbus_set_debug(modbus_t *ctx, int boolean)
void modbus_close(modbus_t *ctx)
int modbus_write_register(modbus_t *ctx, int reg_addr, int value)
const char * modbus_strerror(int errnum)
int modbus_write_registers(modbus_t *ctx, int addr, int nb, const uint16_t *data)
int modbus_write_bits(modbus_t *ctx, int addr, int nb, const uint8_t *data)
int modbus_read_registers(modbus_t *ctx, int addr, int nb, uint16_t *dest)
int modbus_connect(modbus_t *ctx)
int modbus_write_and_read_registers(modbus_t *ctx, int write_addr, int write_nb, const uint16_t *src, int read_addr, int read_nb, uint16_t *dest)
modbus_t * modbus_new_tcp(const char *ip_address, int port)
int modbus_write_bit(modbus_t *ctx, int coil_addr, int status)