modbus 的float与uint_16的转换

float values = 0.5; // 要写入的值

uint16_t wdata2{ 0 };

modbus_set_float(values, wdata);

// 写多个寄存器

int rc = modbus_write_registers(ctx, 8194, 2, wdata);

if (rc == -1) {

// 写操作失败,处理错误

fprintf(stderr, "Failed to write registers: %s\n", modbus_strerror(errno));

modbus_close(ctx);

modbus_free(ctx);

ctx = nullptr;

// 可能还需要更新UI以反映连接状态

}

else {

qDebug() << "Registers written successfully.";

// 读取写入的寄存器以验证

uint16_t rdata2 = { 0 };

rc = modbus_read_registers(ctx, 8194, 2, rdata);

if (rc == -1) {

// 处理读取错误

}

else {

float read_value = modbus_get_float(rdata); // 假设这个函数正确处理了字节序

qDebug() << "Read value:" << read_value;

// 验证读取的值是否与写入的值相同

if (fabs(read_value - values) < 0.0001) {

qDebug() << "Read and written values match.";

}

else {

qDebug() << "Read and written values do not match!";

}

}

}

相关推荐
程与留18 小时前
15_国际化和本地化:tr()、ts 文件、QM 文件、多语言切换
c++·qt
程与留20 小时前
14_Qt 样式表(QSS)入门(语法、选择器、美化实战)
c++·qt
欧特克_Glodon1 天前
OpenCV计算机视觉开发入门与实践<二十七>:图像分割概述
c++·人工智能·opencv·计算机视觉
蒸蒸yyyyzwd1 天前
cpp 选手秋招学习笔记 day21
c++·面试·八股
Interview Aid1121 天前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
CoderIsArt2 天前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
AI情绪识别开源2 天前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
「QT(C++)开发工程师」2 天前
C++ auto 用法详解
开发语言·c++
OPEN-F2 天前
C++STL教程:容器适配器与实用工具
开发语言·c++