UpsUH11.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: UpsUH11.cpp //////
  4. ////// 作 者: Suguobing //////
  5. ////// 创建时间: //////
  6. ////// 说 明: UH11协议 //////
  7. ////// //////
  8. ////// 修改时间:2010-08-05 //////
  9. ////// 修改说明:数据转换 //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "CommProcess.h"
  14. #include "UpsUH11.h"
  15. CUpsUh11::CUpsUh11( 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_szUh_Q41Msg, 0, sizeof(m_szUh_Q41Msg));
  27. memset(m_szUh_Q43Msg, 0, sizeof(m_szUh_Q43Msg));
  28. memset(m_szUh_Q44Msg, 0, sizeof(m_szUh_Q44Msg));
  29. memset(m_szUh_Q45Msg, 0, sizeof(m_szUh_Q45Msg));
  30. memset(m_szUh_Q4FMsg, 0, sizeof(m_szUh_Q4FMsg));
  31. memset(m_szUh_Q50Msg, 0, sizeof(m_szUh_Q50Msg));
  32. m_dwOnlineTick = 0;
  33. m_devOnline = TRUE;
  34. }
  35. CUpsUh11::~CUpsUh11()
  36. {
  37. #if IS_USE_READMSG_CS
  38. DeleteCriticalSection(& m_csReadMsg);
  39. #endif
  40. MTVERIFY(CloseHandle(m_hSemComm));
  41. CloseComm();
  42. }
  43. BOOL CUpsUh11::UpsUhOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  44. {
  45. BOOL bResult = FALSE;
  46. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  47. return bResult;
  48. }
  49. int CUpsUh11::SendReadRequest(
  50. char szPath[MAX_PATH],
  51. char szIniName[MAX_PATH],
  52. int nCommPort,
  53. int nAddr,
  54. char szCmd[MAX_CMD],
  55. char szMsg[VAR_MSG],
  56. int nReversed1,
  57. int nReversed2,
  58. int nReversed3,
  59. int nReversed4,
  60. int nReversed5,
  61. float fReversed1,
  62. float fReversed2,
  63. float fReversed3,
  64. char szReversed1[MAX_RESERVED1],
  65. char szReversed2[MAX_RESERVED2],
  66. char szReversed3[MAX_RESERVED3],
  67. char szReversed4[MAX_RESERVED4],
  68. char szReversed5[MAX_RESERVED5]
  69. )
  70. {
  71. //LOG4C((LOG_NOTICE,"CUpsUh11::SendReadRequest"));
  72. int nIndex = 0, nLen = 0;
  73. char szSendMsg[UPS_UH_SEND_MSG] = {0};
  74. char szType[UPS_TYPE_LENGTH] = {0};
  75. int startBit = 0;
  76. int endBit = 0;
  77. m_address = nAddr;
  78. GetUhFromIni(szPath, szIniName, szCmd, szSendMsg, szType, nIndex, nLen, startBit, endBit);
  79. CCommProcess *pComm = FindComm(nCommPort);
  80. if( pComm == NULL ) return -1;
  81. int nRet = -1;
  82. if( ( strlen(m_szUh_Q41Msg) == 0 &&
  83. (strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  84. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  85. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-5") == 0 ||
  86. strcmp(szCmd, "cmd-6") == 0 || strcmp(szCmd, "cmd-7") == 0 ||
  87. strcmp(szCmd, "cmd-8") == 0 || strcmp(szCmd, "cmd-9") == 0 ||
  88. strcmp(szCmd, "cmd-10") == 0 || strcmp(szCmd, "cmd-11") == 0 ||
  89. strcmp(szCmd, "cmd-12") == 0 || strcmp(szCmd, "cmd-13") == 0 ||
  90. strcmp(szCmd, "cmd-14") == 0 || strcmp(szCmd, "cmd-15") == 0 ||
  91. strcmp(szCmd, "cmd-16") == 0 || strcmp(szCmd, "cmd-17") == 0 ||
  92. strcmp(szCmd, "cmd-18") == 0 ) ) || //获取系统模拟量量化数据(浮点数)
  93. ( strlen(m_szUh_Q43Msg) == 0 &&
  94. (strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0||
  95. strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 ||
  96. strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ) )||//UPS工作状态及传送顺序
  97. ( strlen(m_szUh_Q44Msg) == 0 &&
  98. (strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 ||
  99. strcmp(szCmd, "cmd-27") == 0||strcmp(szCmd, "cmd-28") == 0 ||
  100. strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 ||
  101. strcmp(szCmd, "cmd-31") == 0 || strcmp(szCmd, "cmd-32") == 0 ||
  102. strcmp(szCmd, "cmd-33") == 0 || strcmp(szCmd, "cmd-34") == 0 ||
  103. strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0 ||
  104. strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 ) ) ||//UPS告警内容及传送
  105. ( strlen(m_szUh_Q45Msg) == 0 &&
  106. (strcmp(szCmd, "cmd-39") == 0 ) ) ||//获取系统参数(浮点数)
  107. ( strlen(m_szUh_Q4FMsg) == 0 &&
  108. (strcmp(szCmd, "cmd-40") == 0 ) ) ||//获取通信协议版本号
  109. ( strlen(m_szUh_Q50Msg) == 0 &&
  110. (strcmp(szCmd, "cmd-41") == 0 ) ) || //获取设备地址
  111. strcmp(szCmd, "cmd-1") == 0 ||
  112. strcmp(szCmd, "cmd-19") == 0 ||
  113. strcmp(szCmd, "cmd-25") == 0 ||
  114. strcmp(szCmd, "cmd-39") == 0 ||
  115. strcmp(szCmd, "cmd-40") == 0 ||
  116. strcmp(szCmd, "cmd-41") == 0 )
  117. {
  118. nRet = GetDeviceParam( pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
  119. if( nRet != 0 ) return nRet;
  120. }
  121. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 )
  122. {
  123. m_devOnline = FALSE;
  124. //LOG4C((LOG_NOTICE,"串口通信FALSE"));
  125. }
  126. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 )
  127. {
  128. m_devOnline = TRUE;
  129. //LOG4C((LOG_NOTICE,"串口通信TRUE"));
  130. }
  131. if( m_devOnline == FALSE )
  132. {
  133. return -1;
  134. }
  135. nRet = GetUhQ41VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  136. nRet = GetUhQ43VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  137. nRet = GetUhQ44VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  138. nRet = GetUhQ45VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  139. nRet = GetUhQ4FVarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  140. nRet = GetUhQ50VarMsg(szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  141. //LOG4C((LOG_NOTICE, "%s = %s, return %d", szCmd, szMsg, nRet));
  142. //if (strcmp(szCmd , "cmd-24") == 0)
  143. //{
  144. //LOG4C((LOG_NOTICE, "%s = %s, return %d", szCmd, szMsg, nRet));
  145. //}
  146. //LOG4C_IF((strcmp(szCmd , "cmd-24") == 0, LOG_NOTICE, "nCommPort = %d,nAddr = %d, %s = %s, return %d",nCommPort, nAddr, szCmd, szMsg, nRet));
  147. //LOG4C_IF((strcmp(szCmd , "cmd-19") == 0, LOG_NOTICE, "nCommPort = %d,nAddr = %d, %s = %s, return %d",nCommPort, nAddr, szCmd, szMsg, nRet));
  148. return nRet;
  149. }
  150. int CUpsUh11::GetUhQ41VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit)
  151. {
  152. int nRet = 0;
  153. if (strcmp(szCmd, "cmd-1") == 0 ||
  154. strcmp(szCmd, "cmd-2") == 0 ||
  155. strcmp(szCmd, "cmd-3") == 0 ||
  156. strcmp(szCmd, "cmd-4") == 0 ||
  157. strcmp(szCmd, "cmd-6") == 0 ||
  158. strcmp(szCmd, "cmd-5") == 0 ||
  159. strcmp(szCmd, "cmd-6") == 0 ||
  160. strcmp(szCmd, "cmd-7") == 0 ||
  161. strcmp(szCmd, "cmd-8") == 0 ||
  162. strcmp(szCmd, "cmd-9") == 0 ||
  163. strcmp(szCmd, "cmd-10") == 0 ||
  164. strcmp(szCmd, "cmd-11") == 0 ||
  165. strcmp(szCmd, "cmd-12") == 0 ||
  166. strcmp(szCmd, "cmd-13") == 0 ||
  167. strcmp(szCmd, "cmd-14") == 0 ||
  168. strcmp(szCmd, "cmd-15") == 0 ||
  169. strcmp(szCmd, "cmd-16") == 0 ||
  170. strcmp(szCmd, "cmd-17") == 0 ||
  171. strcmp(szCmd, "cmd-18") == 0 )
  172. {
  173. #if IS_USE_READMSG_CS
  174. EnterCriticalSection(&m_csReadMsg);
  175. #endif
  176. DataConversion(szType, m_szUh_Q41Msg + nIndex, szMsg, nLen, startBit, endBit);
  177. #if IS_USE_READMSG_CS
  178. LeaveCriticalSection(&m_csReadMsg);
  179. #endif
  180. nRet = 0;
  181. }
  182. return nRet;
  183. }
  184. int CUpsUh11::GetUhQ43VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  185. {
  186. int nRet = 0;
  187. if (strcmp(szCmd, "cmd-19") == 0 ||
  188. strcmp(szCmd, "cmd-20") == 0 ||
  189. strcmp(szCmd, "cmd-21") == 0 ||
  190. strcmp(szCmd, "cmd-22") == 0 ||
  191. strcmp(szCmd, "cmd-23") == 0 ||
  192. strcmp(szCmd, "cmd-24") == 0 )
  193. {
  194. #if IS_USE_READMSG_CS
  195. EnterCriticalSection(&m_csReadMsg);
  196. #endif
  197. DataConversion(szType, m_szUh_Q43Msg + nIndex, szMsg, nLen, startBit, endBit);
  198. #if IS_USE_READMSG_CS
  199. LeaveCriticalSection(&m_csReadMsg);
  200. #endif
  201. nRet = 0;
  202. }
  203. return nRet;
  204. }
  205. int CUpsUh11::GetUhQ44VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  206. {
  207. int nRet = 0;
  208. if (strcmp(szCmd, "cmd-25") == 0 ||
  209. strcmp(szCmd, "cmd-26") == 0 ||
  210. strcmp(szCmd, "cmd-27") == 0 ||
  211. strcmp(szCmd, "cmd-28") == 0 ||
  212. strcmp(szCmd, "cmd-29") == 0 ||
  213. strcmp(szCmd, "cmd-30") == 0 ||
  214. strcmp(szCmd, "cmd-31") == 0 ||
  215. strcmp(szCmd, "cmd-32") == 0 ||
  216. strcmp(szCmd, "cmd-33") == 0 ||
  217. strcmp(szCmd, "cmd-34") == 0 ||
  218. strcmp(szCmd, "cmd-35") == 0 ||
  219. strcmp(szCmd, "cmd-36") == 0 ||
  220. strcmp(szCmd, "cmd-37") == 0 ||
  221. strcmp(szCmd, "cmd-38") == 0 )
  222. {
  223. #if IS_USE_READMSG_CS
  224. EnterCriticalSection(&m_csReadMsg);
  225. #endif
  226. DataConversion(szType, m_szUh_Q44Msg + nIndex, szMsg, nLen, startBit, endBit);
  227. #if IS_USE_READMSG_CS
  228. LeaveCriticalSection(&m_csReadMsg);
  229. #endif
  230. nRet = 0;
  231. }
  232. return nRet;
  233. }
  234. int CUpsUh11::GetUhQ45VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  235. {
  236. int nRet = 0;
  237. if (strcmp(szCmd, "cmd-39") == 0 )
  238. {
  239. #if IS_USE_READMSG_CS
  240. EnterCriticalSection(&m_csReadMsg);
  241. #endif
  242. DataConversion(szType, m_szUh_Q45Msg + nIndex, szMsg, nLen, startBit, endBit);
  243. #if IS_USE_READMSG_CS
  244. LeaveCriticalSection(&m_csReadMsg);
  245. #endif
  246. nRet = 0;
  247. }
  248. return nRet;
  249. }
  250. int CUpsUh11::GetUhQ4FVarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  251. {
  252. int nRet = 0;
  253. if (strcmp(szCmd, "cmd-40") == 0 )
  254. {
  255. #if IS_USE_READMSG_CS
  256. EnterCriticalSection(&m_csReadMsg);
  257. #endif
  258. DataConversion(szType, m_szUh_Q4FMsg + nIndex, szMsg, nLen, startBit, endBit);
  259. #if IS_USE_READMSG_CS
  260. LeaveCriticalSection(&m_csReadMsg);
  261. #endif
  262. nRet = 0;
  263. }
  264. return nRet;
  265. }
  266. int CUpsUh11::GetUhQ50VarMsg(char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit )
  267. {
  268. int nRet = 0;
  269. if (strcmp(szCmd, "cmd-41") == 0 )
  270. {
  271. #if IS_USE_READMSG_CS
  272. EnterCriticalSection(&m_csReadMsg);
  273. #endif
  274. DataConversion(szType, m_szUh_Q50Msg + nIndex, szMsg, nLen, startBit, endBit);
  275. #if IS_USE_READMSG_CS
  276. LeaveCriticalSection(&m_csReadMsg);
  277. #endif
  278. nRet = 0;
  279. }
  280. return nRet;
  281. }
  282. int CUpsUh11::GetDeviceParam(
  283. CCommProcess *pComm, //串口对象指针
  284. char szSendMsg[UPS_UH_SEND_MSG], //发送Buffer
  285. char szCmd[MAX_CMD], // 命令
  286. char szMsg[VAR_MSG], // 接收Buffer
  287. int &nIndex, // 变量索引,针对接收Buffer而言
  288. int &nLen, // 变量长度
  289. char szType[TYPE_LENGTH]) // 变量数据类型
  290. {
  291. int nRet = -1;
  292. nRet = RequestStatus(pComm, szSendMsg);
  293. if( nRet != 0 )
  294. {
  295. return nRet; // 串口忙
  296. }
  297. nRet = ResponseStatus(pComm, szCmd, szMsg, nIndex, nLen, szType);
  298. return nRet;
  299. }
  300. int CUpsUh11::GetUhFromIni(char szPath[MAX_PATH], //服务器程序所在目录
  301. char szIniName[MAX_PATH], //配置文件名
  302. char szCmd[MAX_CMD], //命令
  303. char szSanTakSendMsg[MAX_CMD], //发送Buffer
  304. char nType[MAX_CMD],
  305. int &nIndex,
  306. int &nLen,
  307. int &startBit,
  308. int &endBit)
  309. {
  310. CHAR szFile[MAX_PATH + 1] = "";
  311. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  312. TRACE("szFile =%s\n",szFile);
  313. GetPrivateProfileString(szCmd, "SendCmd", "", szSanTakSendMsg, 10, szFile);
  314. szSanTakSendMsg[strlen(szSanTakSendMsg)] = '\0';
  315. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  316. nType[strlen(nType)] = '\0';
  317. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  318. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  319. return 0;
  320. }
  321. int CUpsUh11::RequestStatus(CCommProcess *pComm, char chSendMsg[UPS_UH_SEND_MSG] )
  322. {
  323. #if DEBUG_UH //开关量,用于测试模拟数据
  324. REQUEST_STRUCT structResuest;
  325. int nLen = sizeof(REQUEST_STRUCT);
  326. char chkSumBuf[5] ={0};
  327. char addBuf[UPS_TYPE_LENGTH] = {0};
  328. memset(&structResuest,0,nLen);
  329. //
  330. structResuest.headMes.Start = 0x7E;
  331. //
  332. structResuest.headMes.Version[0] = 0x32;
  333. structResuest.headMes.Version[1] = 0x31;
  334. //
  335. sprintf(addBuf, "%x",m_address);
  336. structResuest.headMes.Address[0] = ByteToAscii((m_address >> 4) & 0x0f);
  337. structResuest.headMes.Address[1] = ByteToAscii(m_address & 0x0f);
  338. //
  339. structResuest.headMes.Cid1[0] = 0x32;
  340. structResuest.headMes.Cid1[1] = 0x41;
  341. structResuest.headMes.Cid2[0] = ByteToAscii(chSendMsg[0] & 0x0f);
  342. structResuest.headMes.Cid2[1] = ByteToAscii(chSendMsg[1] & 0x0f);
  343. structResuest.headMes.Lenth[0] = 0x30;
  344. structResuest.headMes.Lenth[1] = 0x30;
  345. structResuest.headMes.Lenth[2] = 0x30;
  346. structResuest.headMes.Lenth[3] = 0x30;
  347. BYTE *pDatabuf = new BYTE[nLen - sizeof(structResuest.chkSum) - 1];
  348. memset(pDatabuf,0,nLen - sizeof(structResuest.chkSum) - 1);
  349. memcpy(pDatabuf,&structResuest,nLen - sizeof(structResuest.chkSum) - 1);
  350. GetCheckSum((char *)pDatabuf + 1, chkSumBuf, nLen - sizeof(structResuest.chkSum) - 2);
  351. structResuest.chkSum[0] = chkSumBuf[0];
  352. structResuest.chkSum[1] = chkSumBuf[1];
  353. structResuest.chkSum[2] = chkSumBuf[2];
  354. structResuest.chkSum[3] = chkSumBuf[3];
  355. structResuest.End = 0x0D;
  356. TRACE("headMes.Start= %c\nversion=%s\ncid1=%s\ncid2=%s\nlength=%s\nchksum=%s\n\n",
  357. structResuest.headMes.Start,structResuest.headMes.Version,
  358. structResuest.headMes.Address,structResuest.headMes.Cid1,
  359. structResuest.headMes.Cid2,structResuest.headMes.Lenth,structResuest.chkSum,structResuest.End);
  360. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  361. {
  362. int nDatalen = (int)sizeof(structResuest);
  363. ResetEvent(m_hSemComm);
  364. int nResult = pComm->Write((BYTE *)&structResuest, nDatalen);
  365. //LOG4C_HEX_DUMP((LOG_NOTICE, (char *)&structResuest, nDatalen));
  366. if (nResult == nDatalen)
  367. {
  368. }
  369. else
  370. {
  371. delete [] pDatabuf;
  372. SetEvent(m_hSemComm);
  373. return EER_CODE_UPSUH_COM_REGNUM;
  374. }
  375. }
  376. else
  377. {
  378. delete [] pDatabuf;
  379. return ERR_CODE_UPSUH_COM_BUSY; //串口忙
  380. }
  381. delete [] pDatabuf;
  382. #endif
  383. return 0;
  384. }
  385. int CUpsUh11::ResponseStatus(
  386. CCommProcess *pComm,
  387. char szCmd[MAX_CMD],
  388. char szMsg[VAR_MSG],
  389. int &nIndex,
  390. int &nLen,
  391. char szType[TYPE_LENGTH]
  392. )
  393. {
  394. #if DEBUG_UH
  395. int nReceiveLen = 0;
  396. int nProcessLen = 0;
  397. int nReadLen = 0;
  398. RESPONSE_STRUCT strcutResponse;
  399. memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
  400. nReceiveLen = sizeof(RESPONSE_STRUCT);
  401. char *pBuff = new char[nReceiveLen];
  402. memset(pBuff, 0 , nLen);
  403. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  404. if (nReadLen <= 0)
  405. {
  406. //LOG4C((LOG_NOTICE,"CUpsUh11::ResponseStatus nReadLen < = 0"));
  407. SetEvent(m_hSemComm);
  408. if (pBuff != NULL)
  409. {
  410. delete [] pBuff;
  411. pBuff = NULL;
  412. }
  413. return ERR_CODE_UPSUH_COM_READ_NO_DATA;
  414. }
  415. //长度校验出错
  416. if (LengthCheck(pBuff) != 0)
  417. {
  418. SetEvent(m_hSemComm);
  419. //LOG4C((LOG_NOTICE,"CUpsUh11::ResponseStatus LengthCheck ERROR "));
  420. if (pBuff != NULL)
  421. {
  422. delete [] pBuff;
  423. pBuff = NULL;
  424. }
  425. return ERR_CODE_RTN_LCHKSUM_ERROR;
  426. }
  427. if (!ChkSumCheck(pBuff, nReadLen))
  428. {
  429. //LOG4C((LOG_NOTICE,"CUpsUh11::ResponseStatus ChkSumCheck ERROR "));
  430. SetEvent(m_hSemComm);
  431. if (pBuff != NULL)
  432. {
  433. delete [] pBuff;
  434. pBuff = NULL;
  435. }
  436. return ERR_CODE_RTN_CHKSUM_ERROR; //校验码校验出错
  437. }
  438. if (CheckRtn(pBuff, nReadLen) != 0)
  439. {
  440. //LOG4C((LOG_NOTICE,"CUpsUh11::ResponseStatus CheckRtn ERROR "));
  441. SetEvent(m_hSemComm);
  442. if (pBuff != NULL)
  443. {
  444. delete [] pBuff;
  445. pBuff = NULL;
  446. }
  447. return ERR_CODE_RTN_CHKSUM_RTN_ERROR; //RTN校验出错
  448. }
  449. SetUhQ41VarMsg(szCmd, pBuff);
  450. SetUhQ43VarMsg(szCmd, pBuff);
  451. SetUhQ44VarMsg(szCmd, pBuff);
  452. SetUhQ45VarMsg(szCmd, pBuff);
  453. SetUhQ4FVarMsg(szCmd, pBuff);
  454. SetUhQ50VarMsg(szCmd, pBuff);
  455. m_dwOnlineTick = GetTickCount();
  456. // 设置串口等待事件为有信号
  457. SetEvent( m_hSemComm );
  458. if( pBuff != NULL)
  459. {
  460. delete[] pBuff;
  461. pBuff = NULL;
  462. }
  463. #else
  464. SimulationCommData();
  465. #endif
  466. return 0;
  467. }
  468. void CUpsUh11::SetUhQ41VarMsg(char szCmd[MAX_CMD], char *pBuff)
  469. {
  470. if (strcmp(szCmd, "cmd-1") == 0 ||
  471. strcmp(szCmd, "cmd-2") == 0 ||
  472. strcmp(szCmd, "cmd-3") == 0 ||
  473. strcmp(szCmd, "cmd-4") == 0 ||
  474. strcmp(szCmd, "cmd-6") == 0 ||
  475. strcmp(szCmd, "cmd-5") == 0 ||
  476. strcmp(szCmd, "cmd-6") == 0 ||
  477. strcmp(szCmd, "cmd-7") == 0 ||
  478. strcmp(szCmd, "cmd-8") == 0 ||
  479. strcmp(szCmd, "cmd-9") == 0 ||
  480. strcmp(szCmd, "cmd-10") == 0 ||
  481. strcmp(szCmd, "cmd-11") == 0 ||
  482. strcmp(szCmd, "cmd-12") == 0 ||
  483. strcmp(szCmd, "cmd-13") == 0 ||
  484. strcmp(szCmd, "cmd-14") == 0 ||
  485. strcmp(szCmd, "cmd-15") == 0 ||
  486. strcmp(szCmd, "cmd-16") == 0 ||
  487. strcmp(szCmd, "cmd-17") == 0 ||
  488. strcmp(szCmd, "cmd-18") == 0 )
  489. {
  490. #if IS_USE_READMSG_CS
  491. EnterCriticalSection(&m_csReadMsg);
  492. #endif
  493. memcpy(m_szUh_Q41Msg, pBuff, sizeof(m_szUh_Q41Msg));
  494. #if IS_USE_READMSG_CS
  495. LeaveCriticalSection(&m_csReadMsg);
  496. #endif
  497. }
  498. }
  499. void CUpsUh11::SetUhQ43VarMsg(char szCmd[MAX_CMD], char *pBuff)
  500. {
  501. if ( strcmp(szCmd, "cmd-19") == 0 ||
  502. strcmp(szCmd, "cmd-20") == 0 ||
  503. strcmp(szCmd, "cmd-21") == 0 ||
  504. strcmp(szCmd, "cmd-22") == 0 ||
  505. strcmp(szCmd, "cmd-23") == 0 ||
  506. strcmp(szCmd, "cmd-24") == 0
  507. )
  508. {
  509. #if IS_USE_READMSG_CS
  510. EnterCriticalSection(&m_csReadMsg);
  511. #endif
  512. memcpy(m_szUh_Q43Msg,pBuff,sizeof(m_szUh_Q43Msg));
  513. #if IS_USE_READMSG_CS
  514. LeaveCriticalSection(&m_csReadMsg);
  515. #endif
  516. }
  517. }
  518. void CUpsUh11::SetUhQ44VarMsg(char szCmd[MAX_CMD], char *pBuff)
  519. {
  520. if ( strcmp(szCmd, "cmd-25") == 0 ||
  521. strcmp(szCmd, "cmd-26") == 0 ||
  522. strcmp(szCmd, "cmd-27") == 0 ||
  523. strcmp(szCmd, "cmd-28") == 0 ||
  524. strcmp(szCmd, "cmd-29") == 0 ||
  525. strcmp(szCmd, "cmd-30") == 0 ||
  526. strcmp(szCmd, "cmd-31") == 0 ||
  527. strcmp(szCmd, "cmd-32") == 0 ||
  528. strcmp(szCmd, "cmd-33") == 0 ||
  529. strcmp(szCmd, "cmd-34") == 0 ||
  530. strcmp(szCmd, "cmd-35") == 0 ||
  531. strcmp(szCmd, "cmd-36") == 0 ||
  532. strcmp(szCmd, "cmd-37") == 0 ||
  533. strcmp(szCmd, "cmd-38") == 0 )
  534. {
  535. #if IS_USE_READMSG_CS
  536. EnterCriticalSection(&m_csReadMsg);
  537. #endif
  538. memcpy(m_szUh_Q44Msg,pBuff,sizeof(m_szUh_Q44Msg));
  539. #if IS_USE_READMSG_CS
  540. LeaveCriticalSection(&m_csReadMsg);
  541. #endif
  542. }
  543. }
  544. void CUpsUh11::SetUhQ45VarMsg(char szCmd[MAX_CMD], char *pBuff)
  545. {
  546. if ( strcmp(szCmd, "cmd-39") == 0 )
  547. {
  548. #if IS_USE_READMSG_CS
  549. EnterCriticalSection(&m_csReadMsg);
  550. #endif
  551. memcpy(m_szUh_Q45Msg,pBuff,sizeof(m_szUh_Q45Msg));
  552. #if IS_USE_READMSG_CS
  553. LeaveCriticalSection(&m_csReadMsg);
  554. #endif
  555. }
  556. }
  557. void CUpsUh11::SetUhQ4FVarMsg(char szCmd[MAX_CMD], char *pBuff)
  558. {
  559. if (strcmp(szCmd, "cmd-40") == 0 )
  560. {
  561. #if IS_USE_READMSG_CS
  562. EnterCriticalSection(&m_csReadMsg);
  563. #endif
  564. memcpy(m_szUh_Q4FMsg,pBuff,sizeof(m_szUh_Q4FMsg));
  565. #if IS_USE_READMSG_CS
  566. LeaveCriticalSection(&m_csReadMsg);
  567. #endif
  568. }
  569. }
  570. void CUpsUh11::SetUhQ50VarMsg(char szCmd[MAX_CMD], char *pBuff)
  571. {
  572. if (strcmp(szCmd, "cmd-41") == 0)
  573. {
  574. #if IS_USE_READMSG_CS
  575. EnterCriticalSection(&m_csReadMsg);
  576. #endif
  577. memcpy(m_szUh_Q50Msg,pBuff,sizeof(m_szUh_Q50Msg));
  578. #if IS_USE_READMSG_CS
  579. LeaveCriticalSection(&m_csReadMsg);
  580. #endif
  581. }
  582. }
  583. void CUpsUh11::SimulationCommData(void)
  584. {
  585. /*获取模拟量量化数据浮点数*/
  586. // 7E 32 31 30 30 32 41 30 30 37
  587. // 30 39 30 30 30 36 36 36 36 36
  588. // 44 34 33 36 36 36 36 36 44 34
  589. // 33 36 36 36 36 37 30 34 33 36
  590. // 36 36 36 35 43 34 33 20 20 20
  591. // 20 20 20 20 20 20 20 20 20 20
  592. // 20 20 20 43 44 43 43 34 43 34
  593. // 30 20 20 20 20 20 20 20 20 20
  594. // 20 20 20 20 20 20 20 39 41 31
  595. // 39 38 36 34 33 37 42 31 34 34
  596. // 38 34 32 30 30 30 30 30 36 37
  597. // 42 31 34 34 38 34 32 30 30 30
  598. // 30 30 30 34 31 30 30 30 30 43
  599. // 38 34 32 34 38 45 31 44 41 34
  600. // 31 36 36 45 36 36 41 34 33 30
  601. // 30 38 30 38 36 34 33 45 31 46
  602. // 30 0D
  603. m_szUh_Q41Msg[0] = 0x7E;
  604. m_szUh_Q41Msg[1] = 0x32;
  605. m_szUh_Q41Msg[2] = 0x31;
  606. m_szUh_Q41Msg[3] = 0x30;
  607. m_szUh_Q41Msg[4] = 0x30;
  608. m_szUh_Q41Msg[5] = 0x32;
  609. m_szUh_Q41Msg[6] = 0x41;
  610. m_szUh_Q41Msg[7] = 0x30;
  611. m_szUh_Q41Msg[8] = 0x30;
  612. m_szUh_Q41Msg[9] = 0x37;
  613. m_szUh_Q41Msg[10] = 0x30;
  614. m_szUh_Q41Msg[11] = 0x39;
  615. m_szUh_Q41Msg[12] = 0x30;
  616. m_szUh_Q41Msg[13] = 0x30;
  617. m_szUh_Q41Msg[14] = 0x30;
  618. m_szUh_Q41Msg[15] = 0x36;
  619. m_szUh_Q41Msg[16] = 0x36;
  620. m_szUh_Q41Msg[17] = 0x36;
  621. m_szUh_Q41Msg[18] = 0x36;
  622. m_szUh_Q41Msg[19] = 0x36;
  623. //44 34 33 36 36 36 36 36 44 34
  624. m_szUh_Q41Msg[20] = 0x44;
  625. m_szUh_Q41Msg[21] = 0x34;
  626. m_szUh_Q41Msg[22] = 0x33;
  627. m_szUh_Q41Msg[23] = 0x36;
  628. m_szUh_Q41Msg[24] = 0x36;
  629. m_szUh_Q41Msg[25] = 0x36;
  630. m_szUh_Q41Msg[26] = 0x36;
  631. m_szUh_Q41Msg[27] = 0x36;
  632. m_szUh_Q41Msg[28] = 0x44;
  633. m_szUh_Q41Msg[29] = 0x34;
  634. //33 36 36 36 36 37 30 34 33 36
  635. m_szUh_Q41Msg[30] = 0x33;
  636. m_szUh_Q41Msg[31] = 0x36;
  637. m_szUh_Q41Msg[32] = 0x36;
  638. m_szUh_Q41Msg[33] = 0x36;
  639. m_szUh_Q41Msg[34] = 0x36;
  640. m_szUh_Q41Msg[35] = 0x37;
  641. m_szUh_Q41Msg[36] = 0x30;
  642. m_szUh_Q41Msg[37] = 0x34;
  643. m_szUh_Q41Msg[38] = 0x33;
  644. m_szUh_Q41Msg[39] = 0x36;
  645. //36 36 36 35 43 34 33 20 20 20
  646. m_szUh_Q41Msg[40] = 0x36;
  647. m_szUh_Q41Msg[41] = 0x36;
  648. m_szUh_Q41Msg[42] = 0x36;
  649. m_szUh_Q41Msg[43] = 0x35;
  650. m_szUh_Q41Msg[44] = 0x43;
  651. m_szUh_Q41Msg[45] = 0x34;
  652. m_szUh_Q41Msg[46] = 0x33;
  653. m_szUh_Q41Msg[47] = 0x20;
  654. m_szUh_Q41Msg[48] = 0x20;
  655. m_szUh_Q41Msg[49] = 0x20;
  656. //20 20 20 20 20 20 20 20 20 20
  657. m_szUh_Q41Msg[50] = 0x20;
  658. m_szUh_Q41Msg[51] = 0x20;
  659. m_szUh_Q41Msg[52] = 0x20;
  660. m_szUh_Q41Msg[53] = 0x20;
  661. m_szUh_Q41Msg[54] = 0x20;
  662. m_szUh_Q41Msg[55] = 0x20;
  663. m_szUh_Q41Msg[56] = 0x20;
  664. m_szUh_Q41Msg[57] = 0x20;
  665. m_szUh_Q41Msg[58] = 0x20;
  666. m_szUh_Q41Msg[59] = 0x20;
  667. //20 20 20 30 30 30 30 36 30 34
  668. m_szUh_Q41Msg[60] = 0x20;
  669. m_szUh_Q41Msg[61] = 0x20;
  670. m_szUh_Q41Msg[62] = 0x20;
  671. m_szUh_Q41Msg[63] = 0x30;
  672. m_szUh_Q41Msg[64] = 0x30;
  673. m_szUh_Q41Msg[65] = 0x30;
  674. m_szUh_Q41Msg[66] = 0x30;
  675. m_szUh_Q41Msg[67] = 0x36;
  676. m_szUh_Q41Msg[68] = 0x30;
  677. m_szUh_Q41Msg[69] = 0x34;
  678. // 30 20 20 20 20 20 20 20 20 20
  679. m_szUh_Q41Msg[70] = 0x30;
  680. m_szUh_Q41Msg[71] = 0x20;
  681. m_szUh_Q41Msg[72] = 0x20;
  682. m_szUh_Q41Msg[73] = 0x20;
  683. m_szUh_Q41Msg[74] = 0x20;
  684. m_szUh_Q41Msg[75] = 0x20;
  685. m_szUh_Q41Msg[76] = 0x20;
  686. m_szUh_Q41Msg[77] = 0x20;
  687. m_szUh_Q41Msg[78] = 0x20;
  688. m_szUh_Q41Msg[79] = 0x20;
  689. //20 20 20 20 20 20 20 39 41 31
  690. m_szUh_Q41Msg[80] = 0x20;
  691. m_szUh_Q41Msg[81] = 0x20;
  692. m_szUh_Q41Msg[82] = 0x20;
  693. m_szUh_Q41Msg[83] = 0x20;
  694. m_szUh_Q41Msg[84] = 0x20;
  695. m_szUh_Q41Msg[85] = 0x20;
  696. m_szUh_Q41Msg[86] = 0x20;
  697. m_szUh_Q41Msg[87] = 0x39;
  698. m_szUh_Q41Msg[88] = 0x41;
  699. m_szUh_Q41Msg[89] = 0x31;
  700. //39 38 36 34 33 33 44 31 34 34
  701. m_szUh_Q41Msg[90] = 0x39;
  702. m_szUh_Q41Msg[91] = 0x38;
  703. m_szUh_Q41Msg[92] = 0x36;
  704. m_szUh_Q41Msg[93] = 0x34;
  705. m_szUh_Q41Msg[94] = 0x33;
  706. m_szUh_Q41Msg[95] = 0x33;
  707. m_szUh_Q41Msg[96] = 0x44;
  708. m_szUh_Q41Msg[97] = 0x30;
  709. m_szUh_Q41Msg[98] = 0x41;
  710. m_szUh_Q41Msg[99] = 0x34;
  711. //38 34 32 30 30 30 30 30 36 37
  712. m_szUh_Q41Msg[100] = 0x38;
  713. m_szUh_Q41Msg[101] = 0x34;
  714. m_szUh_Q41Msg[102] = 0x32;
  715. m_szUh_Q41Msg[103] = 0x30;
  716. m_szUh_Q41Msg[104] = 0x30;
  717. m_szUh_Q41Msg[105] = 0x30;
  718. m_szUh_Q41Msg[106] = 0x30;
  719. m_szUh_Q41Msg[107] = 0x30;
  720. m_szUh_Q41Msg[108] = 0x36;
  721. m_szUh_Q41Msg[109] = 0x37;
  722. // 42 31 34 34 38 34 32 30 30 30
  723. m_szUh_Q41Msg[110] = 0x42;
  724. m_szUh_Q41Msg[111] = 0x31;
  725. m_szUh_Q41Msg[112] = 0x34;
  726. m_szUh_Q41Msg[113] = 0x34;
  727. m_szUh_Q41Msg[114] = 0x38;
  728. m_szUh_Q41Msg[115] = 0x34;
  729. m_szUh_Q41Msg[116] = 0x32;
  730. m_szUh_Q41Msg[117] = 0x30;
  731. m_szUh_Q41Msg[118] = 0x30;
  732. m_szUh_Q41Msg[119] = 0x30;
  733. //30 30 30 34 31 30 30 30 30 43
  734. m_szUh_Q41Msg[120] = 0X30;
  735. m_szUh_Q41Msg[121] = 0x30;
  736. m_szUh_Q41Msg[122] = 0x30;
  737. m_szUh_Q41Msg[123] = 0x34;
  738. m_szUh_Q41Msg[124] = 0x31;
  739. m_szUh_Q41Msg[125] = 0x30;
  740. m_szUh_Q41Msg[126] = 0x30;
  741. m_szUh_Q41Msg[127] = 0x30;
  742. m_szUh_Q41Msg[128] = 0x30;
  743. m_szUh_Q41Msg[129] = 0x43;
  744. //38 34 32 34 38 45 31 44 41 34
  745. m_szUh_Q41Msg[130] = 0x38;
  746. m_szUh_Q41Msg[131] = 0x34;
  747. m_szUh_Q41Msg[132] = 0x32;
  748. m_szUh_Q41Msg[133] = 0x34;
  749. m_szUh_Q41Msg[134] = 0x38;
  750. m_szUh_Q41Msg[135] = 0x45;
  751. m_szUh_Q41Msg[136] = 0x31;
  752. m_szUh_Q41Msg[137] = 0x44;
  753. m_szUh_Q41Msg[138] = 0x41;
  754. m_szUh_Q41Msg[139] = 0x34;
  755. //31 36 36 45 36 36 41 34 33 30
  756. m_szUh_Q41Msg[140] = 0x31;
  757. m_szUh_Q41Msg[141] = 0x39;
  758. m_szUh_Q41Msg[142] = 0x41;
  759. m_szUh_Q41Msg[143] = 0x39;
  760. m_szUh_Q41Msg[144] = 0x39;
  761. m_szUh_Q41Msg[145] = 0x36;
  762. m_szUh_Q41Msg[146] = 0x42;
  763. m_szUh_Q41Msg[147] = 0x34;
  764. m_szUh_Q41Msg[148] = 0x33;
  765. m_szUh_Q41Msg[149] = 0x36;
  766. //30 38 30 38 36 34 33 45 31 46
  767. m_szUh_Q41Msg[150] = 0x36;
  768. m_szUh_Q41Msg[151] = 0x36;
  769. m_szUh_Q41Msg[152] = 0x36;
  770. m_szUh_Q41Msg[153] = 0x38;
  771. m_szUh_Q41Msg[154] = 0x36;
  772. m_szUh_Q41Msg[155] = 0x34;
  773. m_szUh_Q41Msg[156] = 0x33;
  774. m_szUh_Q41Msg[157] = 0x45;
  775. m_szUh_Q41Msg[158] = 0x31;
  776. m_szUh_Q41Msg[159] = 0x46;
  777. //30 0D
  778. m_szUh_Q41Msg[160] = 0x30;
  779. m_szUh_Q41Msg[161] = 0x0D;
  780. //7E 32 31 30 30 32 41 30 30 46
  781. //30 31 30 30 30 30 31 30 35 45
  782. //31 45 32 45 32 45 34 45 38 46
  783. //41 31 33 0D
  784. /*获取开关量输入状态*/
  785. // 7E 32 31 30 30 32 41 30 30 46
  786. m_szUh_Q43Msg[0] = 0x7E;
  787. m_szUh_Q43Msg[1] = 0x32;
  788. m_szUh_Q43Msg[2] = 0x31;
  789. m_szUh_Q43Msg[3] = 0x30;
  790. m_szUh_Q43Msg[4] = 0x30;
  791. m_szUh_Q43Msg[5] = 0x32;
  792. m_szUh_Q43Msg[6] = 0x41;
  793. m_szUh_Q43Msg[7] = 0x30;
  794. m_szUh_Q43Msg[8] = 0x30;
  795. m_szUh_Q43Msg[9] = 0x46;
  796. // 30 31 30 30 30 30 31 30 35 45
  797. m_szUh_Q43Msg[10] = 0x30;
  798. m_szUh_Q43Msg[11] = 0x31;
  799. m_szUh_Q43Msg[12] = 0x30;
  800. m_szUh_Q43Msg[13] = 0x30;
  801. m_szUh_Q43Msg[14] = 0x30;
  802. m_szUh_Q43Msg[15] = 0x30;
  803. m_szUh_Q43Msg[16] = 0x31;
  804. m_szUh_Q43Msg[17] = 0x30;
  805. m_szUh_Q43Msg[18] = 0x35;
  806. m_szUh_Q43Msg[19] = 0x45;
  807. // 31 45 32 45 32 45 34 45 38 46
  808. m_szUh_Q43Msg[20] = 0x31;
  809. m_szUh_Q43Msg[21] = 0x45;
  810. m_szUh_Q43Msg[22] = 0x32;
  811. m_szUh_Q43Msg[23] = 0x45;
  812. m_szUh_Q43Msg[24] = 0x32;
  813. m_szUh_Q43Msg[25] = 0x45;
  814. m_szUh_Q43Msg[26] = 0x34;
  815. m_szUh_Q43Msg[27] = 0x45;
  816. m_szUh_Q43Msg[28] = 0x38;
  817. m_szUh_Q43Msg[29] = 0x46;
  818. // 41 31 33 0D
  819. m_szUh_Q43Msg[30] = 0x41;
  820. m_szUh_Q43Msg[31] = 0x31;
  821. m_szUh_Q43Msg[32] = 0x33;
  822. m_szUh_Q43Msg[33] = 0x0D;
  823. /*获取告警状态*/
  824. // 7E 32 31 30 30 32 41 30 30 38
  825. // 30 32 36 30 30 30 30 30 30 30
  826. // 30 30 30 30 30 30 30 30 30 30
  827. // 41 30 30 30 30 30 30 30 30 30
  828. // 30 30 30 30 30 30 30 30 30 30
  829. // 30 46 36 36 39 0D
  830. m_szUh_Q44Msg[0] = 0x7E;
  831. m_szUh_Q44Msg[1] = 0x32;
  832. m_szUh_Q44Msg[2] = 0x31;
  833. m_szUh_Q44Msg[3] = 0x30;
  834. m_szUh_Q44Msg[4] = 0x30;
  835. m_szUh_Q44Msg[5] = 0x32;
  836. m_szUh_Q44Msg[6] = 0x41;
  837. m_szUh_Q44Msg[7] = 0x30;
  838. m_szUh_Q44Msg[8] = 0x30;
  839. m_szUh_Q44Msg[9] = 0x38;
  840. // 30 32 36 30 30 30 30 30 30 30
  841. m_szUh_Q44Msg[10] = 0x30;
  842. m_szUh_Q44Msg[11] = 0x32;
  843. m_szUh_Q44Msg[12] = 0x36;
  844. m_szUh_Q44Msg[13] = 0x30;
  845. m_szUh_Q44Msg[14] = 0x30;
  846. m_szUh_Q44Msg[15] = 0x30;
  847. m_szUh_Q44Msg[16] = 0x30;
  848. m_szUh_Q44Msg[17] = 0x30;
  849. m_szUh_Q44Msg[18] = 0x30;
  850. m_szUh_Q44Msg[19] = 0x30;
  851. // 30 30 30 30 30 30 30 30 30 30
  852. m_szUh_Q44Msg[20] = 0x30;
  853. m_szUh_Q44Msg[21] = 0x30;
  854. m_szUh_Q44Msg[22] = 0x30;
  855. m_szUh_Q44Msg[23] = 0x30;
  856. m_szUh_Q44Msg[24] = 0x30;
  857. m_szUh_Q44Msg[25] = 0x30;
  858. m_szUh_Q44Msg[26] = 0x30;
  859. m_szUh_Q44Msg[27] = 0x30;
  860. m_szUh_Q44Msg[28] = 0x30;
  861. m_szUh_Q44Msg[29] = 0x30;
  862. // 41 30 30 30 30 30 30 30 30 30
  863. m_szUh_Q44Msg[30] = 0x41;
  864. m_szUh_Q44Msg[31] = 0x30;
  865. m_szUh_Q44Msg[32] = 0x30;
  866. m_szUh_Q44Msg[33] = 0x30;
  867. m_szUh_Q44Msg[34] = 0x30;
  868. m_szUh_Q44Msg[35] = 0x30;
  869. m_szUh_Q44Msg[36] = 0x30;
  870. m_szUh_Q44Msg[37] = 0x30;
  871. m_szUh_Q44Msg[38] = 0x30;
  872. m_szUh_Q44Msg[39] = 0x30;
  873. // 30 30 30 30 30 30 30 30 30 30
  874. m_szUh_Q44Msg[40] = 0x30;
  875. m_szUh_Q44Msg[41] = 0x30;
  876. m_szUh_Q44Msg[42] = 0x30;
  877. m_szUh_Q44Msg[43] = 0x30;
  878. m_szUh_Q44Msg[44] = 0x30;
  879. m_szUh_Q44Msg[45] = 0x30;
  880. m_szUh_Q44Msg[46] = 0x30;
  881. m_szUh_Q44Msg[47] = 0x30;
  882. m_szUh_Q44Msg[48] = 0x30;
  883. m_szUh_Q44Msg[49] = 0x30;
  884. // 30 46 36 36 39 0D
  885. m_szUh_Q44Msg[50] = 0x30;
  886. m_szUh_Q44Msg[51] = 0x46;
  887. m_szUh_Q44Msg[52] = 0x36;
  888. m_szUh_Q44Msg[53] = 0x36;
  889. m_szUh_Q44Msg[54] = 0x39;
  890. m_szUh_Q44Msg[55] = 0x0D;
  891. }
  892. int CUpsUh11::LengthCheck(char szSendMsg[])
  893. {
  894. int len = (int)strlen(szSendMsg);
  895. char buffer[5] = {0};
  896. int j = 0;
  897. for (int i = 9; i< 13; i++)
  898. {
  899. buffer[j++] = szSendMsg[i]; //取出检验位
  900. }
  901. buffer[j] ='0';
  902. if(!ValiateLength(buffer)) // 相加模十六取反加一
  903. {
  904. return ERR_CODE_RTN_LCHKSUM_ERROR;
  905. }
  906. /*转换为十六进制*/
  907. return 0;
  908. }
  909. BOOL CUpsUh11::CheckCHKSUM(char szSendMsg[])
  910. {
  911. int j = 0;
  912. char buf1[5] = {0};
  913. char buf2[5] = {0};
  914. char szbuf[5] = {0};
  915. char chDest[5] = {0};
  916. unsigned short nData = 0;
  917. int len = (int)strlen(szSendMsg);
  918. char *buffer = new char [len-2];
  919. memset(buffer,0,sizeof(buffer));
  920. for (int i = 1; i < len-5; i++)
  921. {
  922. buffer[j++] = szSendMsg[i];
  923. }
  924. buffer[j] = '\0';
  925. for (j = 0; j < buffer[j] != '\0'; j ++)
  926. {
  927. nData += AsciiToBYTE(buffer[j]); //十进制相加
  928. }
  929. nData = ~nData +1; //取反加1 (有问题)
  930. nData %= 65536;
  931. //转换为16进制
  932. sprintf(buf2,"%x",nData);
  933. StrLower(buf2,chDest);
  934. j = 0;
  935. for (int i = len-5; i < len -1; i++)
  936. {
  937. buf1[j++] = szSendMsg[i];
  938. }
  939. buf1[j] = '\0';
  940. StrLower(buf1,szbuf);
  941. if (strcmp(szbuf,chDest) == 0)
  942. {
  943. delete [] buffer;
  944. return TRUE;
  945. }
  946. delete [] buffer;
  947. return FALSE;
  948. }
  949. BOOL CUpsUh11::CheckRtn(char szMsg[], int len)
  950. {
  951. //int len = (int)strlen(szMsg);
  952. char buffer[3] = {0};
  953. int j = 0;
  954. for (int i = 7; i < 9; i++)
  955. {
  956. buffer[j++] = szMsg[i];
  957. }
  958. buffer[j] = '\0';
  959. if (strcmp(buffer,"00") == 0)
  960. {
  961. return 0;
  962. }
  963. else if (strcmp(buffer,"01") == 0)
  964. {
  965. return ERR_CODE_RTN_VER_ERROR;
  966. }
  967. else if (strcmp(buffer,"02") == 0)
  968. {
  969. return ERR_CODE_RTN_CHKSUM_ERROR;
  970. }
  971. else if (strcmp(buffer,"03") == 0)
  972. {
  973. return ERR_CODE_RTN_LCHKSUM_ERROR;
  974. }
  975. else if (strcmp(buffer,"04") == 0)
  976. {
  977. return ERR_CODE_RTN_CID_ERROR;
  978. }
  979. else if (strcmp(buffer,"05") == 0)
  980. {
  981. return ERR_CODE_RTN_COMMAND_FORMAT;
  982. }
  983. else if (strcmp(buffer,"06") == 0)
  984. {
  985. return ERR_CODE_RTN_INVALID_LIMIT;
  986. }
  987. else if (strcmp(buffer,"E0") == 0)
  988. {
  989. return ERR_CODE_RTN_OPERATE_FAIL;
  990. }
  991. else if (strcmp(buffer,"E1") == 0)
  992. {
  993. return ERR_CODE_RTN_EQUIT_FAIL;
  994. }
  995. else if (strcmp(buffer,"E3")==0 ||
  996. strcmp(buffer,"E4")==0
  997. )
  998. {
  999. return ERR_CODE_RTN_KEEP;
  1000. }
  1001. else if (strcmp(buffer, "E5")==0 ||
  1002. strcmp(buffer, "E6") == 0 ||
  1003. strcmp(buffer, "E7") == 0 ||
  1004. strcmp(buffer, "E8") == 0 ||
  1005. strcmp(buffer, "E9") == 0
  1006. )
  1007. {
  1008. return ERR_CODE_RTN_WATCH;
  1009. }
  1010. else if (strcmp(buffer,"EA") == 0 ||
  1011. strcmp(buffer, "EB") == 0 ||
  1012. strcmp(buffer, "EC") == 0 ||
  1013. strcmp(buffer, "ED") == 0 ||
  1014. strcmp(buffer, "EE") == 0 ||
  1015. strcmp(buffer, "EF") == 0
  1016. )
  1017. {
  1018. return ERR_CODE_RTN_ELECTICAL_SOURCE;
  1019. }
  1020. return -1;
  1021. }
  1022. int CUpsUh11::UhRequestWrStatus(char szSendMsg[])
  1023. {
  1024. return 0;
  1025. }
  1026. int CUpsUh11::UhResponseWrStatus(char szSendMsg[])
  1027. {
  1028. return 0;
  1029. }
  1030. int CUpsUh11::StrLower(char *pSource, char *pDest)
  1031. {
  1032. int length, i;
  1033. length = (int)strlen(pSource);
  1034. for (i = 0; i < length; i++) {
  1035. if ((pSource[i] >= 'A') && (pSource[i] <= 'Z')){
  1036. pDest[i] = _tolower(pSource[i]);
  1037. }
  1038. else
  1039. pDest[i] = pSource[i];
  1040. }
  1041. return 0;
  1042. }
  1043. BOOL CUpsUh11::ValiateLength(char szChar[])
  1044. {
  1045. unsigned int nData = 0;
  1046. unsigned int nData1 =0;
  1047. unsigned short nDataLength = 0;
  1048. unsigned short nChar1;
  1049. nChar1 =AsciiToBYTE(szChar[1]) +
  1050. AsciiToBYTE(szChar[2]) +
  1051. AsciiToBYTE(szChar[3]);
  1052. nChar1 = (~nChar1) + 1;
  1053. nChar1 %=16;
  1054. nDataLength = AsciiToBYTE(szChar[0]);
  1055. if (nDataLength != nChar1)
  1056. {
  1057. return FALSE;
  1058. }
  1059. return TRUE;
  1060. }
  1061. int CUpsUh11::SendSetReuest(
  1062. char szPath[MAX_PATH], // 程序所在路径
  1063. char szIniName[MAX_PATH], // 配置文件名称
  1064. int nCommPort, // 串行端口
  1065. int nAddr, // 设备地址
  1066. char szCmd[MAX_CMD], // 请求命令
  1067. char szMsg[VAR_MSG], // 响应的值
  1068. int nReversed1, // 预留整形参数1接口
  1069. int nReversed2, // 预留整形参数2接口
  1070. int nReversed3, // 预留整形参数3接口
  1071. int nReversed4, // 预留整形参数4接口
  1072. int nReversed5, // 预留整形参数5接口
  1073. float fReversed1, // 预留float参数1接口
  1074. float fReversed2, // 预留float参数2接口
  1075. float fReversed3, // 预留float参数3接口
  1076. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  1077. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  1078. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  1079. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  1080. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  1081. )
  1082. {
  1083. return 0;
  1084. }
  1085. UINT CUpsUh11::GetCheckSum(char *pBuf, char chDest[5], int len)
  1086. {
  1087. WORD iSum = 0;
  1088. //unsigned char chCompliment[2] = {0};
  1089. for(int i=0; i<len; i++)//求和
  1090. {
  1091. //TRACE("%x\r\n", pBuf[i]);
  1092. iSum += pBuf[i];
  1093. }
  1094. WORD iCompliment = iSum;
  1095. iCompliment = ~iCompliment;//取反
  1096. iCompliment++;
  1097. //DigitToBinary(iCompliment, chDest, sizeof(chDest));
  1098. itoa(iCompliment, chDest, 16);
  1099. chDest[0] = lowercase2uppercase(chDest[0]);
  1100. chDest[1] = lowercase2uppercase(chDest[1]);
  1101. chDest[2] = lowercase2uppercase(chDest[2]);
  1102. chDest[3] = lowercase2uppercase(chDest[3]);
  1103. return atoi(chDest);
  1104. }