117 lines
4.9 KiB
C++
117 lines
4.9 KiB
C++
#ifndef Hd_SocketModule_FanucTcpClient_H
|
||
#define Hd_SocketModule_FanucTcpClient_H
|
||
|
||
#include <QtCore/qglobal.h>
|
||
#include <QByteArray>
|
||
#include <QThread>
|
||
#include <iostream>
|
||
#include <QTcpSocket>
|
||
#include <QHostAddress>
|
||
#include <QtWidgets/qmessagebox.h>
|
||
#include <QMutex>
|
||
#include <QObject>
|
||
#include <QQueue>
|
||
#include <QDebug>
|
||
#include <Windows.h>
|
||
|
||
#include "base.h"
|
||
|
||
class MyThread;
|
||
class Hd_SocketModule_FanucTcpClient :public Base
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
//创建,这里new一下锁
|
||
Hd_SocketModule_FanucTcpClient();
|
||
//释放,删除连接,释放锁
|
||
virtual ~Hd_SocketModule_FanucTcpClient();
|
||
//向主线程发送数据,通信Mat为空
|
||
//主线程调用sendData函数,把读报文的数据段发送回来。 //注意,只要数据段。
|
||
//主线程根据通信流程解析数据段。
|
||
//先writeData,再readData;
|
||
//从类中读数据到实例对象。通信类返回的内容,只包含数据段,不含包头。
|
||
bool readData(std::vector<cv::Mat>& vec_mat, QByteArray& data);//readData将PLC读取或者发送的返回值发回给主线程,通过全局变量的方式逐条发送
|
||
//接收主线程发来的数据,通信Mat为空
|
||
//例:write_DM1300_9,写寄存器DM1300为9;
|
||
//例:writeAll_DM1700_14_1,1,1,1,1,1,1,1,1,1,1,1,1,1,写寄存器DM1700-DM1713的14个寄存器,均为1
|
||
//例:readAll_DM1400_100,读DM1400-DM1499的100个寄存器;
|
||
//例:readAll_DM1001_1,读DM1001
|
||
//例:writeString_DM1340_12_2023-0217-1650-03-0899,写寄存器DM1340-DM1351时间戳,一个寄存器里面含2个字符;
|
||
//其它通信方式可参考,并添加解析协议。如:writeFloat_DM10000_-1.1,指令函数增加bool writeFloat(QString DM,QString value);
|
||
//实例对象把数据写入到类。通信类包含读、写指令。
|
||
bool writeData(std::vector<cv::Mat>& vec_mat, QByteArray& data); //writeData调用PLC的收发,将需要的数据发给寄存器,返回OK/NG
|
||
//把通信创建需要的参数传进去,不同协议不同,例:init_192.168.1.50_9600_24;
|
||
//连接通信,可以在run函数里进行
|
||
bool initParas(QByteArray&);
|
||
//运行
|
||
bool run();
|
||
//检查异常标志
|
||
bool checkStatus();
|
||
//private:
|
||
// QString UShortToHex(int value); // 只转 ushort 类型
|
||
// QString ShortToHex(int value); // 只转 short 类型
|
||
// void setSleepTimeAndReadNum(int time,int num);
|
||
// bool writeCommond(QString DM,QString m_value); // 写入单个值; //例:write_DM1300_9
|
||
// bool readCommond(QString DM,QString Num);
|
||
// bool readAllCommond(QString DM,QString Num); // 写入连续的值
|
||
// bool readStringCommond(QString DM, QString Num);
|
||
// bool writeAllCommond(QString DM,QString write_Num,QVector<short> vec_value);
|
||
// bool writeResultCommond(QString DM,QString write_Num,QVector<QString> vec_value); // 写入起始位结果值,要做解析再写入
|
||
// bool writeStringCommond(QString DM, QString write_num, QString vec_value);
|
||
// bool decodeWriteResult(QByteArray resultByte,QVector<QString> &vec_ret);
|
||
// bool tcpServer(QByteArray cmd, QByteArray& resultPack);
|
||
// void decodePack(QByteArray resultArray);
|
||
private:
|
||
int m_sleepTime = 100; // 延迟时间
|
||
QString m_host;
|
||
QString m_pot;
|
||
MyThread *m_Thread = nullptr;
|
||
|
||
};
|
||
|
||
//自己创建线程
|
||
class MyThread : public QThread
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
explicit MyThread(QObject* parent = nullptr) :QThread(parent) {}
|
||
~MyThread();
|
||
bool setIpAndPort(QString ip, QString port);
|
||
void run();
|
||
void readSocketData(); //读取数据
|
||
bool writeSocketData(); //写入数据
|
||
void decodePack();
|
||
void setMutexLock(QMutex* lock) { m_mutex_lock = lock; }
|
||
void setRunStop() { init_flag = 2; }
|
||
void setReadPackValue(int value);
|
||
void closeSocket();
|
||
bool getInitSuccessFlag() { return init_success; }
|
||
bool disReconnects();
|
||
void setSocketIp(QString m_socketIp) { socketIp = m_socketIp; }
|
||
|
||
QQueue<QByteArray> WritePLCPack;
|
||
QQueue<QByteArray> ReadPack;
|
||
private:
|
||
QString m_IP;
|
||
QString m_Port;
|
||
QMutex m_Mutex;
|
||
QByteArray reciveData;
|
||
QString socketIp;
|
||
bool init_success = true; // 初始化成功 返回true;失败返回false
|
||
int init_flag = 0; // false为完整数据包,true为非完整数据包
|
||
int m_readPackValue = 122; // 接受50个数据包的长度 为 122 ,加包头一起为 130, 如果是 100的话就是 222 加包头为230
|
||
QMutex* m_mutex_lock = nullptr;
|
||
QTcpSocket* tcpsocket = nullptr; // tcp通信的socket
|
||
|
||
|
||
};
|
||
|
||
|
||
extern "C"
|
||
{
|
||
Q_DECL_EXPORT Hd_SocketModule_FanucTcpClient * create();
|
||
Q_DECL_EXPORT void destory(Hd_SocketModule_FanucTcpClient * ptr);
|
||
}
|
||
|
||
#endif // Hd_SocketModule_FanucTcpClient_H
|