

#ifndef MCBINARYTCP_H
#define MCBINARYTCP_H
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <chrono>
#include <thread>
#include <stdio.h>
#include <algorithm>
#include <QObject>
#include <QTcpSocket>
#include <QHostAddress>
using namespace std;
class McBinaryTCP
{
public:
McBinaryTCP(const string& server, uint16_t port);
~McBinaryTCP();
void Close();
bool isConnected();
bool connectToServer();
bool Tsend(const string& message);
bool sendHex(const vector<uint8_t>& bytesToSend);
string receive();
string receiveHex();
string ReadPLC(uint16_t intPlcAddress, uint16_t intCount, const string& strAddressType,const string& indexBit);
vector<int> ReadWordToListInt(const string& key, uint16_t intCount);
vector<int> ReadWordToListShort(const string& key, uint16_t intCount);
vector<string> ReadWordListstring(const string& key, uint16_t intCount);
vector<string> ReadBitToListstring(const string& key, uint16_t intCount);
vector<bool> ReadBitToList(const string& key, uint16_t intCount);
bool WritePlc(uint16_t intPlcAddress, const string& strValue, const string& strAddressType);
bool WritePlcBit(uint16_t intPlcAddress, const string& strValue, const string& strAddressType, const string& index);
bool WritePlcFormListBit(const string& key, const vector<int>& list);
bool WritePlcFormListInt(const string& key, const vector<int>& list);
bool WritePlcFormListShort(const string& key, const vector<int>& list);
private:
vector<uint8_t> hexStringToBytes(const string& hexStr);
uint16_t StringToUShort(const string& str);
string decimalToHex(int decimal) ;
string PadLeft(const string& input, size_t length, char paddingChar = ' ') ;
string hexReversal(const string& key);
string server_;
uint16_t port_;
QTcpSocket qtsocket;
string DA1=""; //plc的IP后缀信息
bool firstconnect=false;
};
#endif // FINSTCP_H
源码下载
https://8ma.co/res/ZUDR1EJJ
在国产化浪潮下,Deepin系统作为国产操作系统标杆,为工业控制领域提供了稳定可靠的部署平台。本文以Qt C++框架开发欧姆龙PLC上位机通信为例,阐述国产化适配实践。
核心实现:上位机与三菱PLC通信时,可以使用3E二进制协议。这是一种专用的通信协议,用于实现上位机软件与三菱PLC之间的数据交换。通过这种协议,上位机可以发送指令给PLC,同时从PLC读取所需的数据,从而实现对工业自动化过程的监控和控制。这种协议的应用可以提高通信的效率和可靠性,确保工业自动化系统的稳定运行。
国产化亮点:Deepin对Qt5/6的完美兼容,使跨平台编译零成本;其内核级实时性优化,显著降低通信抖动。实测在鲲鹏CPU架构下,数据刷新周期稳定在10ms以内,满足严苛的产线节拍要求。
开发建议:封装三菱MC3E协议库为独立模块,利用Qt信号槽机制实现异步通信,避免界面卡顿。通过qmake条件编译适配不同国产平台,一套代码即可覆盖龙芯、飞腾等架构,助力工业软件自主可控。


被折叠的 条评论
为什么被折叠?



