MostDevice.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "stdafx.h"
  2. #include "Global.h"
  3. #include "struct.h"
  4. #include "NoticeQueue.h"
  5. #include "MostDevice.h"
  6. #include "Syslib.h"
  7. CRITICAL_SECTION g_csMostDeviceReadOneData;
  8. //Most Device动态库输出函数
  9. HINSTANCE g_hMostDeviceLibModule = NULL;
  10. DLL_MostDeviceInit pDllMostDeviceInit = NULL;
  11. DLL_MostDeviceUnInit pDllMostDeviceUnInit = NULL;
  12. DLL_MostDeviceRead pDllMostDeviceRead = NULL;
  13. DLL_MostDeviceSet pDllMostDeviceSet = NULL;
  14. BOOL LoadMostDeviceDll(CString strpath)
  15. {
  16. char strFile[256] = {0};
  17. g_hMostDeviceLibModule = NULL;
  18. sprintf(strFile, "%s\\dll\\MostDevice.dll", strpath);
  19. g_hMostDeviceLibModule = AfxLoadLibrary(strFile);
  20. InitializeCriticalSection( &g_csMostDeviceReadOneData );
  21. //Separate 动态库初始化
  22. if (NULL != g_hMostDeviceLibModule)
  23. {
  24. pDllMostDeviceInit = (DLL_MostDeviceInit)::GetProcAddress(g_hMostDeviceLibModule, "DLL_MostDeviceInit");
  25. pDllMostDeviceUnInit = (DLL_MostDeviceUnInit)::GetProcAddress(g_hMostDeviceLibModule, "DLL_MostDeviceUnInit");
  26. pDllMostDeviceRead = (DLL_MostDeviceRead)::GetProcAddress(g_hMostDeviceLibModule, "DLL_MostDeviceRead");
  27. pDllMostDeviceSet = (DLL_MostDeviceSet)::GetProcAddress(g_hMostDeviceLibModule, "DLL_MostDeviceSet");
  28. return TRUE;
  29. }
  30. else
  31. {
  32. return FALSE;
  33. }
  34. }
  35. BOOL InitMostDeviceComm(
  36. char szPath[MAX_PATH], // 程序所在路径
  37. char szIniName[MAX_PATH], // 配置文件名称
  38. int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  39. {
  40. if (pDllMostDeviceInit(szPath, szIniName, iPort,iAddr, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  41. {
  42. return TRUE;
  43. }
  44. else
  45. {
  46. return FALSE;
  47. }
  48. }
  49. int MostDeviceRequestData(
  50. int nPort,
  51. int nDevAddr,
  52. char szDevUid[20],
  53. int iVarID,
  54. char szRs232cmd[MAX_CMD],
  55. char szIniName[MAX_PATH])
  56. {
  57. int nReversed1 = 0; // 预留整形参数1接口
  58. int nReversed2 = 0; // 预留整形参数2接口
  59. int nReversed3 = 0; // 预留整形参数3接口
  60. int nReversed4 = 0; // 预留整形参数4接口
  61. int nReversed5 = 0; // 预留整形参数5接口
  62. float fReversed1 = 0.0; // 预留float参数1接口
  63. float fReversed2 = 0.0; // 预留float参数2接口
  64. float fReversed3 = 0.0; // 预留float参数3接口
  65. char szReversed1[MAX_RESERVED1] = {0}; // 预留字符数组参数1接口
  66. char szReversed2[MAX_RESERVED2] = {0}; // 预留字符数组参数2接口
  67. char szReversed3[MAX_RESERVED3] = {0}; // 预留字符数组参数3接口
  68. char szReversed4[MAX_RESERVED4] = {0}; // 预留字符数组参数4接口
  69. char szReversed5[MAX_RESERVED5] = {0}; // 预留字符数组参数5接口
  70. char szMsg[MAX_VAR_MSG] = {0};
  71. int nRet = pDllMostDeviceRead(g_strDirectory, szIniName, nPort, nDevAddr,
  72. szRs232cmd, szMsg, nReversed1, nReversed2, nReversed3, nReversed4, nReversed5,
  73. fReversed1, fReversed2, fReversed3,
  74. szReversed1, szReversed2, szReversed3, szReversed4, szReversed5);
  75. if( nRet ==0 )
  76. {
  77. }
  78. else
  79. {
  80. return -1;
  81. }
  82. return MostDeviceSingleResponseData(szDevUid, iVarID, szMsg);
  83. //return -1;
  84. }
  85. // 通过采集到的值,赋值变量;
  86. int MostDeviceSingleResponseData(char chDevUid[20], int iVarID, char chMsg[MAX_VAR_MSG])
  87. {
  88. //*************************通过设备UID,变量ID,获取相应的设备序号,变量索引ID,进行变量赋值*************************
  89. int nDeviceIndex = -1, nVarIndex = -1;
  90. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  91. if( bFind == FALSE ) return -1;
  92. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  93. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  94. EnterCriticalSection(&g_csMostDeviceReadOneData);
  95. pDev->m_dwOnlineTick = GetTickCount();
  96. LeaveCriticalSection(&g_csMostDeviceReadOneData);
  97. DWORD nValue = 0;
  98. WORD wdValue = 0;
  99. // *********获取转换系数,数据长度,变量子ID**************
  100. double fCoef = pBaseVar->m_dbCoefficient; // 转换系数;
  101. int nDataLen = pBaseVar->m_iDataLen; // 有效位数;
  102. int nVarItemID = pBaseVar->m_nVarItemID; // 取字节位;
  103. CString strMsg = CString(chMsg);
  104. EnterCriticalSection( &g_csMostDeviceReadOneData );
  105. // m_nMaxValues 最大值,m_nMinValues 最小值 ; m_nMaxConvtRate 最小转换比率 , m_nMinConvtRate 最小转换比率;
  106. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 || pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  107. {
  108. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) / (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  109. pBaseVar->m_dbData = atof(strMsg) * fCoef * fScale;
  110. }
  111. else
  112. {
  113. pBaseVar->m_dbData = (double)(atof(strMsg) * fCoef);
  114. }
  115. // 校正值 m_nRearm;
  116. if( pBaseVar->m_nRearm != 0 )
  117. {
  118. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  119. }
  120. LeaveCriticalSection( &g_csMostDeviceReadOneData );
  121. EnterCriticalSection(&g_csMostDeviceReadOneData);
  122. pDev->m_dwOnlineTick = GetTickCount();
  123. //LOG4C((LOG_NOTICE, "pDev->m_dwOnlineTick=%d", pDev->m_dwOnlineTick));
  124. LeaveCriticalSection(&g_csMostDeviceReadOneData);
  125. return 0;
  126. }
  127. int MostDeviceRequestSet(
  128. int nPort,
  129. int nDevAddr,
  130. char szDevUid[20],
  131. int iVarID,
  132. char szRs232cmd[MAX_CMD],
  133. char szIniName[MAX_PATH],
  134. char szMsg[MAX_VAR_MSG])
  135. {
  136. int nReversed1 = 0; // 预留整形参数1接口
  137. int nReversed2 = 0; // 预留整形参数2接口
  138. int nReversed3 = 0; // 预留整形参数3接口
  139. int nReversed4 = 0; // 预留整形参数4接口
  140. int nReversed5 = 0; // 预留整形参数5接口
  141. float fReversed1 = 0.0; // 预留float参数1接口
  142. float fReversed2 = 0.0; // 预留float参数2接口
  143. float fReversed3 = 0.0; // 预留float参数3接口
  144. char szReversed1[MAX_RESERVED1] = {0}; // 预留字符数组参数1接口
  145. char szReversed2[MAX_RESERVED2] = {0}; // 预留字符数组参数2接口
  146. char szReversed3[MAX_RESERVED3] = {0}; // 预留字符数组参数3接口
  147. char szReversed4[MAX_RESERVED4] = {0}; // 预留字符数组参数4接口
  148. char szReversed5[MAX_RESERVED5] = {0}; // 预留字符数组参数5接口
  149. if( pDllMostDeviceSet(g_strDirectory, szIniName, nPort, nDevAddr,
  150. szRs232cmd, szMsg, nReversed1, nReversed2, nReversed3, nReversed4, nReversed5,
  151. fReversed1, fReversed2, fReversed3,
  152. szReversed1, szReversed2, szReversed3, szReversed4, szReversed5)==0)
  153. {
  154. }
  155. else
  156. {
  157. return -1;
  158. }
  159. return 0;
  160. //return -1;
  161. }
  162. void UnInitMostDeviceDll(
  163. char szPath[MAX_PATH], // 程序所在路径
  164. char szIniName[MAX_PATH], // 配置文件名称
  165. int nCommPort // 串行端口
  166. )
  167. {
  168. if (g_hMostDeviceLibModule != NULL && ( NULL != pDllMostDeviceUnInit ))
  169. {
  170. pDllMostDeviceUnInit(szPath, szIniName, nCommPort);
  171. }
  172. DeleteCriticalSection( &g_csMostDeviceReadOneData );
  173. if (g_hMostDeviceLibModule!= NULL)
  174. {
  175. AfxFreeLibrary(g_hMostDeviceLibModule);
  176. g_hMostDeviceLibModule = NULL;
  177. }
  178. }