icpdasprocess.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. #include "stdafx.h"
  2. #include "rtuprocess.h"
  3. #include "Global.h"
  4. #include "struct.h"
  5. #include "NoticeQueue.h"
  6. DWORD g_dwIcpdasVariantTick = 0;
  7. HANDLE g_ICPDASRequestThread = NULL;
  8. HANDLE g_ICPDASResponseThread = NULL;
  9. DWORD g_dwICPDASVariantTick = 0;
  10. CRITICAL_SECTION g_csIcpdasReadOneData;
  11. //icpdas动态库输出函数
  12. HINSTANCE g_hICPDASLibModule = NULL;
  13. ICPDAS_DLLInitCom pICPDAS_DLLInitCom = NULL;
  14. ICPDAS_DLLInit pICPDAS_DLLInit = NULL;
  15. ICPDAS_DLLUnInit pICPDAS_DLLUnInit = NULL;
  16. ICPDAS_DLLRequestStatusData pICPDAS_DLLRequestStatusData = NULL;
  17. ICPDAS_DLLWrCom pICPDAS_DLLWrCom = NULL;
  18. int IcpdasResponseValue();
  19. int IcpdasCommandSend();
  20. void UnInitIcpdasDll();
  21. int IcpdasSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80]);
  22. int GetIcpdasFromIni(char chCmd[32], BYTE *Start, BYTE* byWithAddrFlag, BYTE *pFuncCode);
  23. BOOL LoadIcpdasDll(CString strpath)
  24. {
  25. char strFile[256] = {0};
  26. g_hICPDASLibModule = NULL;
  27. sprintf(strFile, "%s\\dll\\icpdas.dll", strpath);
  28. g_hICPDASLibModule = AfxLoadLibrary(strFile);
  29. InitializeCriticalSection( &g_csIcpdasReadOneData );
  30. //RTU 动态库初始化
  31. if (NULL != g_hICPDASLibModule)
  32. {
  33. pICPDAS_DLLInitCom =( ICPDAS_DLLInitCom)::GetProcAddress(g_hICPDASLibModule, "ICPDAS_DLLInitCom");
  34. pICPDAS_DLLInit = (ICPDAS_DLLInit)::GetProcAddress(g_hICPDASLibModule, "ICPDAS_DLLInit");
  35. pICPDAS_DLLUnInit = (ICPDAS_DLLUnInit)::GetProcAddress(g_hICPDASLibModule, "ICPDAS_DLLUnInit");
  36. pICPDAS_DLLRequestStatusData = (ICPDAS_DLLRequestStatusData)::GetProcAddress(g_hICPDASLibModule, "ICPDAS_DLLRequestStatusData");
  37. pICPDAS_DLLWrCom = (ICPDAS_DLLWrCom)::GetProcAddress(g_hICPDASLibModule, "ICPDAS_DLLWrCom");
  38. return TRUE;
  39. }
  40. else
  41. {
  42. return FALSE;
  43. }
  44. }
  45. BOOL InitIcpdasComm(int iAddr, int iPort, int iBaudrate, int iDataBit, int iStopBit, int iParity, int iIntervals)
  46. {
  47. if (pICPDAS_DLLInitCom(iAddr,iPort, iBaudrate, iDataBit, iStopBit, iParity, iIntervals))//初始化串口
  48. {
  49. return TRUE;
  50. }
  51. else
  52. {
  53. return FALSE;
  54. }
  55. }
  56. int IcpdasRequestData(
  57. int nIndex,
  58. SETBASEPARAM SetBasePara,
  59. int nDataLen,
  60. int nPort,
  61. int nDevAddr,
  62. char chDevUid[20],
  63. int iVarID,
  64. char chRs232cmd[32])
  65. {
  66. char chMsg[80] = {0};
  67. BYTE Start= 0x0;//起始位
  68. BYTE StartAddr[MAX_ID] = {0};
  69. BYTE FuncCode[MAX_ID] = {0};
  70. BYTE byWithAddrFlag;
  71. int nDeviceIndex = -1, nVarIndex = -1;
  72. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  73. if( bFind == FALSE ) return -1;
  74. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  75. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  76. char szMsg[80] = {0};
  77. sprintf((char*)StartAddr, "%02d", SetBasePara.nStartAddr);
  78. GetIcpdasFromIni(chRs232cmd, &Start, &byWithAddrFlag,FuncCode);
  79. CString strIndex;
  80. if( 0 == stricmp((char *)FuncCode, "#AAN") )
  81. {
  82. strIndex.Format("%d", atoi(pBaseVar->m_strRs232Index));
  83. strcpy((char *)FuncCode, (char *)(LPCTSTR)strIndex);
  84. //LOG4C((LOG_NOTICE, "strIndex=%s, FuncCode=%s", strIndex, FuncCode));
  85. }
  86. if( nIndex == 0 )
  87. {
  88. if( pICPDAS_DLLInit(nPort, nDevAddr, Start, StartAddr,
  89. SetBasePara.nRegNum, FuncCode, nDataLen, byWithAddrFlag, szMsg)!=0)
  90. {
  91. return -1;
  92. }
  93. }
  94. else if( strlen(szMsg) == 0 )
  95. {
  96. if( pICPDAS_DLLInit(nPort, nDevAddr, Start, StartAddr,
  97. SetBasePara.nRegNum, FuncCode, nDataLen, byWithAddrFlag, szMsg)!=0)
  98. {
  99. return -1;
  100. }
  101. }
  102. return IcpdasSingleResponseData(chDevUid, iVarID, szMsg);
  103. }
  104. int IcpdasSingleResponseData(char chDevUid[20], int iVarID, char chMsg[80])
  105. {
  106. int nDeviceIndex = -1, nVarIndex = -1;
  107. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  108. if( bFind == FALSE ) return -1;
  109. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  110. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  111. EnterCriticalSection(&g_csIcpdasReadOneData);
  112. pDev->m_dwOnlineTick = GetTickCount();
  113. LeaveCriticalSection(&g_csIcpdasReadOneData);
  114. DWORD nValue = 0;
  115. WORD wdValue = 0;
  116. double fCoef = pBaseVar->m_dbCoefficient;
  117. int nDataLen = pBaseVar->m_iDataLen/2;
  118. int nVarItemID = pBaseVar->m_nVarItemID;
  119. if( nVarItemID > 0 )//BIT位变量
  120. {
  121. int nStartBit = pBaseVar->m_nStartBit;
  122. int nEndBit = pBaseVar->m_nEndBit;
  123. int nStartReg, nEndReg;
  124. if( nEndBit - nStartBit < 7 ) // 一般告警状态会这样子定义,Modbus Ascii码暂时没有碰到,没有经过调试,待测试
  125. {
  126. if( nDataLen == 1 )
  127. {
  128. char chBuffer[8] = {0};
  129. memset(chBuffer, 0, sizeof(chBuffer));
  130. BYTE byValue = (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[0 + 1]) & 0x00FF );
  131. DigitToBinary(byValue, chBuffer, 8) ;
  132. CString strTemp;
  133. switch( nEndBit - nStartBit )
  134. {
  135. case 0:
  136. strTemp.Format("%c", chBuffer[nStartBit]);
  137. break;
  138. case 1:
  139. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  140. break;
  141. case 2:
  142. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  143. break;
  144. case 3:
  145. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  146. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  147. break;
  148. case 4:
  149. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  150. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  151. break;
  152. case 5:
  153. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  154. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  155. break;
  156. case 6:
  157. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  158. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  159. break;
  160. }
  161. nValue = atoi(strTemp);
  162. //strTemp.Format("%d",
  163. }
  164. else if( nDataLen == 2 )
  165. {
  166. char chBuffer[16] = {0};
  167. //chMsg[0] = 0x00;
  168. //chMsg[2] = 0x00;
  169. //chMsg[2] = 0x0E;
  170. //chMsg[3] = 0x0F;
  171. WORD wValue = ( ( (AsciiToBYTE(chMsg[0]) << 4) | ( AsciiToBYTE(chMsg[1]) & 0x00FF ) ) << 8 ) |
  172. ( ( (AsciiToBYTE(chMsg[2]) << 4) | ( AsciiToBYTE(chMsg[3]) & 0x00FF ) ) & 0x0000FFFF );
  173. DigitToBinary(wValue, chBuffer, 16) ;
  174. CString strTemp;
  175. switch( nEndBit - nStartBit )
  176. {
  177. case 0:
  178. strTemp.Format("%c", chBuffer[nStartBit]);
  179. break;
  180. case 1:
  181. strTemp.Format("%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1]);
  182. break;
  183. case 2:
  184. strTemp.Format("%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2]);
  185. break;
  186. case 3:
  187. strTemp.Format("%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  188. chBuffer[nStartBit + 2], chBuffer[nStartBit +3]);
  189. break;
  190. case 4:
  191. strTemp.Format("%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1],
  192. chBuffer[nStartBit + 2], chBuffer[nStartBit + 3], chBuffer[nStartBit + 4]);
  193. break;
  194. case 5:
  195. strTemp.Format("%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  196. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5]);
  197. break;
  198. case 6:
  199. strTemp.Format("%c%c%c%c%c%c%c", chBuffer[nStartBit], chBuffer[nStartBit + 1], chBuffer[nStartBit + 2],
  200. chBuffer[nStartBit + 3], chBuffer[nStartBit + 4], chBuffer[nStartBit + 5], chBuffer[nStartBit + 6]);
  201. break;
  202. }
  203. nValue = atoi(strTemp);
  204. }
  205. }
  206. // 特殊变量处理(例如,一个命令返回两个变量的情况)
  207. else if( nEndBit - nStartBit == 7 ) // 1个字节的情况
  208. {
  209. nStartReg = (int)nStartBit / 8;
  210. nEndReg = (int)(nEndBit + 1) / 8;
  211. nValue = (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF );
  212. }
  213. else if( nEndBit - nStartBit == 15 ) // 2个字节的情况
  214. {
  215. nStartReg = (int)nStartBit / 8;
  216. nEndReg = (int)(nEndBit + 1) / 8;
  217. nValue = ( ( (AsciiToBYTE(chMsg[nStartReg]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 1]) & 0x00FF ) ) << 8 ) |
  218. ( ( (AsciiToBYTE(chMsg[nStartReg + 2]) << 4) | ( AsciiToBYTE(chMsg[nStartReg + 3]) & 0x00FF ) ) & 0x0000FFFF );
  219. }
  220. EnterCriticalSection( &g_csIcpdasReadOneData );
  221. pBaseVar->m_dbData = (double)(nValue * fCoef);
  222. if( pBaseVar->m_nRearm != 0 )
  223. {
  224. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  225. }
  226. LeaveCriticalSection( &g_csIcpdasReadOneData );
  227. //LOG4C((LOG_NOTICE, "变量:%s, 值=%d", pBaseVar->m_strDesc, (int)pBaseVar->m_dbData));
  228. }
  229. else
  230. {
  231. // 联合类型变量做特殊处理
  232. if( pBaseVar->m_nVarTypeID >= UNION_TYPE_MIN_ID && pBaseVar->m_nVarTypeID <= UNION_TYPE_MAX_ID )
  233. {
  234. if( nDataLen == 2 )
  235. {
  236. //int类型
  237. union __UNION_VAR_INT{
  238. char ch[2];
  239. int value;
  240. }unionVarInt;
  241. unionVarInt.ch[0] = chMsg[1];
  242. unionVarInt.ch[1] = chMsg[0];
  243. EnterCriticalSection( &g_csIcpdasReadOneData );
  244. pBaseVar->m_dbData = (double)(unionVarInt.value * fCoef);
  245. if( pBaseVar->m_nRearm != 0 )
  246. {
  247. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  248. }
  249. LeaveCriticalSection( &g_csIcpdasReadOneData );
  250. //TRACE2("联合类型变量ID%d, 值=%d\r\n", iVarID, unionVarInt.value);
  251. }
  252. else if( nDataLen == 4 )
  253. {
  254. //float类型
  255. union __UNION_VAR_FLOAT{
  256. char ch[4];
  257. float value;
  258. }unionVarFloat;
  259. unionVarFloat.ch[0] = chMsg[3];
  260. unionVarFloat.ch[1] = chMsg[2];
  261. unionVarFloat.ch[2] = chMsg[1];
  262. unionVarFloat.ch[3] = chMsg[0];
  263. EnterCriticalSection( &g_csIcpdasReadOneData );
  264. pBaseVar->m_dbData = (double)(unionVarFloat.value * fCoef);
  265. if( pBaseVar->m_nRearm != 0 )
  266. {
  267. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  268. }
  269. LeaveCriticalSection( &g_csIcpdasReadOneData );
  270. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarFloat.value);
  271. }
  272. else if( nDataLen == 8 )
  273. {
  274. //double类型
  275. union __UNION_VAR_DOUBLE{
  276. char ch[8];
  277. double value;
  278. }unionVarDouble;
  279. unionVarDouble.ch[0] = chMsg[7];
  280. unionVarDouble.ch[1] = chMsg[6];
  281. unionVarDouble.ch[2] = chMsg[5];
  282. unionVarDouble.ch[3] = chMsg[4];
  283. unionVarDouble.ch[4] = chMsg[3];
  284. unionVarDouble.ch[5] = chMsg[2];
  285. unionVarDouble.ch[6] = chMsg[1];
  286. unionVarDouble.ch[7] = chMsg[0];
  287. EnterCriticalSection( &g_csIcpdasReadOneData );
  288. pBaseVar->m_dbData = (double)(unionVarDouble.value * fCoef);
  289. if( pBaseVar->m_nRearm != 0 )
  290. {
  291. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  292. }
  293. LeaveCriticalSection( &g_csIcpdasReadOneData );
  294. //TRACE2("联合类型变量ID%d, 值=%.1f\r\n", iVarID, unionVarDouble.value);
  295. }
  296. }
  297. else
  298. {
  299. CString str;
  300. WORD wdValue;
  301. switch( nDataLen )
  302. {
  303. case 1:
  304. nValue = atoi(chMsg);
  305. break;
  306. case 2:
  307. memcpy(&wdValue, chMsg, sizeof(WORD));
  308. nValue = ntohs(wdValue);
  309. break;
  310. case 3: // 一般为字符串类型,不作处理
  311. break;
  312. case 4: // 保留,暂时不知道什么处理
  313. break;
  314. case 5: // 一般为字符串类型,不作处理
  315. break;
  316. case 6: // 一般为字符串类型,不作处理
  317. break;
  318. case 7: // 7017
  319. //str.Format("%s", chMsg);
  320. break;
  321. case 8: // 保留,暂时没有用到
  322. break;
  323. }
  324. //LOG4C((LOG_NOTICE, "pBaseVar->m_iDataLen = %d", pBaseVar->m_iDataLen));
  325. if( pBaseVar->m_iDataLen == 7 )
  326. {
  327. str.Format("%s", chMsg);
  328. EnterCriticalSection( &g_csIcpdasReadOneData );
  329. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  330. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  331. {
  332. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  333. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  334. pBaseVar->m_dbData = atof(str) *
  335. fCoef *
  336. fScale;
  337. }
  338. else
  339. {
  340. pBaseVar->m_dbData = (double)(atof(str) * fCoef);
  341. }
  342. if( pBaseVar->m_nRearm != 0 )
  343. {
  344. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  345. }
  346. pDev->m_dwOnlineTick = GetTickCount();
  347. LeaveCriticalSection( &g_csIcpdasReadOneData );
  348. //LOG4C((LOG_NOTICE, "desc = %s, data = %d", pBaseVar->m_strDesc, (int)pBaseVar->m_dbData));
  349. return 0;
  350. }
  351. EnterCriticalSection( &g_csIcpdasReadOneData );
  352. if( pBaseVar->m_nMaxValues != 0 || pBaseVar->m_nMinValues != 0 ||
  353. pBaseVar->m_nMaxConvtRate != 0 || pBaseVar->m_nMinConvtRate != 0 )
  354. {
  355. double fScale = (double)(pBaseVar->m_nMaxConvtRate - pBaseVar->m_nMinConvtRate) /
  356. (double)(pBaseVar->m_nMaxValues - pBaseVar->m_nMinValues);
  357. pBaseVar->m_dbData = nValue *
  358. fCoef *
  359. fScale;
  360. }
  361. else
  362. {
  363. pBaseVar->m_dbData = (double)(nValue * fCoef);
  364. }
  365. if( pBaseVar->m_nRearm != 0 )
  366. {
  367. pBaseVar->m_dbData = pBaseVar->m_dbData + pBaseVar->m_nRearm;
  368. }
  369. LeaveCriticalSection( &g_csIcpdasReadOneData );
  370. //g_IcpdasReadOneData.dbData = (double)(nValue * fCoef);
  371. //TRACE2("普通变量ID%d, 值=%d\r\n", iVarID, g_IcpdasReadOneData.dbData);
  372. }
  373. }
  374. EnterCriticalSection(&g_csIcpdasReadOneData);
  375. pDev->m_dwOnlineTick = GetTickCount();
  376. LeaveCriticalSection(&g_csIcpdasReadOneData);
  377. return 0;
  378. }
  379. void UnInitIcpdasDll()
  380. {
  381. if (g_hICPDASLibModule != NULL && ( NULL != pICPDAS_DLLUnInit ))
  382. {
  383. pICPDAS_DLLUnInit();
  384. }
  385. DeleteCriticalSection( &g_csIcpdasReadOneData );
  386. if (g_hICPDASLibModule!= NULL)
  387. {
  388. AfxFreeLibrary(g_hICPDASLibModule);
  389. g_hICPDASLibModule = NULL;
  390. }
  391. }
  392. int GetIcpdasFromIni(char chCmd[32], BYTE *Start, BYTE* byWithAddrFlag, BYTE *pFuncCode)
  393. {
  394. CHAR strFile[MAX_FILE_LENGTH + 1] = "";
  395. CHAR strValue[MAX_VALUE_LENGTH + 1] = "";
  396. CHAR strValueAddrFlag[4] ="";
  397. wsprintf(strFile, "%s\\config\\icpdas.ini", g_strDirectory);
  398. //Database
  399. GetPrivateProfileString("CMD", chCmd, "", strValue, sizeof(strValue), strFile);
  400. GetPrivateProfileString("RESWITHADDR", chCmd, "", strValueAddrFlag, sizeof(strValueAddrFlag), strFile);
  401. Start[0] = strValue[0];
  402. byWithAddrFlag[0] = strValueAddrFlag[0];
  403. if (strcmp( strValue, "@AA") == 0)
  404. sprintf((char*)pFuncCode, "%02d", -1);
  405. else if (strcmp( strValue, "$AALS") == 0)
  406. memcpy(pFuncCode, strValue + 3, 2);
  407. else if (strcmp( strValue, "@AA(Data)") == 0)
  408. sprintf((char*)pFuncCode, "%02d", -1);
  409. else if( strcmp( strValue, "#AA1cDD") == 0 )
  410. memcpy(pFuncCode, strValue + 3, 4);
  411. else if( strcmp( strValue, "#AAN" ) == 0 )
  412. memcpy(pFuncCode, "#AAN", strlen("#AAN"));
  413. else
  414. sprintf((char*)pFuncCode, "%02C", strValue[3]);
  415. return 0;
  416. }
  417. int RequestWrIcpdasData(char chDevUid[20], int iVarID, double data)//用于请求写命令操作
  418. {
  419. int nDeviceIndex = -1, nVarIndex = -1;
  420. BOOL bFind = FindVar(chDevUid, iVarID, nDeviceIndex, nVarIndex);
  421. if( bFind == FALSE ) return -1;
  422. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  423. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  424. char chMsg[80] = {0};
  425. BYTE Start= 0x0;//起始位
  426. BYTE StartAddr[MAX_ID] = {0};
  427. BYTE FuncCode[MAX_ID] = {0};
  428. BYTE byWithAddrFlag = 0;
  429. int nLen = 0;
  430. sprintf((char*)StartAddr, "%02d", pDev->m_iDevideaddr);
  431. GetIcpdasFromIni((char *)(LPCTSTR)pBaseVar->m_strRs232cmd, &Start, &byWithAddrFlag,FuncCode);
  432. memcpy(chMsg, &Start, sizeof(Start));
  433. nLen += sizeof(Start);
  434. memcpy(chMsg + nLen, StartAddr, strlen((char *)StartAddr));
  435. nLen += strlen((char *)StartAddr);
  436. // 固定信息
  437. memcpy(chMsg + nLen, "1", 1);
  438. nLen += 1;
  439. double fCoef = pBaseVar->m_dbCoefficient;
  440. int nDataLen = pBaseVar->m_iDataLen;
  441. int nVarItemID = pBaseVar->m_nVarItemID;
  442. // 泓格I-7000系列为特殊协议只有BIT变量,这里只处理BIT位变量
  443. if( nVarItemID > 0 )//BIT位变量
  444. {
  445. int nStartBit = pBaseVar->m_nStartBit;
  446. int nEndBit = pBaseVar->m_nEndBit;
  447. if( nEndBit - nStartBit < 7 )
  448. {
  449. CString strTemp;
  450. CString strIndex;
  451. if( nDataLen == 1 )
  452. {
  453. switch( nEndBit - nStartBit )
  454. {
  455. case 0:
  456. strTemp.Format("%02d", (int)data);
  457. break;
  458. }
  459. strIndex.Format("%d", atoi(pBaseVar->m_strRs232Index));
  460. memcpy(chMsg + nLen, (char *)(LPCTSTR)strIndex, strIndex.GetLength());
  461. nLen += strIndex.GetLength();
  462. memcpy(chMsg + nLen, (char *)(LPCTSTR)strTemp, strTemp.GetLength());
  463. nLen += strTemp.GetLength();
  464. }
  465. }
  466. }
  467. chMsg[nLen] = 0x0D;
  468. nLen++;
  469. if( pICPDAS_DLLWrCom(pDev->m_iPort, pDev->m_iDevideaddr, chMsg, nLen) == 0)
  470. {
  471. LOG4C((LOG_NOTICE, "pICPDAS_DLLWrCom success!"));
  472. return 0;
  473. }
  474. else
  475. {
  476. LOG4C((LOG_NOTICE, "pICPDAS_DLLWrCom lost!"));
  477. return -1;
  478. }
  479. }