C++封装modbustcp 客户端和服务端的头文件和源文件和主函数调用

时间: 2023-11-06 10:04:06 浏览: 48
下面是一个简单的 C++ 封装 Modbus TCP 客户端和服务端的示例代码,包括头文件和源文件: ```c++ // modbus_tcp_client.h #include <modbus/modbus.h> class ModbusTCPClient { public: ModbusTCPClient(const char* ip, int port); ~ModbusTCPClient(); void connect(); void disconnect(); void read_coil(int address, int count, uint8_t* dest); void write_coil(int address, int count, const uint8_t* data); private: modbus_t* ctx; const char* ip; int port; }; // modbus_tcp_client.cpp #include "modbus_tcp_client.h" #include <stdexcept> ModbusTCPClient::ModbusTCPClient(const char* ip, int port) : ip(ip), port(port) { ctx = modbus_new_tcp(ip, port); if (ctx == nullptr) { throw std::runtime_error("Failed to create modbus context"); } } ModbusTCPClient::~ModbusTCPClient() { modbus_free(ctx); } void ModbusTCPClient::connect() { if (modbus_connect(ctx) == -1) { throw std::runtime_error("Failed to connect to modbus server"); } } void ModbusTCPClient::disconnect() { modbus_close(ctx); } void ModbusTCPClient::read_coil(int address, int count, uint8_t* dest) { int rc = modbus_read_bits(ctx, address, count, dest); if (rc == -1) { throw std::runtime_error("Failed to read coil"); } } void ModbusTCPClient::write_coil(int address, int count, const uint8_t* data) { int rc = modbus_write_bits(ctx, address, count, data); if (rc == -1) { throw std::runtime_error("Failed to write coil"); } } // modbus_tcp_server.h #include <modbus/modbus.h> class ModbusTCPServer { public: ModbusTCPServer(int port); ~ModbusTCPServer(); void start(); void stop(); void add_coil(int address, bool value); private: modbus_t* ctx; int port; }; // modbus_tcp_server.cpp #include "modbus_tcp_server.h" #include <stdexcept> ModbusTCPServer::ModbusTCPServer(int port) : port(port) { ctx = modbus_new_tcp(NULL, port); if (ctx == NULL) { throw std::runtime_error("Failed to create modbus context"); } } ModbusTCPServer::~ModbusTCPServer() { modbus_free(ctx); } void ModbusTCPServer::start() { if (modbus_listen(ctx, 1) == -1) { throw std::runtime_error("Failed to start modbus server"); } } void ModbusTCPServer::stop() { modbus_close(ctx); } void ModbusTCPServer::add_coil(int address, bool value) { int rc = modbus_write_bit(ctx, address, value); if (rc == -1) { throw std::runtime_error("Failed to add coil"); } } ``` 下面是一个简单的主函数调用示例,用于测试这些类: ```c++ #include "modbus_tcp_client.h" #include "modbus_tcp_server.h" #include <iostream> int main() { try { // 创建 modbus TCP 服务器并启动 ModbusTCPServer server(502); server.start(); // 创建 modbus TCP 客户端并连接到服务器 ModbusTCPClient client("127.0.0.1", 502); client.connect(); // 在服务器上添加一个线圈 server.add_coil(0, true); // 从服务器上读取线圈的值 uint8_t value; client.read_coil(0, 1, &value); std::cout << "Coil value: " << (int)value << std::endl; // 在服务器上修改线圈的值 server.add_coil(0, false); // 向服务器上写入线圈的值 uint8_t data = 0; client.write_coil(0, 1, &data); // 断开客户端连接并停止服务器 client.disconnect(); server.stop(); } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; return 1; } return 0; } ``` 在上面的代码中,我们先创建了一个 modbus TCP 服务器并启动,然后创建了一个 modbus TCP 客户端并连接到服务器,然后在服务器上添加一个线圈,并从客户端上读取该线圈的值,并在服务器上修改该线圈的值,并向服务器上写入该线圈的值,最后断开客户端连接并停止服务器。

相关推荐

最新推荐

recommend-type

kepware作服务器的modbusTCP通信(原创).docx

网上kepserver作modbusRTU的文章很多,modbusTCP的很少,仅有文章中,kepware通信类似于modbusRTU作上位机,实质是kepserver工作在TCPclient模式,现有文章用modsim32选择modbusTCP协议模拟仪表发送数据,kepserver...
recommend-type

python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例

今天小编就为大家分享一篇python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

S7-200 SMART Modbus TCP服务 器指令库

本文用于介绍 S7-200 SMART Modbus TCP 服务器指令的使用。 伴随着 STEP 7-Micro/WIN SMART V2.2 和 S7-200 SMART CPU 固件 V2.2 的 发布, S7-200 SMART CPU 开始支持 TCP 、 ISO-on-TCP 和 UDP 等开放式用 户通信...
recommend-type

S7-200 SMART Modbus TCP 服务器指令.docx

每个 MB_Server指令库只能创建一个Mdobus TCP连接,如果一个S7-200 SMART 需要连接多个Modbus TCP 客户端,则需要购买多个名称不同的MB_Server指令库(例如MB_Server_0,MB_Server_1,MB_Server_2)。
recommend-type

三菱Q系列与汇川H3u的Modbus TCP通信说明文档.pdf

通过三菱Q PLC的内置以太网口与汇川H3u MODBUS TCP通信, 实现不同PLC之间的数据交换,实现PLC MODBUS TCP的主从站数据交换;
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。