基于libmodbus 开发RTU Master端测试程序
- 开发环境
- 完整代码
- 配合Modbus Slave进行测试
- 测试运行演示
开发环境
- 编译软件:VS2019
- libmodbusbus库:3.1.6版本 下载地址:https://libmodbus.org/download/
完整代码
/*********************************************************************
* INCLUDES
*/
#include
#ifndef _MSC_VER
include <unistd.h>
#endif
#include
#include
#include
#include "modbus.h"
#define LOOP 1
#define SERVER_ID 17
#define ADDRESS_START 0
#define ADDRESS_END 99
int main(void)
{
modbus_t* ctx;
int rc;
int nb_fail;
int nb_loop;
int addr;
int nb;
int seed_c,RTU_mode,loop_n;
uint8_t* tab_rq_bits;
uint8_t* tab_rp_bits;
uint16_t* tab_rq_registers;
uint16_t* tab_rw_rq_registers;
uint16_t* tab_rp_registers;
system("title C++ modbus-RTU 测试程序");
system("mode con cols=48 lines=25");
system("color 0B");
system("date /T");
system("TIME /T");
printf(" ╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪\n");
printf("╔═╧╧ C++ modbus-RTU 测试程序 ╧╧═╗\n");
printf("║※1. 单个读写寄存器║\n");
printf("║※2. 多个读写寄存器║\n");
printf("║※3. 0x17功能码读写寄存器║\n");
printf("║※0. ║\n");
printf("╚═══════════════════╝\n");
Sleep(1000);
printf("输入一个随机数种子:");
scanf_s("%d", &seed_c);
srand(seed_c);
printf("输入测试模式(1~3):");
scanf_s("%d", &RTU_mode);
printf("测试循环次数:");
scanf_s("%d", &loop_n);
ctx = modbus_new_rtu("COM3", 9600, 'N', 8, 1);
modbus_set_slave(ctx, SERVER_ID);
if (modbus_connect(ctx) == -1)
{
fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
modbus_free(ctx);
return -1;
}
nb = ADDRESS_END - ADDRESS_START;
tab_rq_bits = (uint8_t*)malloc(nb * sizeof(uint8_t));
memset(tab_rq_bits, 0, nb * sizeof(uint8_t));
tab_rp_bits = (uint8_t*)malloc(nb * sizeof(uint8_t));
memset(tab_rp_bits, 0, nb * sizeof(uint8_t));
tab_rq_registers = (uint16_t*)malloc(nb * sizeof(uint16_t));
memset(tab_rq_registers, 0, nb * sizeof(uint16_t));
tab_rp_registers = (uint16_t*)malloc(nb * sizeof(uint16_t));
memset(tab_rp_registers, 0, nb * sizeof(uint16_t));
tab_rw_rq_registers = (uint16_t*)malloc(nb * sizeof(uint16_t));
memset(tab_rw_rq_registers, 0, nb * sizeof(uint16_t));
Sleep(1000);
nb_loop = nb_fail = 0;
while (nb_loop++ < loop_n)
{
if (RTU_mode == 1)
{
for (addr = ADDRESS_START; addr < ADDRESS_END; addr++)
{
Sleep(100);
tab_rq_registers[0] = (uint16_t)rand();
tab_rq_bits[0] = tab_rq_registers[0] % 2;
printf("tab_rq_registers[0] =0x%04X;tab_rq_bits[0] =%d\n", tab_rq_registers[0], tab_rq_bits[0]);
nb = ADDRESS_END - addr;
rc = modbus_write_bit(ctx, addr, tab_rq_bits[0]);
if (rc != 1)
{
printf("ERROR modbus_write_bit (%d)\n", rc);
printf("Address =%d, value =%d\n", addr, tab_rq_bits[0]);
nb_fail++;
}
else
{
rc = modbus_read_bits(ctx, addr, 1, tab_rp_bits);
if (rc != 1 || tab_rq_bits[0] != tab_rp_bits[0])
{
printf("ERROR modbus_read_bits single (%d)\n", rc);
printf("address =%d\n", addr);
nb_fail++;
}
}
rc = modbus_write_register(ctx, addr, tab_rq_registers[0]);
if (rc != 1)
{
printf("ERROR modbus_write_register (%d)\n", rc);
printf("Address =%d, Val =%d(0x%X)\n", addr, tab_rq_registers[0], tab_rq_registers[0]);
nb_fail++;
}
else
{
rc = modbus_read_registers(ctx, addr, 1, tab_rp_registers);
if (rc != 1)
{
printf("ERROR modbus_read_registers (%d)\n", rc);
printf("Address =%d\n", addr);
nb_fail++;
}
else
{
if (tab_rp_registers[0] != tab_rq_registers[0])
{
printf("ERROR modbus_read_registers\n");
printf("Addr =%d, val =%d(0x%X) != %d(0x%X)\n", addr, tab_rq_registers[0], tab_rq_registers[0], tab_rp_registers[0], tab_rp_registers[0]);
nb_fail++;
}
}
}
}
}
else if(RTU_mode == 2)
{
addr = ADDRESS_START;
nb = ADDRESS_END - addr;
int i;
for (i = 0; i < nb; i++)
{
tab_rq_registers[i] =
(uint16_t)(65535.0 * rand() / (RAND_MAX + 1.0));
tab_rq_bits[i] = tab_rq_registers[i] % 2;
}
rc = modbus_write_bits(ctx, addr, nb, tab_rq_bits);
if (rc != nb)
{
printf("ERROR modbus_write_bits (%d)\n", rc);
printf("Address =%d, nb =%d\n", addr, nb);
nb_fail++;
}
else
{
rc = modbus_read_bits(ctx, addr, nb, tab_rp_bits);
if (rc != nb)
{
printf("ERROR modbus_read_bits\n");
printf("Address =%d, nb =%d\n", addr,nb);
nb_fail++;
}
else
{
for (i = 0; i < nb; i++)
{
if (tab_rp_bits[i] != tab_rq_bits[i])
{
printf("ERROR modbus_read_bits\n");
printf("Addr =%d, val =%d(0x%X) != %d(0x%X)\n",addr,tab_rq_bits[i],tab_rq_bits[i],tab_rp_bits[i],tab_rq_bits[i]);
nb_fail++;
}
}
}
}
rc = modbus_write_registers(ctx, addr, nb,tab_rq_registers);
if (rc != nb)
{
printf("ERROR modbus_write_register (%d)\n", rc);
printf("Address =%d, nb =%d\n", addr, nb);
nb_fail++;
}
else
{
rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
if (rc != nb)
{
printf("ERROR modbus_read_registers (%d)\n", rc);
printf("Address =%d, nb =%d\n", addr, nb);
nb_fail++;
}
else
{
for (i = 0; i < nb; i++)
{
if (tab_rp_registers[i] != tab_rq_registers[i])
{
printf("ERROR modbus_read_registers\n");
printf("Addr =%d, val =%d(0x%X) != %d(0x%X)\n", addr, tab_rq_registers[i], tab_rq_registers[i], tab_rp_registers[i], tab_rp_registers[i]);
nb_fail++;
}
}
}
}
}
else if (RTU_mode == 3)
{
addr = ADDRESS_START;
nb = ADDRESS_END - addr;
int i;
for (i = 0; i < nb; i++)
{
tab_rq_registers[i] =
(uint16_t)(65535.0 * rand() / (RAND_MAX + 1.0));
tab_rw_rq_registers[i] = ~tab_rq_registers[i];
}
rc = modbus_write_and_read_registers(ctx, addr, nb, tab_rw_rq_registers, addr, nb, tab_rp_registers);
if (rc != nb)
{
printf("ERROR modbus_write_and_read_register (%d)\n", rc);
printf("Address =%d, nb =%d\n", addr, nb);
nb_fail++;
}
else
{
for (i = 0; i < nb; i++)
{
if (tab_rp_registers[i] != tab_rw_rq_registers[i])
{
printf("ERROR modbus_write_and_read_register READ\n");
printf("Addr =%d, val =%d(0x%X) != %d(0x%X)\n", addr, tab_rq_registers[i], tab_rq_registers[i], tab_rp_registers[i], tab_rp_registers[i]);
nb_fail++;
}
}
rc = modbus_read_registers(ctx, addr, nb, tab_rp_registers);
if (rc != nb)
{
printf("ERROR modbus_write_and_read_register Write ti READ\n");
printf("Address =%d, nb =%d\n", addr, nb);
nb_fail++;
}
else
{
for (i = 0; i < nb; i++)
{
if (tab_rp_registers[i] != tab_rw_rq_registers[i])
{
printf("ERROR modbus_write_and_read_register Write ti READ\n");
printf("Addr =%d, val =%d(0x%X) != %d(0x%X)\n", addr, tab_rw_rq_registers[i], tab_rw_rq_registers[i], tab_rp_registers[i], tab_rp_registers[i]);
nb_fail++;
}
}
}
}
}
printf("Test Rtu:");
if (nb_fail)
printf("%d FAILS\n", nb_fail);
else
printf("SUCCESS\n");
srand(seed_c + nb_loop + 1);
}
free(tab_rq_bits);
free(tab_rp_bits);
free(tab_rq_registers);
free(tab_rp_registers);
free(tab_rw_rq_registers);
modbus_close(ctx);
modbus_free(ctx);
system("pause");
return 0;
}
配合Modbus Slave进行测试
Modbus Slave 如下图设置
测试运行演示