// libmodbus_use.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include
#include "modbus.h"
#define CODE_BOOL_READ (0x01) /*BOOL 读功能码 */
#define CODE_BOOL_WRITE (0x05)/*(0x0F)*/ /*BOOL 写功能码 */
#define CODE_INT_READ (0x03) /*INT 读功能码 */
#define CODE_INT_WRITE (0x10) /*INT 写功能码 */
#define CODE_REAL_READ CODE_INT_READ /*REAL 读功能码 */
#define CODE_REAL_WRITE CODE_INT_WRITE /*REAL 写功能码 */
//读取( 寄存器1-高16位AB-value1 寄存器2-低16位CD-value2 )模式的float
float modbus_float_inverse(uint16_t value1, uint16_t value2)
{
float temp;
char* Modbus_HoldReg[4];
Modbus_HoldReg[0] = ((char*)(&temp)) + 3;
Modbus_HoldReg[1] = ((char*)(&temp)) + 2;
Modbus_HoldReg[2] = ((char*)(&temp)) + 1;
Modbus_HoldReg[3] = ((char*)(&temp)) + 0;
*Modbus_HoldReg[0] = (value1 >> 8) & 0xff;
*Modbus_HoldReg[1] = value1 & 0xff;
*Modbus_HoldReg[2] = (value2 >> 8) & 0xff;
*Modbus_HoldReg[3] = value2 & 0xff;
return temp;
}
// 读取( 寄存器1-低16位CD-value1 寄存器2-高16位AB-value2 )模式的float
float modbus_float(uint16_t value1, uint16_t value2)
{
float temp;
char* Modbus_HoldReg[4];
Modbus_HoldReg[0] = ((char*)(&temp)) + 3;
Modbus_HoldReg[1] = ((char*)(&temp)) + 2;
Modbus_HoldReg[2] = ((char*)(&temp)) + 1;
Modbus_HoldReg[3] = ((char*)(&temp)) + 0;
*Modbus_HoldReg[0] = (value2 >> 8) & 0xff;
*Modbus_HoldReg[1] = value2 & 0xff;
*Modbus_HoldReg[2] = (value1 >> 8) & 0xff;
*Modbus_HoldReg[3] = value1 & 0xff;
return temp;
}
float modbus_float_inverse(uint8_t value1, uint8_t value2, uint8_t value3, uint8_t value4)
{
float temp;
char* Modbus_HoldReg[4];
Modbus_HoldReg[0] = ((char*)(&temp)) + 3;
Modbus_HoldReg[1] = ((char*)(&temp)) + 2;
Modbus_HoldReg[2] = ((char*)(&temp)) + 1;
Modbus_HoldReg[3] = ((char*)(&temp)) + 0;
*Modbus_HoldReg[0] = value1;
*Modbus_HoldReg[1] = value2;
*Modbus_HoldReg[2] = value3;
*Modbus_HoldReg[3] = value4;
return temp;
}
int main()
{
// 创建modbus对象create a modbus object
modbus mb = modbus("127.0.0.1", 502);
//设置从站 set slave id
mb.modbus_set_slave_id(1);
// 连接服务器connect with the server
mb.modbus_connect();
///
// 读取线圈 read coil function 0x01
bool read_coil;
mb.modbus_read_coils(0, 1, &read_coil);//1开始
//读取离散输入 read input bits(discrete input) function 0x02
bool read_bits;
mb.modbus_read_input_bits(0, 1, &read_bits);//10001开始
// 读取保持寄存器read holding registers function 0x03
uint16_t read_holding_regs[1];
mb.modbus_read_holding_registers(0, 1, read_holding_regs);//40001开始
// 读取模拟输入寄存器read input registers function 0x04
uint16_t read_input_regs[1];
mb.modbus_read_input_registers(0, 1, read_input_regs);//30001开始
///
//写入单线圈 write single coil function 0x05
mb.modbus_write_coil(0, true);//1开始
//写入单寄存器 write single reg function 0x06
mb.modbus_write_register(0, 123);//40001开始
// 写入多线圈write multiple coils function 0x0F
bool write_cols[4] = { true, true, true, true };
mb.modbus_write_coils(0, 4, write_cols);
// 写入多寄存器write multiple regs function 0x10
uint16_t write_regs[4] = { 123, 123, 123 };
mb.modbus_write_registers(0, 4, write_regs);
/
//写入浮点数
float data = 3512.5549;
uint16_t write_regs2[2] = { 0 };
memcpy(write_regs2, &data, sizeof(float));
for (int i = 0; i < 2; i++) // ABCD模式 write_regs2[0]-高16位AB-寄存器1 write_regs2[1]-低16位CD-寄存器2
printf("data change write_regs[%d] = 0x%02x\n", i, write_regs2[i]);
uint16_t t;
t = write_regs2[0];
write_regs2[0] = write_regs2[1];
write_regs2[1] = t;
for (int i = 0; i < 2; i++) // CDAB模式 写入。
printf("data change write_regs[%d] = 0x%02x\n", i, write_regs2[i]);
mb.modbus_write_registers(4, 2, write_regs2);
//读取浮点数
uint16_t read_regs[2];
mb.modbus_read_holding_registers(4, 2, read_regs);
float f1 = modbus_float(read_regs[1], read_regs[0]);
printf("float number is %7f\n", f1);
// 关闭连接并释放内存
mb.modbus_close();
// std::cout << getfloat(0x80, 0x00, 0xc3, 0xb5);
std::cout << "The End!\n";
}
void getfloat()
{
float fresult;
unsigned short int iresult;
char rec[4] = { 0x80,0x00,0xc3,0xb5 };
char* modebus[4];
modebus[0] = ((char*)(&fresult)) + 1;
modebus[1] = ((char*)(&fresult)) + 0;
modebus[2] = ((char*)(&fresult)) + 3;
modebus[3] = ((char*)(&fresult)) + 2;
*modebus[0] = rec[0];
*modebus[1] = rec[1];
*modebus[2] = rec[2];
*modebus[3] = rec[3];
printf("输出浮点数: %f\r\n", fresult);
modebus[0] = ((char*)(&iresult)) + 1;
modebus[1] = ((char*)(&iresult)) + 0;
*modebus[0] = rec[0];
*modebus[1] = rec[1];
printf("输出整数: %d\r\n", iresult);
}
float getfloat(unsigned char A, unsigned char B, unsigned char C, unsigned char D)
{
float fresult;
unsigned short int iresult;
//char rec[4] = { 0x80,0x00,0xc3,0xb5 };
char rec[4] = { A,B,C,D };
char* modebus[4];
modebus[0] = ((char*)(&fresult)) + 1;
modebus[1] = ((char*)(&fresult)) + 0;
modebus[2] = ((char*)(&fresult)) + 3;
modebus[3] = ((char*)(&fresult)) + 2;
*modebus[0] = rec[0];
*modebus[1] = rec[1];
*modebus[2] = rec[2];
*modebus[3] = rec[3];
return fresult;
/*printf("输出浮点数: %f\r\n", fresult);
modebus[0] = ((char*)(&iresult)) + 1;
modebus[1] = ((char*)(&iresult)) + 0;
*modebus[0] = rec[0];
*modebus[1] = rec[1];
printf("输出整数: %d\r\n", iresult);*/
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
参考:
C++ modbus 通信中Long与Float数据解析_小鱼仙官的博客-CSDN博客_float inversehttps://blog.csdn.net/yuchunhai321/article/details/119725011
fanzhe98/modbuspp: A C++ Library for Modbus TCP Client Protocol (github.com)https://github.com/fanzhe98/modbuspp