iec104masterqt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #ifndef IEC104MASTERQT_H
  2. #define IEC104MASTERQT_H
  3. #include <QObject>
  4. #include <QTcpSocket>
  5. #include <QTimer>
  6. #include <QMap>
  7. #include <QMutex>
  8. #include <QAbstractSocket>
  9. #include "IEC104Types.h"
  10. #include <QDateTime>
  11. // 遥测数据结构
  12. struct MeasuredValue {
  13. float value;
  14. bool valid;
  15. bool overflow;
  16. bool blocked;
  17. bool substituted;
  18. QDateTime timestamp;
  19. };
  20. // 遥控命令类型
  21. enum class ControlCommandType {
  22. SinglePoint = 0, // 单点命令
  23. DoublePoint = 1, // 双点命令
  24. FloatSetpoint = 2 // 浮点设值
  25. };
  26. // 遥控命令
  27. struct ControlCommand {
  28. int address;
  29. ControlCommandType type;
  30. bool value; // 用于单点/双点命令
  31. float setpoint; // 用于浮点设值
  32. int commonAddress; // 公共地址
  33. int EqpmtAddress; //设备地址
  34. QDateTime timestamp;
  35. };
  36. // 定值数据结构
  37. struct FixDataValue {
  38. int nEqpmtId; //设备号
  39. int nAreaID; //定值区
  40. int nAddr; //定值序号
  41. int nType; //定值数据类型
  42. QString strData; //定值打印的数据
  43. QDateTime timestamp; //定值时间标
  44. };
  45. // 文件传输状态信息
  46. typedef struct {
  47. QString fileName;
  48. int fileSize;
  49. int currentSection;
  50. int totalSections;
  51. QDateTime startTime;
  52. QDateTime lastUpdate;
  53. FileTransferStatus status;
  54. //QByteArray fileData;
  55. QMap<int, QByteArray> fileDataMap;
  56. } FileTransferInfo;
  57. class IEC104MasterQt : public QObject
  58. {
  59. Q_OBJECT
  60. public:
  61. explicit IEC104MasterQt(QObject *parent = nullptr);
  62. ~IEC104MasterQt();
  63. bool openLink();
  64. void closeLink();
  65. void setConnectionParameters(const QString &serverIP, int port);
  66. // IEC104 commands
  67. bool sendStartDT();
  68. bool sendStopDT();
  69. bool sendTestFR();
  70. bool sendInterrogation(int asdu, int group);
  71. bool sendASDUMessage(const QByteArray &data);
  72. // 新增:遥控和遥调功能
  73. bool sendSingleCommand(int commonAddress, int objectAddress, bool command, bool selectExecute = true);
  74. bool sendDoubleCommand(int commonAddress, int objectAddress, int command, bool selectExecute = true);
  75. bool sendFloatSetpoint(int commonAddress, int objectAddress, float value, bool selectExecute = true);
  76. bool sendSetpointCommandNormalized(int commonAddress, int objectAddress, float value, bool selectExecute = true);
  77. bool sendSingleCommand2(int commonAddress, int objectAddress, bool command, bool selectExecute = true, bool isRun = true);
  78. bool sendDoubleCommand2(int commonAddress, int objectAddress, int command, bool selectExecute = true,bool isRun=true);
  79. bool sendDoubleCommand3(int commonAddress,int nEqpmtID, int objectAddress, int command, bool selectExecute = true, bool isRun = true);
  80. //定值处理功能
  81. bool getFixAreaValue(int commonAddress,int objectAddress,int nEqpmtID);//召唤当前定值区号
  82. bool getFixValue(int commonAddress,int objectAddress,int nEqpmtID);//召唤当前定值区定值
  83. bool sendFixAreaValueCommand(FixAreaCommandType nType, int commonAddress,int objectAddress,int nEqpmtID,int nFixArea);//type 0 预置 1 执行 2 撤销
  84. bool sendPresetFixValueCommand(int commonAddress,int objectAddress,int nEqpmtID,int nFixArea,int nIndex,int nType, QString &msg);//修改当前定值区定值预置
  85. bool sendSetFixValueCommand(FixAreaCommandType nType, int commonAddress,int objectAddress,int nEqpmtID,int nFixArea);//修改当前定值区定值
  86. //故障召唤功能
  87. bool getFaultReportList(int commonAddress,int objectAddress,int nEqpmtID);//召唤故障报告列表
  88. bool getFaultReport(int commonAddress,int objectAddress,int nEqpmtID);//召唤故障报告
  89. //对时
  90. bool sendTimeSynchronizationCommand();
  91. // 获取遥测数据
  92. QMap<int, MeasuredValue> getMeasuredValues() const;
  93. QMap<int,QMap<int,QMap<int,FixDataValue>>> getFixDataValues() const;
  94. bool getConnect() {
  95. return m_connected;
  96. };
  97. signals:
  98. void logMessage(const QString &message);
  99. void equipmentStatusUpdated(int address, int status,int nSingle);
  100. void measuredValueUpdated(int address, float value, bool valid);
  101. void connectionStatusChanged(bool connected);
  102. void dataReceived(const QByteArray &data);
  103. void dataSent(const QByteArray &data);
  104. void controlCommandConfirmed(int address, bool success, const QString &message);
  105. void setpointCommandConfirmed(int address, bool success, const QString &message);
  106. //void fixValueDataUpdated(int nEqpmtID,int address,QString strData);
  107. //文件读写信号
  108. void fileReadyReceived(const QString &fileName, int fileSize, bool ready);
  109. void sectionReadyReceived(const QString &fileName, int section, int sectionSize, bool ready);
  110. void fileSegmentReceived(const QString &fileName, int section, const QByteArray &data);
  111. void fileTransferCompleted(const QString &fileName, int section, int lsq, bool success, const QString &message);
  112. void directoryReceived(const QList<QString> &fileList);
  113. private slots:
  114. void onSocketConnected();
  115. void onSocketDisconnected();
  116. void onSocketReadyRead();
  117. void onSocketError(QAbstractSocket::SocketError error);
  118. void onTimerT1Timeout();
  119. void onTimerT2Timeout();
  120. void onTimerT3Timeout();
  121. void delayedStartT3Timer();
  122. private:
  123. // Socket communication
  124. QTcpSocket *m_socket;
  125. QString m_serverIP;
  126. int m_port;
  127. bool m_connected;
  128. // Timers
  129. QTimer *m_timerT1;
  130. QTimer *m_timerT2;
  131. QTimer *m_timerT3;
  132. // IEC104 protocol variables
  133. int m_rxCounter;
  134. int m_txCounter;
  135. int m_lastAckRx;
  136. int m_lastAckTx;
  137. // Parameters
  138. int m_t0;
  139. int m_t1;
  140. int m_t2;
  141. int m_t3;
  142. int m_k;
  143. int m_w;
  144. // Equipment status - 使用 mutable 修饰符
  145. mutable QMutex m_statusMutex;
  146. QMap<int, int> m_equipmentStatus;
  147. QMap<int, MeasuredValue> m_measuredValues;
  148. //定值
  149. int nSelectFixArea;
  150. QMap<int,QMap<int,QMap<int,FixDataValue>>> m_fixValues;
  151. // 遥控确认等待
  152. QMap<int, ControlCommand> m_pendingCommands;
  153. mutable QMutex m_commandMutex;
  154. // Protocol functions
  155. bool sendUMessage(int command);
  156. bool sendSMessage();
  157. bool sendIMessage(const QByteArray &data);
  158. void processReceivedData(const QByteArray &data);
  159. void decodeIMessage(const QByteArray &data);
  160. void processFormatU(const APCI &apci);
  161. //类型说明:
  162. //<1> : 1 字节无符号整数 UI8
  163. //<2> : 1 字节有符号整数 I8
  164. //<3> : 2 字节无符号整数 UI16
  165. //<4> : 2 字节有符号整数 I16
  166. //<5> : 4 字节无符号整数 UI32
  167. //<6> : 4 字节有符号整数 I32
  168. //<7> : 4 字节有符号浮点数 F32
  169. //<8> : 8字节 ASCII 字符串
  170. enum ReadDataType {
  171. TYPE_UI8 = 1, // 1字节无符号整数
  172. TYPE_I8 = 2, // 1字节有符号整数
  173. TYPE_UI16 = 3, // 2字节无符号整数
  174. TYPE_I16 = 4, // 2字节有符号整数
  175. TYPE_UI32 = 5, // 4字节无符号整数
  176. TYPE_I32 = 6, // 4字节有符号整数
  177. TYPE_F32 = 7, // 4字节浮点数
  178. TYPE_S64 = 8 // 8字节 ASCII 字符串
  179. };
  180. QString getDataValue(int& nPos,const QByteArray &data,int nType);
  181. /**
  182. * @brief 将二进制数据解析为对应的QString字符串
  183. * @param nPos 当前位置索引,解析完成后会自动更新
  184. * @param data 二进制数据
  185. * @param nType 数据类型
  186. * @return 转换后的字符串,如果失败返回空字符串
  187. */
  188. QString Data2Value(int& nPos, const QByteArray &data, int nType);
  189. bool Value2Data(QByteArray &data, int nType, const QString &strData);
  190. // 新增:遥控处理函数
  191. void processControlCommand(const ASDU_header *header, const QByteArray &data, int cause);
  192. void processSetpointCommand(const ASDU_header *header, const QByteArray &data, int cause);
  193. void sendCommandActivationTermination(int type, int commonAddress, int objectAddress);
  194. void sendCtrlActivationTermination(ControlCommand& pCMD);
  195. //定值处理函数
  196. void FixValueCommand(const ASDU_header *header, const QByteArray &data, int cause);
  197. void updateFixValue(int nEqpmtID,int nAreaID,int nAddress,int nType,QString strData);
  198. // Utility functions
  199. QByteArray toHexString(const QByteArray &data);
  200. void updateEquipmentStatus(int address, int status ,int nSingle);
  201. void updateMeasuredValue(int address, float value, bool valid, bool overflow, bool blocked, bool substituted);
  202. float bytesToFloat(const unsigned char* bytes);
  203. QDateTime cp56TimeToDateTime(const unsigned char* timeData);
  204. QByteArray dateTimeToCp56Time2a(const QDateTime& dateTime);
  205. QByteArray floatToBytes(float value);
  206. short floatToNormalized(float value, float scale = 1.0f);
  207. // 文件传输相关成员
  208. QMap<QString, FileTransferInfo> m_fileTransfers;
  209. mutable QMutex m_fileMutex;
  210. // 文件传输处理函数
  211. void processFileReady(const ASDU_header *header, const QByteArray &data, int cause);
  212. void processSectionReady(const ASDU_header *header, const QByteArray &data, int cause);
  213. void processFileCall(const ASDU_header *header, const QByteArray &data, int cause);
  214. void processLastSection(const ASDU_header *header, const QByteArray &data, int cause);
  215. void processFileAck(const ASDU_header *header, const QByteArray &data, int cause);
  216. void processFileSegment(const ASDU_header *header, const QByteArray &data, int cause);
  217. void processDirectory(const ASDU_header *header, const QByteArray &data, int cause);
  218. // 文件传输工具函数
  219. QString bytesToFileName(const unsigned char *bytes);
  220. QByteArray fileNameToBytes(const QString &fileName);
  221. int bytesToFileSize(const unsigned char *bytes);
  222. QByteArray fileSizeToBytes(int fileSize);
  223. // 记录定值数据
  224. QByteArray m_FixData;
  225. public:
  226. // 文件传输公共接口
  227. bool requestDirectory(int commonAddress,int msgAddress);
  228. bool selectFile(int commonAddress,int msgAddress, const QString &fileName);
  229. bool requestFile(int commonAddress,int msgAddress, const QString &fileName);
  230. bool requestSection(int commonAddress,int msgAddress, const QString &fileName, int section);
  231. bool acceptSection(int commonAddress,int msgAddress, const QString &fileName, int section,int lsq);
  232. bool acknowledgeFile(int commonAddress,int msgAddress, const QString &fileName, bool accept);
  233. bool acknowledgeSection(int commonAddress,int msgAddress, const QString &fileName, int section, bool accept);
  234. // 获取文件传输状态
  235. QMap<QString, FileTransferInfo> getFileTransfers() const;
  236. int nasdu;
  237. };
  238. #endif // IEC104MASTERQT_H