Modbus封装库(Com,tcp,udp一应俱全)

自行封装在用的Modbus通迅库,集成了com,tcp,udp, 做个笔记吧, 以下头文件,

#pragma once

#include <functional>

#include <vector>

#include <string>

#include <memory>

#ifdef LIBMODBUS_EXPORTS

#define LIBMODBUS_EXPORT_API __declspec(dllexport)

#else

#define LIBMODBUS_EXPORT_API __declspec(dllimport)

#endif

class LIBMODBUS_EXPORT_API RTUModbus {

public:

//devflag is 1 as default value

RTUModbus();

~RTUModbus();

public:

std::string getAddrPLC();

uint16_t getDevflag();

int getPort();

void sendBit(uint16_t addrstart, bool mvalue);

bool readBit(uint16_t addrstart, uint8_t* destBit);

void sendDataRegister(uint16_t addrstart, uint16_t mvalue);

void sendDataRegisters(uint16_t addrstart, uint16_t* mvalue);

void sendDataRegisters(uint16_t addrstart,const int len, uint16_t* mvalue);

bool readDataRegisters(uint16_t addrstart, uint16_t* destBit);

bool readDataRegisters(uint16_t addrstart,const int len, uint16_t* destBit);

bool isConnected();

void setPLCParam(const int mport, std::string ipra);

void disconnect();

bool connectPLCbyNet(const int port, const int devflag, std::string addr);

bool connectPLC(const int devflag);

//mport :com1 ,flag:0 Rs232,1:RS485

bool connectPLCbySerial(const char * mport, const int flag, const int baud = 115200,

char parity = 'N', const int data_bit = 8, const int stopbit = 1);

bool connectTo_UDP(std::string strIP, const int mport);

private:

std::shared_ptr<void> _modClient;

};

应用标例:

RTUModbus* mBus=new RTUModbus();

网口连接:

//tcp:

bool res=mBus->connectPLCbyNet(9000,1,"192.168.3.58");

//udp

bool res=connectTo_UDP("192.168.3.58",9000);

串口:

bool res=connectPLCbySerial("com1",1,115200);

读写方式,三者(串口、网口tcp,udp)都是共用的

下载地址:

LibModbus库(udp,tcp.com)一应俱全

https://download.csdn.net/download/sukeman/88504938

平台;VC++2015 ++

相关推荐
2401_873479408 小时前
IPv6查出来的地理位置是错的?用双栈IP库解决定位偏差问题
网络·数据库·tcp/ip·ip
大鱼>9 小时前
从零实现TCP/IP协议栈:AR →IP分片→TCP状态机→拥塞控制
网络·网络协议·tcp/ip
wsad053211 小时前
Ubuntu 26.04 配置静态 IP 地址
网络·tcp/ip·ubuntu
夜雪一千1 天前
TCP数据包长什么样?拆解TCP报文头部结构
网络·网络协议·tcp/ip
TMT星球1 天前
Crybaby特展北京站开幕 泡泡玛特以IP承载情绪表达
tcp/ip
复园电子1 天前
USB Over IP技术详解:基于USB服务器实现USB设备远程访问、重定向与集中管理
服务器·网络协议·tcp/ip
kaixin_learn_qt_ing1 天前
单网卡绑定多个 IP 地址
网络·网络协议·tcp/ip
tang777891 天前
分布式爬虫优化指南:如何用代理IP把采集效率提升300%
分布式·爬虫·python·tcp/ip·分布式爬虫·爬虫代理·代理ip
gwf2161 天前
Soft-RoCE与Soft-iWARP深度解析:无硬件RDMA学习环境搭建(零基础必知必会)
人工智能·python·tcp/ip·tcp·tcpdump
ZeroNews内网穿透1 天前
无公网 IP 打通 CI/CD:内网穿透对接 GitHub Actions 自动部署内网服务
运维·tcp/ip·ci/cd·github·远程工作·内网穿透