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 ++

相关推荐
学习3人组6 小时前
集群服务器主机实现主机名与IP绑定
运维·服务器·tcp/ip
做人求其滴6 小时前
信安 实验1 用Wireshark分析典型TCP/IP体系中的协议
tcp/ip·wireshark
温有情10 小时前
UDP_SOCKET编程实现
网络·网络协议·udp
pemper_11 小时前
数据不出境------IP证书申请
网络·网络协议·tcp/ip·http·https·ssl
Jack黄从零学c++11 小时前
自制网络连接工具(支持tcpudp,客户端服务端)
linux·c语言·开发语言·网络协议·tcp/ip·udp·信息与通信
hgdlip13 小时前
电脑ip会因为换了网络改变吗
服务器·网络·tcp/ip·电脑
人工智能的苟富贵14 小时前
微信小程序中的实时通讯:TCP/UDP 协议实现详解
tcp/ip·微信小程序·udp
~yY…s<#>15 小时前
【计算机网络】传输层协议UDP
网络协议·计算机网络·udp
机器视觉知识推荐、就业指导15 小时前
Qt/C++ TCP调试助手V1.1 新增图像传输与接收功能(附发布版下载链接)
c++·qt·tcp/ip
嘻嘻仙人20 小时前
【网络通信基础与实践第四讲】用户数据报协议UDP和传输控制协议TCP
网络·网络协议·udp·tcp·三次握手·流量控制·拥塞控制