stulzprocess.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "stdafx.h"
  2. #include "stulzprocess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. HANDLE g_STULZRequestThread = NULL;
  7. HANDLE g_STULZResponseThread = NULL;
  8. DWORD g_dwStulzVariantTick = 0;
  9. CRITICAL_SECTION g_csStulzReadOneData;
  10. //UPS PARADIGM动态库输出函数
  11. HINSTANCE g_hSTULZLibModule=NULL;
  12. STULZ_DLLInitCom pSTULZ_DLLInitCom=NULL;
  13. STULZ_DLLInit pSTULZ_DLLInit=NULL;
  14. STULZ_DLLUnInit pSTULZ_DLLUnInit=NULL;
  15. STULZ_DLLWrCom pSTULZ_DLLWrCom=NULL;
  16. int StulzCommandSend();
  17. void UnInitStulzDll();
  18. int GetStulzFromIni(char chRs232cmd[32],
  19. char chDataLen[4],
  20. char chFuncCode[4]);
  21. BOOL LoadStulzDll(CString strpath)
  22. {
  23. char strFile[256] = {0};
  24. g_hSTULZLibModule = NULL;
  25. sprintf(strFile, "%s\\dll\\stulz.dll", strpath);
  26. InitializeCriticalSection( &g_csStulzReadOneData );
  27. g_hSTULZLibModule = AfxLoadLibrary(strFile);
  28. //STULZ 动态库初始化
  29. if (NULL != g_hSTULZLibModule)
  30. {
  31. pSTULZ_DLLInitCom =( STULZ_DLLInitCom)::GetProcAddress(g_hSTULZLibModule, "STULZ_DLLInitCom");
  32. pSTULZ_DLLInit =(STULZ_DLLInit)::GetProcAddress(g_hSTULZLibModule, "STULZ_DLLInit");
  33. pSTULZ_DLLUnInit =(STULZ_DLLUnInit)::GetProcAddress(g_hSTULZLibModule,"STULZ_DLLUnInit");
  34. pSTULZ_DLLWrCom =(STULZ_DLLWrCom)::GetProcAddress(g_hSTULZLibModule,"STULZ_DLLWrCom");
  35. return TRUE;
  36. }
  37. else
  38. {
  39. return FALSE;
  40. }
  41. }
  42. BOOL InitStulzComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  43. {
  44. if (pSTULZ_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  45. return TRUE;
  46. else
  47. return FALSE;
  48. }
  49. //在SUTLZ设备中,SETBASEPARAM结构中的参数解释:
  50. // int nAddr - 控制器ID
  51. // int nStartAddr - 如果命令ID为4,表示需读的EEPROM地址;其他的读命令ID,表示变量所在数据包中字节索引号
  52. // int nRegNum - 读取的寄存器个数, 在stulz空调协议中,它表示是数据和校验位的字节数,即协议中下面字节数
  53. // BYTE FuncCode - 命令ID
  54. int StulzRequestData(SETBASEPARAM SetBasePara,
  55. int nCmdLen,
  56. int nPort,
  57. int nDevAddr,
  58. //char chCmdLen[4],
  59. //char chPort[4],
  60. //char chAddr[4],
  61. char chDevUid[20],
  62. int iVarID,
  63. char chRs232cmd[32])
  64. {
  65. char chMsg[10] = {0};
  66. char chAdrBeginPos[4] = {0};
  67. char chDataLen[4] = {0};//总数据的长度
  68. char chStartAddr[2] = {0};//起始地址
  69. char chNum[2] = {0};//读取的寄存器个数
  70. char FuncCode[4] = {0};//功能码
  71. GetStulzFromIni((char*)chRs232cmd,
  72. chDataLen,
  73. FuncCode);
  74. SetBasePara.FuncCode = (BYTE)atoi(FuncCode);
  75. if (SetBasePara.FuncCode==1 || //长状态请求1
  76. SetBasePara.FuncCode==33 || //长状态请求2
  77. SetBasePara.FuncCode==9 || //控制器状态2
  78. SetBasePara.FuncCode==10 ||//控制器ID
  79. SetBasePara.FuncCode==11 ||//单元配置
  80. SetBasePara.FuncCode==12)//c5000增加项
  81. {
  82. sprintf(chAdrBeginPos, "%d", SetBasePara.nStartAddr);
  83. }
  84. int nRet = pSTULZ_DLLInit( nPort,
  85. nDevAddr,
  86. SetBasePara,
  87. atoi(chDataLen),
  88. atoi(chAdrBeginPos),
  89. nCmdLen,
  90. chMsg );
  91. if(nRet==0)
  92. {
  93. nRet = StulzSingleResponseData(chDevUid, iVarID, chMsg);
  94. }
  95. return nRet;
  96. }
  97. int StulzSingleResponseData(char chDevUid[20], int iVarID, char chMsg[10])
  98. {
  99. int nDeviceIndex = -1, nVarIndex = -1;
  100. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  101. if( bFind == FALSE ) return -1;
  102. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  103. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  104. EnterCriticalSection(&g_csStulzReadOneData);
  105. pDev->m_dwOnlineTick = GetTickCount();
  106. LeaveCriticalSection(&g_csStulzReadOneData);
  107. DWORD nValue = 0;
  108. WORD wdValue = 0;
  109. double fCoef = pBaseVar->m_dbCoefficient;
  110. int nDataLen = pBaseVar->m_iDataLen;
  111. int nVarItemID = pBaseVar->m_nVarItemID;
  112. if( nVarItemID > 0 )//BIT位变量
  113. {
  114. int nStartBit = pBaseVar->m_nStartBit;
  115. int nEndBit = pBaseVar->m_nEndBit;
  116. int nStartReg, nEndReg;
  117. if( nEndBit - nStartBit < 7 )// 一般告警状态会这样子定义,Modbus Rtu 码暂时没有碰到,没有经过调试,待测试
  118. {
  119. if( nDataLen == 1 )
  120. {
  121. char chBuffer[8] = {0};
  122. memset(chBuffer, 0, sizeof(chBuffer));
  123. //nValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[0 + 1]) & 0x00FF );
  124. nValue = chMsg[0];
  125. DigitToBinary(chMsg[0], chBuffer, 8) ;
  126. //itoa(nValue, chBuffer, 2);
  127. CString strTemp;
  128. switch( nEndBit - nStartBit )
  129. {
  130. case 0:
  131. strTemp.Format("%c", chBuffer[nStartBit]);
  132. break;
  133. case 1:
  134. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  135. break;
  136. case 2:
  137. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  138. break;
  139. case 3:
  140. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  141. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  142. break;
  143. case 4:
  144. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  145. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  146. break;
  147. case 5:
  148. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  149. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  150. break;
  151. case 6:
  152. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  153. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  154. break;
  155. }
  156. nValue = atoi(strTemp);
  157. //strTemp.Format("%d",
  158. }
  159. else if( nDataLen == 2 )
  160. {
  161. char chBuffer[16] = {0};
  162. memset(chBuffer, 0, sizeof(chBuffer));
  163. memcpy(&wdValue, chMsg, sizeof(WORD));
  164. itoa(wdValue, chBuffer, 2);
  165. CString strTemp;
  166. switch( nEndBit - nStartBit )
  167. {
  168. case 0:
  169. strTemp.Format("%c", chBuffer[nStartBit]);
  170. break;
  171. case 1:
  172. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  173. break;
  174. case 2:
  175. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  176. break;
  177. case 3:
  178. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  179. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  180. break;
  181. case 4:
  182. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  183. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  184. break;
  185. case 5:
  186. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  187. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  188. break;
  189. case 6:
  190. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  191. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  192. break;
  193. }
  194. nValue = atoi(strTemp);
  195. }
  196. }
  197. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  198. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  199. {
  200. nStartReg = (int)nStartBit / 8;
  201. nEndReg = (int)(nEndBit + 1) / 8;
  202. nValue = chMsg[nStartReg];
  203. }
  204. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  205. {
  206. nStartReg = (int)nStartBit / 8;
  207. nEndReg = (int)(nEndBit + 1) / 8;
  208. memcpy(&wdValue, chMsg, sizeof(WORD));
  209. nValue = (DWORD)ntohs(wdValue);
  210. }
  211. EnterCriticalSection( &g_csStulzReadOneData );
  212. pBaseVar->m_dbData = (double)(nValue * fCoef);
  213. if( pBaseVar->m_nRearm != 0 )
  214. {
  215. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  216. }
  217. LeaveCriticalSection( &g_csStulzReadOneData );
  218. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  219. }
  220. else
  221. {
  222. // 联合类型变量做特殊处理
  223. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  224. {
  225. if( nDataLen == 2 )
  226. {
  227. //int类型
  228. union __UNION_VAR_INT{
  229. char ch[2];
  230. int value;
  231. }unionVarInt;
  232. unionVarInt.ch[0] = chMsg[1];
  233. unionVarInt.ch[1] = chMsg[0];
  234. EnterCriticalSection( &g_csStulzReadOneData );
  235. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  236. if( pBaseVar->m_nRearm != 0 )
  237. {
  238. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  239. }
  240. LeaveCriticalSection( &g_csStulzReadOneData );
  241. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  242. }
  243. else if( nDataLen == 4 )
  244. {
  245. //float类型
  246. union __UNION_VAR_FLOAT{
  247. char ch[4];
  248. float value;
  249. }unionVarFloat;
  250. unionVarFloat.ch[0] = chMsg[3];
  251. unionVarFloat.ch[1] = chMsg[2];
  252. unionVarFloat.ch[2] = chMsg[1];
  253. unionVarFloat.ch[3] = chMsg[0];
  254. EnterCriticalSection( &g_csStulzReadOneData );
  255. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  256. if( pBaseVar->m_nRearm != 0 )
  257. {
  258. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  259. }
  260. LeaveCriticalSection( &g_csStulzReadOneData );
  261. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  262. }
  263. else if( nDataLen == 8 )
  264. {
  265. //double类型
  266. union __UNION_VAR_DOUBLE{
  267. char ch[8];
  268. double value;
  269. }unionVarDouble;
  270. unionVarDouble.ch[0] = chMsg[7];
  271. unionVarDouble.ch[1] = chMsg[6];
  272. unionVarDouble.ch[2] = chMsg[5];
  273. unionVarDouble.ch[3] = chMsg[4];
  274. unionVarDouble.ch[4] = chMsg[3];
  275. unionVarDouble.ch[5] = chMsg[2];
  276. unionVarDouble.ch[6] = chMsg[1];
  277. unionVarDouble.ch[7] = chMsg[0];
  278. EnterCriticalSection( &g_csStulzReadOneData );
  279. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  280. if( pBaseVar->m_nRearm != 0 )
  281. {
  282. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  283. }
  284. LeaveCriticalSection( &g_csStulzReadOneData );
  285. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  286. }
  287. }
  288. else
  289. {
  290. switch( nDataLen )
  291. {
  292. case 1:
  293. nValue = chMsg[0];
  294. break;
  295. case 2:
  296. WORD wdValue;
  297. memcpy(&wdValue, chMsg, sizeof(WORD));
  298. nValue = wdValue;
  299. break;
  300. case 3: // 一般为字符串类型,不作处理
  301. break;
  302. case 4: // 保留,暂时不知道什么处理
  303. break;
  304. case 5: // 一般为字符串类型,不作处理
  305. break;
  306. case 6: // 一般为字符串类型,不作处理
  307. break;
  308. case 7: // 一般为字符串类型,不作处理
  309. break;
  310. case 8: // 保留,暂时没有用到
  311. break;
  312. }
  313. EnterCriticalSection( &g_csStulzReadOneData );
  314. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  315. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  316. {
  317. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  318. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  319. pBaseVar->m_dbData = nValue *
  320. fCoef *
  321. fScale;
  322. }
  323. else
  324. {
  325. pBaseVar->m_dbData = (double)(nValue * fCoef);
  326. }
  327. if( pBaseVar->m_nRearm != 0 )
  328. {
  329. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  330. }
  331. //g_RtuReadOneData.dbData = (double)(nValue * fCoef);
  332. LeaveCriticalSection( &g_csStulzReadOneData );
  333. //TRACE2("普通变量ID%d, 值=%f\r\n", iVarID, g_StulzReadOneData.dbData);
  334. }
  335. }
  336. EnterCriticalSection(&g_csStulzReadOneData);
  337. pDev->m_dwOnlineTick = GetTickCount();
  338. LeaveCriticalSection(&g_csStulzReadOneData);
  339. return 0;
  340. }
  341. int RequestWrStulzData(char chDevUid[20], int iVarID, double data)//用于请求写命令操作
  342. {
  343. int nDeviceIndex = -1, nVarIndex = -1;
  344. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  345. if( bFind == FALSE ) return -1;
  346. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  347. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  348. char chMsg[80] = {0};
  349. BYTE Start= 0x0;//起始位
  350. BYTE StartAddr[3] = {0};
  351. BYTE FuncCode[3] = {0};
  352. BYTE byWithAddrFlag = 0;
  353. int nLen = 0;
  354. chMsg[0] = pBaseVar->m_nRegStartAddr;
  355. chMsg[1] = 7;
  356. chMsg[2] = 3;
  357. char chdata=0x0;
  358. switch(pBaseVar->m_iDataLen)
  359. {
  360. case 1:
  361. {
  362. union __Rensitivity1{
  363. char ch;
  364. char f;
  365. }unionRensitivity1;
  366. unionRensitivity1.f = (char)data;
  367. chMsg[3] = ByteToAscii((unionRensitivity1.ch) & 0x0f);
  368. break;
  369. }
  370. }
  371. char ResDataBuffer[2] = {0};
  372. if( pSTULZ_DLLWrCom(pDev->m_iPort, pDev->m_iDevideaddr, chMsg, ResDataBuffer) == 0)
  373. return 0;
  374. else
  375. return -1;
  376. }
  377. void UnInitStulzDll()
  378. {
  379. if (g_hSTULZLibModule != NULL && ( NULL != pSTULZ_DLLUnInit ))
  380. {
  381. pSTULZ_DLLUnInit();
  382. }
  383. DeleteCriticalSection( &g_csStulzReadOneData );
  384. if (g_hSTULZLibModule!= NULL)
  385. {
  386. AfxFreeLibrary(g_hSTULZLibModule);
  387. g_hSTULZLibModule = NULL;
  388. }
  389. }
  390. int GetStulzFromIni(char chRs232cmd[32],
  391. char chDataLen[4], char chFuncCode[4])
  392. {
  393. char strFile[1024] = {0};
  394. char strValue[128]={0};
  395. wsprintf(strFile, "%s\\config\\stulz.ini", g_strDirectory);
  396. //命令模块
  397. GetPrivateProfileString("CMD",chRs232cmd, "", strValue, sizeof(strValue), strFile);
  398. //数据总长度
  399. GetPrivateProfileString(strValue, "datanum", "", chDataLen, sizeof(chDataLen), strFile);
  400. //命令ID
  401. GetPrivateProfileString(strValue, "cmdid", "", chFuncCode, 4, strFile);
  402. return 0;
  403. }