Separate.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "stdafx.h"
  2. #include "Global.h"
  3. #include "struct.h"
  4. #include "NoticeQueue.h"
  5. #include "Separate.h"
  6. HANDLE g_SeparateRequestThread = NULL;
  7. HANDLE g_SeparateResponseThread = NULL;
  8. DWORD g_dwSeparateVariantTick = 0;
  9. CRITICAL_SECTION g_csSeparateReadOneData;
  10. //icpdas动态库输出函数
  11. HINSTANCE g_hSeparateLibModule = NULL;
  12. SEPARATE_DLLInit pSeparate_DLLInit = NULL;
  13. SEPARATE_DLLUnInit pSeparate_DLLUnInit;
  14. SEPARATE_DLLRead pSeparate_DLLRead = NULL;
  15. SEPARATE_DLLWrite pSeparate_DLLWrite = NULL;
  16. BOOL LoadSeparateDll(CString strpath)
  17. {
  18. char strFile[256] = {0};
  19. g_hSeparateLibModule = NULL;
  20. sprintf(strFile, "%s\\dll\\Separate.dll", strpath);
  21. g_hSeparateLibModule = AfxLoadLibrary(strFile);
  22. InitializeCriticalSection( &g_csSeparateReadOneData );
  23. //Separate 动态库初始化
  24. if (NULL != g_hSeparateLibModule)
  25. {
  26. pSeparate_DLLInit = (SEPARATE_DLLInit)::GetProcAddress(g_hSeparateLibModule, "SEPARATE_DLLInit");
  27. pSeparate_DLLUnInit = (SEPARATE_DLLUnInit)::GetProcAddress(g_hSeparateLibModule, "SEPARATE_DLLUnInit");
  28. pSeparate_DLLRead = (SEPARATE_DLLRead)::GetProcAddress(g_hSeparateLibModule, "SEPARATE_DLLRead");
  29. pSeparate_DLLWrite = (SEPARATE_DLLWrite)::GetProcAddress(g_hSeparateLibModule, "SEPARATE_DLLWrite");
  30. return TRUE;
  31. }
  32. else
  33. {
  34. return FALSE;
  35. }
  36. }
  37. BOOL InitSeparateComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  38. {
  39. if (pSeparate_DLLInit(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  40. return TRUE;
  41. else
  42. return FALSE;
  43. }
  44. int SeparateRequestData(
  45. int nPort,
  46. int nDevAddr,
  47. char szDevUid[20],
  48. int iVarID,
  49. char szRs232cmd[MAX_CMD],
  50. char szIniName[MAX_PATH])
  51. {
  52. char szMsg[MAX_VAR_MSG] = {0};
  53. if( pSeparate_DLLRead(nPort, nDevAddr, g_strDirectory, szIniName, szRs232cmd, szMsg)==0)
  54. {
  55. }
  56. else
  57. {
  58. return -1;
  59. }
  60. return SeparateSingleResponseData(szDevUid, iVarID, szMsg);
  61. //return -1;
  62. }
  63. int SeparateSingleResponseData(char chDevUid[20], int iVarID, char chMsg[MAX_VAR_MSG])
  64. {
  65. int nDeviceIndex = -1, nVarIndex = -1;
  66. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  67. if( bFind == FALSE ) return -1;
  68. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  69. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  70. EnterCriticalSection(&g_csSeparateReadOneData);
  71. pDev->m_dwOnlineTick = GetTickCount();
  72. LeaveCriticalSection(&g_csSeparateReadOneData);
  73. DWORD nValue = 0;
  74. WORD wdValue = 0;
  75. double fCoef = pBaseVar->m_dbCoefficient;
  76. int nDataLen = pBaseVar->m_iDataLen;
  77. int nVarItemID = pBaseVar->m_nVarItemID;
  78. if( nVarItemID > 0 )//BIT位变量
  79. {
  80. int nStartBit = pBaseVar->m_nStartBit;
  81. int nEndBit = pBaseVar->m_nEndBit;
  82. if( nEndBit - nStartBit < 7 ) // 一般告警状态会这样子定义,Modbus Ascii码暂时没有碰到,没有经过调试,待测试
  83. {
  84. CString strTemp;
  85. switch( nEndBit - nStartBit )
  86. {
  87. case 0:
  88. strTemp.Format("%d", chMsg[nStartBit] - 30);
  89. break;
  90. }
  91. nValue = atoi(strTemp);
  92. }
  93. EnterCriticalSection( &g_csSeparateReadOneData );
  94. pBaseVar->m_dbData = (double)(nValue * fCoef);
  95. if( pBaseVar->m_nRearm != 0 )
  96. {
  97. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  98. }
  99. LeaveCriticalSection( &g_csSeparateReadOneData );
  100. //TRACE2("变量ID%d, 值=%d\r\n", iVarID, nValue);
  101. }
  102. else
  103. {
  104. CString strMsg = CString(chMsg);
  105. // 联合类型变量做特殊处理(Separate没有联合类型)
  106. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  107. {
  108. }
  109. else if( nDataLen == 1 )
  110. {
  111. EnterCriticalSection( &g_csSeparateReadOneData );
  112. pBaseVar->m_dbData = (double)(atoi(strMsg) * fCoef);
  113. if( pBaseVar->m_nRearm != 0 )
  114. {
  115. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  116. }
  117. LeaveCriticalSection( &g_csSeparateReadOneData );
  118. }
  119. else
  120. {
  121. EnterCriticalSection( &g_csSeparateReadOneData );
  122. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  123. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  124. {
  125. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  126. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  127. pBaseVar->m_dbData = atof(strMsg) *
  128. fCoef *
  129. fScale;
  130. }
  131. else
  132. {
  133. pBaseVar->m_dbData = (double)(atof(strMsg) * fCoef);
  134. }
  135. if( pBaseVar->m_nRearm != 0 )
  136. {
  137. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  138. }
  139. LeaveCriticalSection( &g_csSeparateReadOneData );
  140. //g_IcpdasReadOneData.dbData = (double)(nValue * fCoef);
  141. //TRACE2("普通变量ID%d, 值=%d\r\n", iVarID, g_IcpdasReadOneData.dbData);
  142. }
  143. }
  144. EnterCriticalSection(&g_csSeparateReadOneData);
  145. pDev->m_dwOnlineTick = GetTickCount();
  146. LeaveCriticalSection(&g_csSeparateReadOneData);
  147. return 0;
  148. }
  149. void UnInitSeparateDll()
  150. {
  151. if (g_hSeparateLibModule != NULL && ( NULL != pSeparate_DLLUnInit ))
  152. {
  153. pSeparate_DLLUnInit();
  154. }
  155. DeleteCriticalSection( &g_csSeparateReadOneData );
  156. if (g_hSeparateLibModule!= NULL)
  157. {
  158. AfxFreeLibrary(g_hSeparateLibModule);
  159. g_hSeparateLibModule = NULL;
  160. }
  161. }