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

相关推荐
群联云防护小杜2 小时前
云服务器被黑客攻击应急响应与加固指南(上)
运维·服务器·人工智能·tcp/ip·自动化·压力测试
努力也学不会java8 小时前
【网络原理】 网络编程套接字
java·开发语言·网络·网络协议·tcp/ip·php
✿ ༺ ོIT技术༻10 小时前
Linux:TCP保证可靠性的方案(2)
网络·网络协议·tcp/ip
Hello.Reader1 天前
洞悉 NGINX ngx_http_access_module基于 IP 的访问控制实战指南
tcp/ip·nginx·http
技术liul1 天前
如何在iStoreOS DHCP中排除特定IP地址
网络·windows·tcp/ip
茉莉玫瑰花茶1 天前
socket套接字-UDP(中)
网络·网络协议·udp
薯条不要番茄酱1 天前
【网络原理】从零开始深入理解TCP的各项特性和机制.(二)
服务器·网络·tcp/ip
薯条不要番茄酱1 天前
【网络原理】从零开始深入理解TCP的各项特性和机制.(三)
网络·网络协议·tcp/ip
专注API从业者1 天前
反爬策略应对指南:淘宝 API 商品数据采集的 IP 代理与请求伪装技术
网络·网络协议·tcp/ip
老六ip加速器2 天前
如何获取静态IP地址?完整教程
网络·网络协议·tcp/ip