| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- #ifndef IEC104MASTERQT_H
- #define IEC104MASTERQT_H
- #include <QObject>
- #include <QTcpSocket>
- #include <QTimer>
- #include <QMap>
- #include <QMutex>
- #include <QAbstractSocket>
- #include "IEC104Types.h"
- #include <QDateTime>
- // 遥测数据结构
- struct MeasuredValue {
- float value;
- bool valid;
- bool overflow;
- bool blocked;
- bool substituted;
- QDateTime timestamp;
- };
- // 遥控命令类型
- enum class ControlCommandType {
- SinglePoint = 0, // 单点命令
- DoublePoint = 1, // 双点命令
- FloatSetpoint = 2 // 浮点设值
- };
- // 遥控命令
- struct ControlCommand {
- int address;
- ControlCommandType type;
- bool value; // 用于单点/双点命令
- float setpoint; // 用于浮点设值
- int commonAddress; // 公共地址
- int EqpmtAddress; //设备地址
- QDateTime timestamp;
- };
- // 定值数据结构
- struct FixDataValue {
- int nEqpmtId; //设备号
- int nAreaID; //定值区
- int nAddr; //定值序号
- int nType; //定值数据类型
- QString strData; //定值打印的数据
- QDateTime timestamp; //定值时间标
- };
- // 文件传输状态信息
- typedef struct {
- QString fileName;
- int fileSize;
- int currentSection;
- int totalSections;
- QDateTime startTime;
- QDateTime lastUpdate;
- FileTransferStatus status;
- //QByteArray fileData;
- QMap<int, QByteArray> fileDataMap;
- } FileTransferInfo;
- class IEC104MasterQt : public QObject
- {
- Q_OBJECT
- public:
- explicit IEC104MasterQt(QObject *parent = nullptr);
- ~IEC104MasterQt();
- bool openLink();
- void closeLink();
- void setConnectionParameters(const QString &serverIP, int port);
- // IEC104 commands
- bool sendStartDT();
- bool sendStopDT();
- bool sendTestFR();
- bool sendInterrogation(int asdu, int group);
- bool sendASDUMessage(const QByteArray &data);
- // 新增:遥控和遥调功能
- bool sendSingleCommand(int commonAddress, int objectAddress, bool command, bool selectExecute = true);
- bool sendDoubleCommand(int commonAddress, int objectAddress, int command, bool selectExecute = true);
- bool sendFloatSetpoint(int commonAddress, int objectAddress, float value, bool selectExecute = true);
- bool sendSetpointCommandNormalized(int commonAddress, int objectAddress, float value, bool selectExecute = true);
- bool sendSingleCommand2(int commonAddress, int objectAddress, bool command, bool selectExecute = true, bool isRun = true);
- bool sendDoubleCommand2(int commonAddress, int objectAddress, int command, bool selectExecute = true,bool isRun=true);
- bool sendDoubleCommand3(int commonAddress,int nEqpmtID, int objectAddress, int command, bool selectExecute = true, bool isRun = true);
- //定值处理功能
- bool getFixAreaValue(int commonAddress,int objectAddress,int nEqpmtID);//召唤当前定值区号
- bool getFixValue(int commonAddress,int objectAddress,int nEqpmtID);//召唤当前定值区定值
- bool sendFixAreaValueCommand(FixAreaCommandType nType, int commonAddress,int objectAddress,int nEqpmtID,int nFixArea);//type 0 预置 1 执行 2 撤销
- bool sendPresetFixValueCommand(int commonAddress,int objectAddress,int nEqpmtID,int nFixArea,int nIndex,int nType, QString &msg);//修改当前定值区定值预置
- bool sendSetFixValueCommand(FixAreaCommandType nType, int commonAddress,int objectAddress,int nEqpmtID,int nFixArea);//修改当前定值区定值
- //故障召唤功能
- bool getFaultReportList(int commonAddress,int objectAddress,int nEqpmtID);//召唤故障报告列表
- bool getFaultReport(int commonAddress,int objectAddress,int nEqpmtID);//召唤故障报告
- //对时
- bool sendTimeSynchronizationCommand();
- // 获取遥测数据
- QMap<int, MeasuredValue> getMeasuredValues() const;
- QMap<int,QMap<int,QMap<int,FixDataValue>>> getFixDataValues() const;
- bool getConnect() {
- return m_connected;
- };
- signals:
- void logMessage(const QString &message);
- void equipmentStatusUpdated(int address, int status,int nSingle);
- void measuredValueUpdated(int address, float value, bool valid);
- void connectionStatusChanged(bool connected);
- void dataReceived(const QByteArray &data);
- void dataSent(const QByteArray &data);
- void controlCommandConfirmed(int address, bool success, const QString &message);
- void setpointCommandConfirmed(int address, bool success, const QString &message);
- //void fixValueDataUpdated(int nEqpmtID,int address,QString strData);
- //文件读写信号
- void fileReadyReceived(const QString &fileName, int fileSize, bool ready);
- void sectionReadyReceived(const QString &fileName, int section, int sectionSize, bool ready);
- void fileSegmentReceived(const QString &fileName, int section, const QByteArray &data);
- void fileTransferCompleted(const QString &fileName, int section, int lsq, bool success, const QString &message);
- void directoryReceived(const QList<QString> &fileList);
- private slots:
- void onSocketConnected();
- void onSocketDisconnected();
- void onSocketReadyRead();
- void onSocketError(QAbstractSocket::SocketError error);
- void onTimerT1Timeout();
- void onTimerT2Timeout();
- void onTimerT3Timeout();
- void delayedStartT3Timer();
- private:
- // Socket communication
- QTcpSocket *m_socket;
- QString m_serverIP;
- int m_port;
- bool m_connected;
- // Timers
- QTimer *m_timerT1;
- QTimer *m_timerT2;
- QTimer *m_timerT3;
- // IEC104 protocol variables
- int m_rxCounter;
- int m_txCounter;
- int m_lastAckRx;
- int m_lastAckTx;
- // Parameters
- int m_t0;
- int m_t1;
- int m_t2;
- int m_t3;
- int m_k;
- int m_w;
- // Equipment status - 使用 mutable 修饰符
- mutable QMutex m_statusMutex;
- QMap<int, int> m_equipmentStatus;
- QMap<int, MeasuredValue> m_measuredValues;
- //定值
- int nSelectFixArea;
- QMap<int,QMap<int,QMap<int,FixDataValue>>> m_fixValues;
- // 遥控确认等待
- QMap<int, ControlCommand> m_pendingCommands;
- mutable QMutex m_commandMutex;
- // Protocol functions
- bool sendUMessage(int command);
- bool sendSMessage();
- bool sendIMessage(const QByteArray &data);
- void processReceivedData(const QByteArray &data);
- void decodeIMessage(const QByteArray &data);
- void processFormatU(const APCI &apci);
- //类型说明:
- //<1> : 1 字节无符号整数 UI8
- //<2> : 1 字节有符号整数 I8
- //<3> : 2 字节无符号整数 UI16
- //<4> : 2 字节有符号整数 I16
- //<5> : 4 字节无符号整数 UI32
- //<6> : 4 字节有符号整数 I32
- //<7> : 4 字节有符号浮点数 F32
- //<8> : 8字节 ASCII 字符串
- enum ReadDataType {
- TYPE_UI8 = 1, // 1字节无符号整数
- TYPE_I8 = 2, // 1字节有符号整数
- TYPE_UI16 = 3, // 2字节无符号整数
- TYPE_I16 = 4, // 2字节有符号整数
- TYPE_UI32 = 5, // 4字节无符号整数
- TYPE_I32 = 6, // 4字节有符号整数
- TYPE_F32 = 7, // 4字节浮点数
- TYPE_S64 = 8 // 8字节 ASCII 字符串
- };
- QString getDataValue(int& nPos,const QByteArray &data,int nType);
- /**
- * @brief 将二进制数据解析为对应的QString字符串
- * @param nPos 当前位置索引,解析完成后会自动更新
- * @param data 二进制数据
- * @param nType 数据类型
- * @return 转换后的字符串,如果失败返回空字符串
- */
- QString Data2Value(int& nPos, const QByteArray &data, int nType);
- bool Value2Data(QByteArray &data, int nType, const QString &strData);
- // 新增:遥控处理函数
- void processControlCommand(const ASDU_header *header, const QByteArray &data, int cause);
- void processSetpointCommand(const ASDU_header *header, const QByteArray &data, int cause);
- void sendCommandActivationTermination(int type, int commonAddress, int objectAddress);
- void sendCtrlActivationTermination(ControlCommand& pCMD);
- //定值处理函数
- void FixValueCommand(const ASDU_header *header, const QByteArray &data, int cause);
- void updateFixValue(int nEqpmtID,int nAreaID,int nAddress,int nType,QString strData);
- // Utility functions
- QByteArray toHexString(const QByteArray &data);
- void updateEquipmentStatus(int address, int status ,int nSingle);
- void updateMeasuredValue(int address, float value, bool valid, bool overflow, bool blocked, bool substituted);
- float bytesToFloat(const unsigned char* bytes);
- QDateTime cp56TimeToDateTime(const unsigned char* timeData);
- QByteArray dateTimeToCp56Time2a(const QDateTime& dateTime);
- QByteArray floatToBytes(float value);
- short floatToNormalized(float value, float scale = 1.0f);
- // 文件传输相关成员
- QMap<QString, FileTransferInfo> m_fileTransfers;
- mutable QMutex m_fileMutex;
- // 文件传输处理函数
- void processFileReady(const ASDU_header *header, const QByteArray &data, int cause);
- void processSectionReady(const ASDU_header *header, const QByteArray &data, int cause);
- void processFileCall(const ASDU_header *header, const QByteArray &data, int cause);
- void processLastSection(const ASDU_header *header, const QByteArray &data, int cause);
- void processFileAck(const ASDU_header *header, const QByteArray &data, int cause);
- void processFileSegment(const ASDU_header *header, const QByteArray &data, int cause);
- void processDirectory(const ASDU_header *header, const QByteArray &data, int cause);
- // 文件传输工具函数
- QString bytesToFileName(const unsigned char *bytes);
- QByteArray fileNameToBytes(const QString &fileName);
- int bytesToFileSize(const unsigned char *bytes);
- QByteArray fileSizeToBytes(int fileSize);
- // 记录定值数据
- QByteArray m_FixData;
- public:
- // 文件传输公共接口
- bool requestDirectory(int commonAddress,int msgAddress);
- bool selectFile(int commonAddress,int msgAddress, const QString &fileName);
- bool requestFile(int commonAddress,int msgAddress, const QString &fileName);
- bool requestSection(int commonAddress,int msgAddress, const QString &fileName, int section);
- bool acceptSection(int commonAddress,int msgAddress, const QString &fileName, int section,int lsq);
- bool acknowledgeFile(int commonAddress,int msgAddress, const QString &fileName, bool accept);
- bool acknowledgeSection(int commonAddress,int msgAddress, const QString &fileName, int section, bool accept);
- // 获取文件传输状态
- QMap<QString, FileTransferInfo> getFileTransfers() const;
- int nasdu;
- };
- #endif // IEC104MASTERQT_H
|