SanTak_ShiWei.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: UpsSanTak.cpp //////
  4. ////// 作 者: Suguobing //////
  5. ////// 创建时间: //////
  6. ////// 说 明: SanTak协议 //////
  7. ////// //////
  8. ////// 修改时间:2010-08-23 //////
  9. ////// 修改说明: //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "CommProcess.h"
  14. #include "SanTak_ShiWei.h"
  15. CSanTakShiWei::CSanTakShiWei( char szPath[MAX_PATH],
  16. char szIniName[MAX_PATH],
  17. int nCommPort, int nAddr,
  18. int nRate, int nDataBit,
  19. int nStopBit, int nParity,
  20. int nInterval )
  21. {
  22. #if IS_USE_READMSG_CS
  23. InitializeCriticalSection(&m_csReadMsg);
  24. #endif
  25. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  26. memset(m_szSanTak_SW_Q2Msg, 0, sizeof(m_szSanTak_SW_Q2Msg));
  27. memset(m_szSanTak_SW_WAMsg, 0, sizeof(m_szSanTak_SW_WAMsg));
  28. m_dwOnlineTick = 0;
  29. m_devOnline = TRUE;
  30. }
  31. CSanTakShiWei::~CSanTakShiWei()
  32. {
  33. #if IS_USE_READMSG_CS
  34. DeleteCriticalSection(& m_csReadMsg);
  35. #endif
  36. MTVERIFY(CloseHandle(m_hSemComm));
  37. CloseComm();
  38. }
  39. BOOL CSanTakShiWei::UpsSanTakSwOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  40. {
  41. BOOL bResult = FALSE;
  42. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  43. return bResult;
  44. }
  45. int CSanTakShiWei::SendReadRequest(
  46. char szPath[MAX_PATH],
  47. char szIniName[MAX_PATH],
  48. int nCommPort,
  49. int nAddr,
  50. char szCmd[MAX_CMD],
  51. char szMsg[VAR_MSG],
  52. int nReversed1,
  53. int nReversed2,
  54. int nReversed3,
  55. int nReversed4,
  56. int nReversed5,
  57. float fReversed1,
  58. float fReversed2,
  59. float fReversed3,
  60. char szReversed1[MAX_RESERVED1],
  61. char szReversed2[MAX_RESERVED2],
  62. char szReversed3[MAX_RESERVED3],
  63. char szReversed4[MAX_RESERVED4],
  64. char szReversed5[MAX_RESERVED5]
  65. )
  66. {
  67. int nIndex = 0, nLen = 0;
  68. char szSendMsg[UPS_SANTAKSHIWEI_SEND_MSG] = {0};
  69. char szType[UPS_TYPE_LENGTH] = {0};
  70. int startBit = 0;
  71. int endBit = 0;
  72. m_address = nAddr;
  73. GetSanTakSwFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
  74. CCommProcess *pComm = FindComm(nCommPort);
  75. if( pComm == NULL ) return -1;
  76. int nRet = -1;
  77. if( ( strlen(m_szSanTak_SW_Q2Msg) == 0 &&
  78. (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  79. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  80. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  81. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  82. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  83. strcmp(szCmd, "cmd-11") == 0 ||strcmp(szCmd, "cmd-12") == 0 ||
  84. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  85. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  86. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  87. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  88. strcmp(szCmd, "cmd-31") ==0 ) ||
  89. (strlen(m_szSanTak_SW_WAMsg) == 0 &&
  90. (strcmp(szCmd, "cmd-21")==0 || strcmp(szCmd, "cmd-22") == 0 ||
  91. strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  92. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  93. strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 ||
  94. strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0)) ||
  95. strcmp(szCmd, "cmd-1") == 0 ||
  96. strcmp(szCmd, "cmd-11") == 0 ||
  97. strcmp(szCmd, "cmd-21") == 0 ||
  98. strcmp(szCmd, "cmd-30") == 0))//UPS工作状态及传送顺序( strlen(m_szSanTak_3C_Q4Msg) == 0 &&
  99. {
  100. nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
  101. if( nRet != 0 )
  102. return nRet;
  103. }
  104. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  105. {
  106. m_devOnline = FALSE;
  107. }
  108. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  109. {
  110. m_devOnline = TRUE;
  111. }
  112. if( m_devOnline == FALSE )
  113. {
  114. return -1;
  115. }
  116. nRet = GetSanTak_Sw_Q2Msg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  117. nRet = GetSanTak_Sw_WAMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  118. //LOG4C_HEX_DUMP((LOG_NOTICE, szMsg, sizeof(szMsg)));
  119. //LOG4C((LOG_NOTICE, "%s=%d, sizeof = %d, return %d", szCmd, atoi(szMsg), sizeof(buf), nRet));
  120. return nRet;
  121. }
  122. int CSanTakShiWei::GetSanTak_Sw_Q2Msg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit)
  123. {
  124. int nRet = 0;
  125. if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  126. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  127. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  128. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  129. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  130. strcmp(szCmd, "cmd-11") == 0 ||strcmp(szCmd, "cmd-12") == 0 ||
  131. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  132. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  133. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  134. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  135. strcmp(szCmd, "cmd-31") == 0)
  136. {
  137. #if IS_USE_READMSG_CS
  138. EnterCriticalSection(&m_csReadMsg);
  139. #endif
  140. DataConversion(szType, m_szSanTak_SW_Q2Msg + nIndex, szMsg, nLen, startBit, endBit);
  141. //LOG4C((LOG_NOTICE, "GetSanTak_3C_Q1Msg"));
  142. //LOG4C_HEX_DUMP((LOG_INFO, m_szSanTak_SW_Q2Msg, sizeof(m_szSanTak_SW_Q2Msg)));
  143. //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 6 ));
  144. #if IS_USE_READMSG_CS
  145. LeaveCriticalSection(&m_csReadMsg);
  146. #endif
  147. nRet = 0;
  148. }
  149. return nRet;
  150. }
  151. int CSanTakShiWei::GetSanTak_Sw_WAMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  152. {
  153. int nRet = 0;
  154. if (strcmp(szCmd, "cmd-21")==0 || strcmp(szCmd, "cmd-22") == 0 ||
  155. strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  156. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  157. strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 ||
  158. strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0)
  159. {
  160. #if IS_USE_READMSG_CS
  161. EnterCriticalSection(&m_csReadMsg);
  162. #endif
  163. DataConversion(szType, m_szSanTak_SW_WAMsg + nIndex, szMsg, nLen, startBit, endBit);
  164. //LOG4C((LOG_NOTICE, "GetSanTak_3C_Q4Msg"));
  165. //LOG4C_HEX_DUMP((LOG_INFO, m_szSanTak_3C_Q4Msg,sizeof(m_szSanTak_3C_Q4Msg)));
  166. //LOG4C_HEX_DUMP((LOG_INFO, szMsg, 5 ));
  167. #if IS_USE_READMSG_CS
  168. LeaveCriticalSection(&m_csReadMsg);
  169. #endif
  170. nRet = 0;
  171. }
  172. return nRet;
  173. }
  174. int CSanTakShiWei::GetDeviceParam(int nAddr,
  175. CCommProcess *pComm, //串口对象指针
  176. char szSendMsg[UPS_SANTAKSHIWEI_SEND_MSG], //发送Buffer
  177. char szCmd[MAX_CMD], // 命令
  178. char szMsg[VAR_MSG], // 接收Buffer
  179. int &nIndex, // 变量索引,针对接收Buffer而言
  180. int &nLen, // 变量长度
  181. char szType[TYPE_LENGTH]) // 变量数据类型
  182. {
  183. int nRet = -1;
  184. nRet = RequestStatus(nAddr, pComm, szSendMsg);
  185. if( nRet != 0 )
  186. {
  187. return nRet; // 串口忙
  188. }
  189. nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
  190. return nRet;
  191. }
  192. int CSanTakShiWei::GetSanTakSwFromIni( char szPath[MAX_PATH], //服务器程序所在目录
  193. char szIniName[MAX_PATH], //配置文件名
  194. char szCmd[MAX_CMD], //命令
  195. char szSanTakSendMsg[MAX_CMD], //发送Buffer
  196. char nType[MAX_CMD],
  197. int &nIndex,
  198. int &nLen,
  199. int &startBit,
  200. int &endBit)
  201. {
  202. CHAR szFile[MAX_PATH + 1] = "";
  203. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  204. TRACE("szFile =%s\n",szFile);
  205. GetPrivateProfileString(szCmd, "SendCmd", "", szSanTakSendMsg, 10, szFile);
  206. szSanTakSendMsg[strlen(szSanTakSendMsg)] = 0x0D;
  207. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  208. nType[strlen(nType)] = '\0';
  209. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  210. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  211. return 0;
  212. }
  213. int CSanTakShiWei::RequestStatus(int nAddr, CCommProcess *pComm, char chSendMsg[UPS_SANTAKSHIWEI_SEND_MSG] )
  214. {
  215. #if DEBUG_SANTAKSHIWEI //开关量,用于测试模拟数据
  216. REQUEST_STRUCT structResuest;
  217. int nLen = sizeof(REQUEST_STRUCT);
  218. char chkSumBuf[5] ={0};
  219. char addBuf[UPS_TYPE_LENGTH] = {0};
  220. memset(&structResuest,0,nLen);
  221. //
  222. memcpy(structResuest.chkSum, chSendMsg, 2);
  223. structResuest.midBuf = 0x2C;
  224. structResuest.addr[0] = ByteToAscii((nAddr >> 4) & 0x0f);;
  225. structResuest.addr[1] = ByteToAscii(nAddr & 0x0f);
  226. structResuest.End = 0x0D;
  227. //
  228. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  229. {
  230. int nDatalen = (int)sizeof(structResuest);
  231. ResetEvent(m_hSemComm);
  232. int nResult = pComm->Write((BYTE *)&structResuest, nDatalen);
  233. //LOG4C_HEX_DUMP((LOG_NOTICE, chSendMsg, 3));
  234. //LOG4C((LOG_NOTICE, "CSanTak::RequestStatus chSendMsg = %s, nResult = %d, nDatalen = %d", chSendMsg,nResult, nDatalen));
  235. if (nResult == nDatalen)
  236. {
  237. }
  238. else
  239. {
  240. SetEvent(m_hSemComm);
  241. return EER_CODE_SANTAKSHIWEI_COM_REGNUM;
  242. }
  243. }
  244. else
  245. {
  246. return ERR_CODE_SANTAKSHIWEI_COM_BUSY; //串口忙
  247. }
  248. #endif
  249. return 0;
  250. }
  251. int CSanTakShiWei::ResponseStatus(int nAddr,
  252. CCommProcess *pComm,
  253. char szCmd[MAX_CMD],
  254. char szMsg[VAR_MSG],
  255. int &nIndex,
  256. int &nLen,
  257. char szType[TYPE_LENGTH]
  258. )
  259. {
  260. #if DEBUG_SANTAKSHIWEI
  261. int nReceiveLen = 0;
  262. int nProcessLen = 0;
  263. int nReadLen = 0;
  264. RESPONSE_STRUCT strcutResponse;
  265. memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
  266. nReceiveLen = sizeof(RESPONSE_STRUCT);
  267. char *pBuff = new char[nReceiveLen];
  268. memset(pBuff, 0 , nReceiveLen);
  269. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  270. //LOG4C((LOG_NOTICE, "CSanTak::ResponseStatus pBuff = %s, nReadLen = %d, nReceiveLen = %d", pBuff, nReadLen, nReceiveLen));
  271. if (nReadLen <= 0)
  272. {
  273. SetEvent(m_hSemComm);
  274. if (pBuff != NULL)
  275. {
  276. delete [] pBuff;
  277. pBuff = NULL;
  278. }
  279. return ERR_CODE_SANTAKSHIWEI_COM_READ_NO_DATA;
  280. }
  281. //长度校验出错
  282. SetSanTakSwQ2Msg(szCmd, pBuff);
  283. SetSanTakSwWAMsg(szCmd, pBuff);
  284. m_dwOnlineTick = GetTickCount();
  285. // 设置串口等待事件为有信号
  286. SetEvent( m_hSemComm );
  287. if( pBuff != NULL)
  288. {
  289. delete[] pBuff;
  290. pBuff = NULL;
  291. }
  292. #else
  293. SimulationCommData();
  294. #endif
  295. //LOG4C((LOG_NOTICE, "SimulationCommData"));
  296. return 0;
  297. }
  298. void CSanTakShiWei::SetSanTakSwQ2Msg(char szCmd[MAX_CMD], char *pBuff)
  299. {
  300. if (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  301. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  302. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  303. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  304. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  305. strcmp(szCmd, "cmd-11") == 0 ||strcmp(szCmd, "cmd-12") == 0 ||
  306. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  307. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  308. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  309. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  310. strcmp(szCmd, "cmd-31") == 0)
  311. {
  312. #if IS_USE_READMSG_CS
  313. EnterCriticalSection(&m_csReadMsg);
  314. #endif
  315. memcpy(m_szSanTak_SW_Q2Msg, pBuff, sizeof(m_szSanTak_SW_Q2Msg));
  316. #if IS_USE_READMSG_CS
  317. LeaveCriticalSection(&m_csReadMsg);
  318. #endif
  319. }
  320. }
  321. void CSanTakShiWei::SetSanTakSwWAMsg(char szCmd[MAX_CMD], char *pBuff)
  322. {
  323. if ( strcmp(szCmd, "cmd-21")==0 || strcmp(szCmd, "cmd-22") == 0 ||
  324. strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  325. strcmp(szCmd, "cmd-26") == 0 || strcmp(szCmd, "cmd-25") == 0 ||
  326. strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 ||
  327. strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 )
  328. {
  329. #if IS_USE_READMSG_CS
  330. EnterCriticalSection(&m_csReadMsg);
  331. #endif
  332. memcpy(m_szSanTak_SW_WAMsg, pBuff, sizeof(m_szSanTak_SW_WAMsg));
  333. #if IS_USE_READMSG_CS
  334. LeaveCriticalSection(&m_csReadMsg);
  335. #endif
  336. }
  337. }
  338. void CSanTakShiWei::SimulationCommData(void)
  339. {
  340. memcpy(m_szSanTak_SW_Q2Msg, "(00 000.0 000.0 000.0 000.0 219.0 219.0 220.0 002 009 021 00.0 407.0 28.3 10000010 000.00 091 0000 00 00 00 06030000 01..", sizeof("(00 000.0 000.0 000.0 000.0 219.0 219.0 220.0 002 009 021 00.0 407.0 28.3 10000000 000.00 091 0000 00 00 00 06030000 01.."));
  341. memcpy(m_szSanTak_SW_WAMsg, "(01 000.2 000.0 000.0 000.2 000.0 000.0 000.2 000.2 000.1 000.1 000.2 005 00000000.", sizeof("(01 000.2 000.0 000.0 000.2 000.0 000.0 000.2 000.2 000.0 000.0 000.0 005 00000000."));
  342. }
  343. int CSanTakShiWei::WriteRequestStatus(char szCmd[MAX_CMD], CCommProcess *pComm, char chSendMsg[UPS_SANTAKSHIWEI_SEND_MSG])
  344. {
  345. return 0;
  346. }
  347. int CSanTakShiWei::WriteResponseStatus( CCommProcess *pComm, //串口对象指针
  348. int nAddr, // 设备地址
  349. char szCmd[MAX_CMD], // 命令
  350. char szMsg[VAR_MSG], // 接收Buffer
  351. int &nIndex, // 变量索引,针对接收Buffer而言
  352. int &nLen, // 变量长度
  353. char szType[TYPE_LENGTH] // 变量数据类型
  354. )
  355. {
  356. return 0;
  357. }
  358. int CSanTakShiWei::SendSetReuest(
  359. char szPath[MAX_PATH], // 程序所在路径
  360. char szIniName[MAX_PATH], // 配置文件名称
  361. int nCommPort, // 串行端口
  362. int nAddr, // 设备地址
  363. char szCmd[MAX_CMD], // 请求命令
  364. char szMsg[VAR_MSG], // 响应的值
  365. int nReversed1, // 预留整形参数1接口
  366. int nReversed2, // 预留整形参数2接口
  367. int nReversed3, // 预留整形参数3接口
  368. int nReversed4, // 预留整形参数4接口
  369. int nReversed5, // 预留整形参数5接口
  370. float fReversed1, // 预留float参数1接口
  371. float fReversed2, // 预留float参数2接口
  372. float fReversed3, // 预留float参数3接口
  373. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  374. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  375. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  376. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  377. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  378. )
  379. {
  380. return 0;
  381. }
  382. int CSanTakShiWei::SetDeviceParam( CCommProcess *pComm, //串口对象指针
  383. int nAddr, // 设备地址
  384. char szSendMsg[UPS_SANTAKSHIWEI_SEND_MSG], //发送Buffer
  385. char szCmd[MAX_CMD], // 命令
  386. char szMsg[VAR_MSG], // 接收Buffer
  387. int &nIndex, // 变量索引,针对接收Buffer而言
  388. int &nLen, // 变量长度
  389. char szType[TYPE_LENGTH] ) // 变量数据类型
  390. {
  391. return 0;
  392. }