SecUPS.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. // SecUPS.cpp: implementation of the CSecUPS class.
  2. ////////////////////////////////////////////////////////////////////////////////
  3. ////// //////
  4. ////// 文 件: secups.cpp //////
  5. ////// 作 者: wangjianfeng //////
  6. ////// 创建时间: //////
  7. ////// 说 明: sec协议 //////
  8. ////// //////
  9. ////// 修改时间:2010-06-26 //////
  10. ////// 修改说明:新版本 //////
  11. ////// //////
  12. //////////////////////////////////////////////////////////////////////////////
  13. //////////////////////////////////////////////////////////////////////
  14. #include "stdafx.h"
  15. #include "CommProcess.h"
  16. #include "Global.h"
  17. #include "SecUPS.h"
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[]=__FILE__;
  21. #define new DEBUG_NEW
  22. #endif
  23. //////////////////////////////////////////////////////////////////////
  24. // Construction/Destruction
  25. //////////////////////////////////////////////////////////////////////
  26. CSecUPS::CSecUPS( char szPath[MAX_PATH], // 程序所在路径
  27. char szIniName[MAX_PATH], // 配置文件名称
  28. int nCommPort, // 串行端口
  29. int nAddr, // 设备地址
  30. int nRate, // 波特率
  31. int nDataBit, // 数据位
  32. int nStopBit, // 停止位
  33. int nParity, // 校验位
  34. int nInterval // 时间间隔
  35. )
  36. {
  37. #if IS_USE_READMSG_CS
  38. InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
  39. #endif
  40. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  41. memset(m_szSEC_ST1Msg, 0, sizeof(m_szSEC_ST1Msg));
  42. memset(m_szSEC_ST2Msg, 0, sizeof(m_szSEC_ST2Msg));
  43. memset(m_szSEC_ST3Msg, 0, sizeof(m_szSEC_ST3Msg));
  44. memset(m_szSEC_ST4Msg, 0, sizeof(m_szSEC_ST4Msg));
  45. memset(m_szSEC_ST5Msg, 0, sizeof(m_szSEC_ST5Msg));
  46. memset(m_szSEC_NOMMsg, 0, sizeof(m_szSEC_NOMMsg));
  47. memset(m_szSEC_ATRMsg, 0, sizeof(m_szSEC_ATRMsg));
  48. }
  49. CSecUPS::~CSecUPS()
  50. {
  51. #if IS_USE_READMSG_CS
  52. DeleteCriticalSection( &m_csReadMsg );
  53. #endif
  54. MTVERIFY( CloseHandle( m_hSemComm ) );
  55. CloseComm();
  56. }
  57. BOOL CSecUPS::UpsSECOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  58. {
  59. BOOL bResult = FALSE;
  60. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  61. return bResult;
  62. }
  63. int CSecUPS::SendReadRequest(
  64. char szPath[MAX_PATH],
  65. char szIniName[MAX_PATH],
  66. int nCommPort,
  67. int nAddr,
  68. char szCmd[MAX_CMD],
  69. char szRecvMsg[VAR_MSG],
  70. int nReversed1,
  71. int nReversed2,
  72. int nReversed3,
  73. int nReversed4,
  74. int nReversed5,
  75. float fReversed1,
  76. float fReversed2,
  77. float fReversed3,
  78. char szReversed1[MAX_RESERVED1],
  79. char szReversed2[MAX_RESERVED2],
  80. char szReversed3[MAX_RESERVED3],
  81. char szReversed4[MAX_RESERVED4],
  82. char szReversed5[MAX_RESERVED5]
  83. )
  84. {
  85. int nIndex = 0;
  86. char szSendMsg[SEC_SEND_MSG] = {0};
  87. GetSECFromIni(szPath, szIniName, szCmd,szSendMsg, nIndex);
  88. CCommProcess *pComm = FindComm(nCommPort);
  89. int nRet = -1;
  90. if (
  91. (strlen(m_szSEC_ST1Msg) == 0 &&
  92. (
  93. strcmp(szCmd, "cmd-1")==0||
  94. strcmp(szCmd, "cmd-2")==0||
  95. strcmp(szCmd, "cmd-3")==0||
  96. strcmp(szCmd, "cmd-4")==0||
  97. strcmp(szCmd, "cmd-5")==0||
  98. strcmp(szCmd, "cmd-6")==0||
  99. strcmp(szCmd, "cmd-7")==0||
  100. strcmp(szCmd, "cmd-8")==0
  101. )) ||
  102. (strlen(m_szSEC_ST2Msg) == 0 &&
  103. (strcmp(szCmd, "cmd-9")==0||
  104. strcmp(szCmd, "cmd-10")==0||
  105. strcmp(szCmd, "cmd-11")==0||
  106. strcmp(szCmd, "cmd-12")==0||
  107. strcmp(szCmd, "cmd-13")==0||
  108. strcmp(szCmd, "cmd-14")==0||
  109. strcmp(szCmd, "cmd-15")==0||
  110. strcmp(szCmd, "cmd-16")==0||
  111. strcmp(szCmd, "cmd-17")==0||
  112. strcmp(szCmd, "cmd-18")==0||
  113. strcmp(szCmd, "cmd-19")==0||
  114. strcmp(szCmd, "cmd-20")==0||
  115. strcmp(szCmd, "cmd-21")==0||
  116. strcmp(szCmd, "cmd-22")==0
  117. )) ||
  118. (strlen(m_szSEC_ST3Msg) == 0 &&
  119. (strcmp(szCmd, "cmd-23")==0||
  120. strcmp(szCmd, "cmd-24")==0||
  121. strcmp(szCmd, "cmd-25")==0||
  122. strcmp(szCmd, "cmd-26")==0||
  123. strcmp(szCmd, "cmd-27")==0||
  124. strcmp(szCmd, "cmd-28")==0||
  125. strcmp(szCmd, "cmd-29")==0||
  126. strcmp(szCmd, "cmd-30")==0||
  127. strcmp(szCmd, "cmd-31")==0||
  128. strcmp(szCmd, "cmd-32")==0||
  129. strcmp(szCmd, "cmd-33")==0||
  130. strcmp(szCmd, "cmd-34")==0||
  131. strcmp(szCmd, "cmd-35")==0||
  132. strcmp(szCmd, "cmd-36")==0||
  133. strcmp(szCmd, "cmd-37")==0
  134. )) ||
  135. (strlen(m_szSEC_ST4Msg) == 0 &&
  136. (strcmp(szCmd, "cmd-38")==0||
  137. strcmp(szCmd, "cmd-39")==0||
  138. strcmp(szCmd, "cmd-40")==0||
  139. strcmp(szCmd, "cmd-41")==0||
  140. strcmp(szCmd, "cmd-42")==0||
  141. strcmp(szCmd, "cmd-43")==0||
  142. strcmp(szCmd, "cmd-44")==0||
  143. strcmp(szCmd, "cmd-45")==0||
  144. strcmp(szCmd, "cmd-46")==0||
  145. strcmp(szCmd, "cmd-47")==0||
  146. strcmp(szCmd, "cmd-48")==0
  147. )) ||
  148. (strlen(m_szSEC_ST5Msg) == 0 &&
  149. (strcmp(szCmd, "cmd-49")==0||
  150. strcmp(szCmd, "cmd-50")==0||
  151. strcmp(szCmd, "cmd-51")==0||
  152. strcmp(szCmd, "cmd-52")==0||
  153. strcmp(szCmd, "cmd-53")==0||
  154. strcmp(szCmd, "cmd-54")==0||
  155. strcmp(szCmd, "cmd-55")==0||
  156. strcmp(szCmd, "cmd-56")==0||
  157. strcmp(szCmd, "cmd-57")==0||
  158. strcmp(szCmd, "cmd-58")==0||
  159. strcmp(szCmd, "cmd-59")==0||
  160. strcmp(szCmd, "cmd-60")==0||
  161. strcmp(szCmd, "cmd-61")==0||
  162. strcmp(szCmd, "cmd-62")==0||
  163. strcmp(szCmd, "cmd-63")==0
  164. )) ||
  165. (strlen(m_szSEC_NOMMsg) == 0 &&
  166. (strcmp(szCmd, "cmd-64")==0||
  167. strcmp(szCmd, "cmd-65")==0||
  168. strcmp(szCmd, "cmd-66")==0||
  169. strcmp(szCmd, "cmd-67")==0||
  170. strcmp(szCmd, "cmd-68")==0||
  171. strcmp(szCmd, "cmd-69")==0||
  172. strcmp(szCmd, "cmd-70")==0||
  173. strcmp(szCmd, "cmd-71")==0||
  174. strcmp(szCmd, "cmd-72")==0
  175. )) ||
  176. (strlen(m_szSEC_ATRMsg) == 0 &&
  177. (strcmp(szCmd, "cmd-73")==0))
  178. ||
  179. strcmp(szCmd, "cmd-1") == 0 ||
  180. strcmp(szCmd, "cmd-9") == 0 ||
  181. strcmp(szCmd, "cmd-23") == 0 ||
  182. strcmp(szCmd, "cmd-38") == 0 ||
  183. strcmp(szCmd, "cmd-49") == 0 ||
  184. strcmp(szCmd, "cmd-64") == 0 ||
  185. strcmp(szCmd, "cmd-73") == 0
  186. )
  187. {
  188. nRet = GetDeviceParam( pComm, szSendMsg, szCmd, szRecvMsg,nIndex );
  189. if (nRet != 0)
  190. {
  191. return nRet;
  192. }
  193. }
  194. nRet = GetSECST1VarMsg(szCmd, szRecvMsg, nIndex);
  195. nRet = GetSECST2VarMsg(szCmd, szRecvMsg, nIndex);
  196. nRet = GetSECST3VarMsg(szCmd, szRecvMsg, nIndex);
  197. nRet = GetSECST4VarMsg(szCmd, szRecvMsg, nIndex);
  198. nRet = GetSECST5VarMsg(szCmd, szRecvMsg, nIndex);
  199. nRet = GetSECNOMVarMsg(szCmd, szRecvMsg, nIndex);
  200. nRet = GetSECATRVarMsg(szCmd, szRecvMsg, nIndex);
  201. return nRet;
  202. }
  203. int CSecUPS::GetDeviceParam(
  204. CCommProcess *pComm, //串口对象指针
  205. char szSendMsg[SEC_SEND_MSG], //发送Buffer
  206. char szCmd[MAX_CMD], // 命令
  207. char szRecvMsg[VAR_MSG], // 接收Buffer
  208. int &nIndex) // 变量数据类型
  209. {
  210. int nRet = -1;
  211. nRet = RequestReadStatus(pComm, szSendMsg);
  212. if( nRet != 0 )
  213. {
  214. return nRet; // 串口忙
  215. }
  216. nRet = ResponseReadStatus(pComm, szCmd, szRecvMsg, nIndex);
  217. return nRet;
  218. }
  219. void CSecUPS::ReturnData(char *szSource, char *szDestion, int nIndex)
  220. {
  221. char nlen[3] = {0};
  222. nlen[0] = szSource[2];
  223. nlen[1] = szSource[3];
  224. nlen[2] = szSource[4];
  225. int len = atoi(nlen);
  226. BOOL bSi = FALSE;
  227. int i = 0;
  228. int index = 0;
  229. if (szSource[5] == 0x2c)
  230. {
  231. index = 1;
  232. }
  233. while(i < len)
  234. {
  235. if (index == nIndex)
  236. {
  237. int datalen = 0;
  238. for (int k = 0;i < len ; k++)
  239. {
  240. if (szSource[i+5] != 0x2C)
  241. szDestion[k] = szSource[i+5];
  242. else
  243. break;
  244. i++;
  245. }
  246. break;
  247. }
  248. if (szSource[i+5] == 0x2C)
  249. {
  250. int j = 0;
  251. for (;i<len;i++)
  252. {
  253. if (szSource[i+5] != 0x2C)
  254. break;
  255. j++;
  256. if (j > 1)
  257. index += 1 ;
  258. if (index == nIndex)
  259. break;
  260. }
  261. }
  262. if (index == nIndex)
  263. {
  264. int datalen = 0;
  265. for (int k = 0;i < len ; k++)
  266. {
  267. if (szSource[i+5] != 0x2C)
  268. szDestion[k] = szSource[i+5];
  269. else
  270. break;
  271. i++;
  272. }
  273. break;
  274. }
  275. if (szSource[i+5] != 0x2C)
  276. {
  277. index++;
  278. if (index == nIndex)
  279. continue;
  280. for (;i < len ;i++)
  281. if (szSource[i+5] == 0x2C)
  282. {
  283. --i;
  284. bSi = FALSE;
  285. break;
  286. }
  287. }
  288. i++;
  289. }
  290. }
  291. int CSecUPS::GetSECST1VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
  292. {
  293. int nRet =0;
  294. if( strcmp(szCmd, "cmd-1")==0||
  295. strcmp(szCmd, "cmd-2")==0||
  296. strcmp(szCmd, "cmd-3")==0||
  297. strcmp(szCmd, "cmd-4")==0||
  298. strcmp(szCmd, "cmd-5")==0||
  299. strcmp(szCmd, "cmd-6")==0||
  300. strcmp(szCmd, "cmd-7")==0||
  301. strcmp(szCmd, "cmd-8")==0
  302. )
  303. {
  304. #if IS_USE_READMSG_CS
  305. EnterCriticalSection( &m_csReadMsg );
  306. #endif
  307. ReturnData(m_szSEC_ST1Msg, szRecvMsg, nIndex);
  308. #if IS_USE_READMSG_CS
  309. LeaveCriticalSection(&m_csReadMsg);
  310. #endif
  311. nRet = 0;
  312. }
  313. return nRet;
  314. }
  315. int CSecUPS::GetSECST2VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
  316. {
  317. int nRet = 0;
  318. if(
  319. strcmp(szCmd, "cmd-9")==0||
  320. strcmp(szCmd, "cmd-10")==0||
  321. strcmp(szCmd, "cmd-11")==0||
  322. strcmp(szCmd, "cmd-12")==0||
  323. strcmp(szCmd, "cmd-13")==0||
  324. strcmp(szCmd, "cmd-14")==0||
  325. strcmp(szCmd, "cmd-15")==0||
  326. strcmp(szCmd, "cmd-16")==0||
  327. strcmp(szCmd, "cmd-17")==0||
  328. strcmp(szCmd, "cmd-18")==0||
  329. strcmp(szCmd, "cmd-19")==0||
  330. strcmp(szCmd, "cmd-20")==0||
  331. strcmp(szCmd, "cmd-21")==0||
  332. strcmp(szCmd, "cmd-22")==0 )
  333. {
  334. #if IS_USE_READMSG_CS
  335. EnterCriticalSection( &m_csReadMsg );
  336. #endif
  337. ReturnData(m_szSEC_ST2Msg, szRecvMsg, nIndex);
  338. #if IS_USE_READMSG_CS
  339. LeaveCriticalSection(&m_csReadMsg);
  340. #endif
  341. nRet = 0;
  342. }
  343. return nRet;
  344. }
  345. int CSecUPS::GetSECST3VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
  346. {
  347. int nRet = 0;
  348. if(
  349. strcmp(szCmd, "cmd-23")==0||
  350. strcmp(szCmd, "cmd-24")==0||
  351. strcmp(szCmd, "cmd-25")==0||
  352. strcmp(szCmd, "cmd-26")==0||
  353. strcmp(szCmd, "cmd-27")==0||
  354. strcmp(szCmd, "cmd-28")==0||
  355. strcmp(szCmd, "cmd-29")==0||
  356. strcmp(szCmd, "cmd-30")==0||
  357. strcmp(szCmd, "cmd-31")==0||
  358. strcmp(szCmd, "cmd-32")==0||
  359. strcmp(szCmd, "cmd-33")==0||
  360. strcmp(szCmd, "cmd-34")==0||
  361. strcmp(szCmd, "cmd-35")==0||
  362. strcmp(szCmd, "cmd-36")==0||
  363. strcmp(szCmd, "cmd-37")==0)
  364. {
  365. #if IS_USE_READMSG_CS
  366. EnterCriticalSection( &m_csReadMsg );
  367. #endif
  368. ReturnData(m_szSEC_ST3Msg, szRecvMsg, nIndex);
  369. #if IS_USE_READMSG_CS
  370. LeaveCriticalSection(&m_csReadMsg);
  371. #endif
  372. nRet = 0;
  373. }
  374. return nRet;
  375. }
  376. int CSecUPS::GetSECST4VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
  377. {
  378. int nRet = 0;
  379. if(
  380. strcmp(szCmd, "cmd-38")==0||
  381. strcmp(szCmd, "cmd-39")==0||
  382. strcmp(szCmd, "cmd-40")==0||
  383. strcmp(szCmd, "cmd-41")==0||
  384. strcmp(szCmd, "cmd-42")==0||
  385. strcmp(szCmd, "cmd-43")==0||
  386. strcmp(szCmd, "cmd-44")==0||
  387. strcmp(szCmd, "cmd-45")==0||
  388. strcmp(szCmd, "cmd-46")==0||
  389. strcmp(szCmd, "cmd-47")==0||
  390. strcmp(szCmd, "cmd-48")==0
  391. )
  392. {
  393. #if IS_USE_READMSG_CS
  394. EnterCriticalSection( &m_csReadMsg );
  395. #endif
  396. ReturnData(m_szSEC_ST4Msg, szRecvMsg, nIndex);
  397. #if IS_USE_READMSG_CS
  398. LeaveCriticalSection(&m_csReadMsg);
  399. #endif
  400. nRet = 0;
  401. }
  402. return nRet;
  403. }
  404. int CSecUPS::GetSECST5VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
  405. {
  406. int nRet = 0;
  407. if( strcmp(szCmd, "cmd-49")==0||
  408. strcmp(szCmd, "cmd-50")==0||
  409. strcmp(szCmd, "cmd-51")==0||
  410. strcmp(szCmd, "cmd-52")==0||
  411. strcmp(szCmd, "cmd-53")==0||
  412. strcmp(szCmd, "cmd-54")==0||
  413. strcmp(szCmd, "cmd-55")==0||
  414. strcmp(szCmd, "cmd-56")==0||
  415. strcmp(szCmd, "cmd-57")==0||
  416. strcmp(szCmd, "cmd-58")==0||
  417. strcmp(szCmd, "cmd-59")==0||
  418. strcmp(szCmd, "cmd-60")==0||
  419. strcmp(szCmd, "cmd-61")==0||
  420. strcmp(szCmd, "cmd-62")==0||
  421. strcmp(szCmd, "cmd-63")==0
  422. )
  423. {
  424. #if IS_USE_READMSG_CS
  425. EnterCriticalSection( &m_csReadMsg );
  426. #endif
  427. ReturnData(m_szSEC_ST5Msg, szRecvMsg, nIndex);
  428. #if IS_USE_READMSG_CS
  429. LeaveCriticalSection(&m_csReadMsg);
  430. #endif
  431. nRet = 0;
  432. }
  433. return nRet;
  434. }
  435. int CSecUPS::GetSECNOMVarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
  436. {
  437. int nRet = 0;
  438. if(strcmp(szCmd, "cmd-64")==0||
  439. strcmp(szCmd, "cmd-65")==0||
  440. strcmp(szCmd, "cmd-66")==0||
  441. strcmp(szCmd, "cmd-67")==0||
  442. strcmp(szCmd, "cmd-68")==0||
  443. strcmp(szCmd, "cmd-69")==0||
  444. strcmp(szCmd, "cmd-70")==0||
  445. strcmp(szCmd, "cmd-71")==0||
  446. strcmp(szCmd, "cmd-72")==0
  447. )
  448. {
  449. #if IS_USE_READMSG_CS
  450. EnterCriticalSection( &m_csReadMsg );
  451. #endif
  452. ReturnData(m_szSEC_NOMMsg, szRecvMsg, nIndex);
  453. #if IS_USE_READMSG_CS
  454. LeaveCriticalSection(&m_csReadMsg);
  455. #endif
  456. nRet = 0;
  457. }
  458. return nRet;
  459. }
  460. int CSecUPS::GetSECATRVarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex)
  461. {
  462. int nRet = 0;
  463. if(strcmp(szCmd, "cmd-73")==0 )
  464. {
  465. #if IS_USE_READMSG_CS
  466. EnterCriticalSection( &m_csReadMsg );
  467. #endif
  468. ReturnData(m_szSEC_ATRMsg, szRecvMsg, nIndex);
  469. #if IS_USE_READMSG_CS
  470. LeaveCriticalSection(&m_csReadMsg);
  471. #endif
  472. nRet = 0;
  473. }
  474. return nRet;
  475. }
  476. // 发送设置设备参数请求
  477. int CSecUPS::SendSetReuest(
  478. char szPath[MAX_PATH], // 程序所在路径
  479. char szIniName[MAX_PATH], // 配置文件名称
  480. int nCommPort, // 串行端口
  481. int nAddr, // 设备地址
  482. char szCmd[MAX_CMD], // 请求命令
  483. char szRecvMsg[VAR_MSG], // 响应的值
  484. int nReversed1, // 预留整形参数1接口
  485. int nReversed2, // 预留整形参数2接口
  486. int nReversed3, // 预留整形参数3接口
  487. int nReversed4, // 预留整形参数4接口
  488. int nReversed5, // 预留整形参数5接口
  489. float fReversed1, // 预留float参数1接口
  490. float fReversed2, // 预留float参数2接口
  491. float fReversed3, // 预留float参数3接口
  492. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  493. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  494. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  495. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  496. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  497. )
  498. {
  499. int nIndex = 0;
  500. char szSendMsg[SEC_SEND_MSG] = {0};
  501. GetSECFromIni(szPath, szIniName, szCmd,szSendMsg, nIndex);
  502. CCommProcess *pComm = FindComm(nCommPort);
  503. int nRet = -1;
  504. //if( (strlen(m_szSEC_ATRMsg) == 0 && strcmp(szCmd, "cmd-73")==0) || (strlen(m_szSEC_NOMMsg) == 0 &&(strcmp(szCmd, "cmd-72")) ))
  505. //{
  506. // nRet = RequestWriteStatus(pComm, szSendMsg);
  507. // if( nRet != 0 )
  508. // {
  509. // return nRet; // 串口忙
  510. // }
  511. // nRet = ResponseWriteStatus(pComm, szCmd, szRecvMsg, nIndex);
  512. //}
  513. return nRet;
  514. }
  515. //int CSecUPS::RequestWriteStatus( CCommProcess *pComm,char szSendMsg[SEC_SEND_MSG] )
  516. //{
  517. //#if DEBUG_SEC
  518. //
  519. // int iLen = sizeof(POTOCOLPARAM_STRUCT);
  520. // char chLength[4] = {0};
  521. // char chChkSum[5] = {0};
  522. //
  523. // POTOCOLPARAM_STRUCT RequestPara;
  524. //
  525. // memset( &RequestPara, 0, iLen );
  526. //
  527. // RequestPara.SendHead = 0x5E;
  528. //
  529. // RequestPara.SendType = 0x53;
  530. // RequestPara.SendLength[0] = 0x30;
  531. // RequestPara.SendLength[1] = 0x30;
  532. // RequestPara.SendLength[2] = 0x33;
  533. //
  534. // memcpy(RequestPara.SendData, szSendMsg, 3);
  535. //
  536. // //WaitForSingObject();等待有信号;
  537. // if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  538. // {
  539. // //求得数组字符实际长度;
  540. // int nDataLen = (int)strlen(szSendMsg);
  541. //
  542. // //ResetEvent()设置无信号状态
  543. // ResetEvent( m_hSemComm );
  544. //
  545. // int nResult = pComm->Write((unsigned char *)&RequestPara, nDataLen);
  546. // if( nResult == nDataLen )
  547. // {
  548. // }
  549. // else
  550. // {
  551. // //SetEvent设置有信号;
  552. // SetEvent( m_hSemComm );
  553. //
  554. // //返回 错误值;
  555. // return EER_CODE_COM_REGNUM;
  556. // }
  557. // }
  558. // else
  559. // {
  560. // //如果m_hSemCom无信号,返回错误;
  561. // return ERR_CODE_COM_BUSY;
  562. // }
  563. //#endif
  564. // return 0;
  565. //}
  566. //
  567. //int CSecUPS::ResponseWriteStatus( CCommProcess *pComm,char szCmd[MAX_CMD], char szRecvMsg[SEC_SEND_MSG],int &nIndex )
  568. //{
  569. // return 0;
  570. //}
  571. int CSecUPS::ResponseReadStatus(
  572. CCommProcess *pComm,
  573. char szCmd[MAX_CMD],
  574. char szRecvMsg[VAR_MSG],
  575. int &nIndex)
  576. {
  577. #if DEBUG_SEC
  578. //这个整型没用到;
  579. int nProcessLen = 0;
  580. POTOCOLPARAM_STRUCT structResponse;
  581. memset( &structResponse, 0, sizeof(POTOCOLPARAM_STRUCT) );
  582. int nReceiveLen = 0;
  583. nReceiveLen = sizeof(POTOCOLPARAM_STRUCT);
  584. char *pBuffer = new char[ nReceiveLen ];
  585. memset(pBuffer, 0, nReceiveLen);
  586. int nReadLen = 0;
  587. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);//pBuffer整串协议 数据
  588. if( nReadLen <= 0)
  589. {
  590. // 串口没有读到数据
  591. TRACE("串口没有读到数据!\r\n");
  592. SetEvent( m_hSemComm );
  593. if( pBuffer != NULL)
  594. {
  595. delete[] pBuffer;
  596. pBuffer = NULL;
  597. }
  598. return ERR_CODE_COM_READ_NO_DATA;
  599. }
  600. SetSECST1VarMsg( szCmd, pBuffer);
  601. SetSECST2VarMsg( szCmd, pBuffer);
  602. SetSECST3VarMsg( szCmd, pBuffer);
  603. SetSECST4VarMsg( szCmd, pBuffer);
  604. SetSECST5VarMsg( szCmd, pBuffer);
  605. SetSECNOMVarMsg( szCmd, pBuffer);
  606. SetSECATRVarMsg( szCmd, pBuffer);
  607. SetEvent( m_hSemComm );
  608. if( pBuffer != NULL)
  609. {
  610. delete[] pBuffer;
  611. pBuffer = NULL;
  612. }
  613. #else
  614. SimulationCommData();
  615. #endif
  616. return 0;
  617. }
  618. int CSecUPS::RequestReadStatus(CCommProcess *pComm,char szSendMsg[SEC_SEND_MSG])
  619. {
  620. #if DEBUG_SEC
  621. int iLen = sizeof(POTOCOLPARAM_STRUCT);
  622. char chLength[4] = {0};
  623. char chChkSum[5] = {0};
  624. POTOCOLPARAM_STRUCT RequestPara;
  625. memset( &RequestPara, 0, iLen );
  626. RequestPara.SendHead = 0x5E;
  627. RequestPara.SendType = 0x50;
  628. RequestPara.SendLength[0] = 0x30;
  629. RequestPara.SendLength[1] = 0x30;
  630. RequestPara.SendLength[2] = 0x33;
  631. memcpy(RequestPara.SendData, szSendMsg, 3);
  632. //WaitForSingObject();等待有信号;
  633. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  634. {
  635. //求得数组字符实际长度;
  636. int nDataLen = (int)strlen(szSendMsg);
  637. //ResetEvent()设置无信号状态
  638. ResetEvent( m_hSemComm );
  639. int nResult = pComm->Write((unsigned char *)&RequestPara, nDataLen);
  640. if( nResult == nDataLen )
  641. {
  642. }
  643. else
  644. {
  645. //SetEvent设置有信号;
  646. SetEvent( m_hSemComm );
  647. //返回 错误值;
  648. return EER_CODE_COM_REGNUM;
  649. }
  650. }
  651. else
  652. {
  653. //如果m_hSemCom无信号,返回错误;
  654. return ERR_CODE_COM_BUSY;
  655. }
  656. #endif
  657. return 0;
  658. }
  659. void CSecUPS::SetSECST1VarMsg(char szCmd[MAX_CMD], char *pBuff)
  660. {
  661. if( strcmp(szCmd, "cmd-1") == 0 ||
  662. strcmp(szCmd, "cmd-2") == 0 ||
  663. strcmp(szCmd, "cmd-3") == 0 ||
  664. strcmp(szCmd, "cmd-4") == 0 ||
  665. strcmp(szCmd, "cmd-5") == 0 ||
  666. strcmp(szCmd, "cmd-6") == 0 ||
  667. strcmp(szCmd, "cmd-7") == 0 ||
  668. strcmp(szCmd, "cmd-8") == 0
  669. )
  670. {
  671. #if IS_USE_READMSG_CS
  672. EnterCriticalSection( &m_csReadMsg );
  673. #endif
  674. memcpy(m_szSEC_ST1Msg, pBuff, sizeof(m_szSEC_ST1Msg));
  675. #if IS_USE_READMSG_CS
  676. LeaveCriticalSection(&m_csReadMsg);
  677. #endif
  678. }
  679. }
  680. void CSecUPS::SetSECST2VarMsg(char szCmd[MAX_CMD], char *pBuff)
  681. {
  682. if(
  683. strcmp(szCmd, "cmd-9")==0||
  684. strcmp(szCmd, "cmd-10")==0||
  685. strcmp(szCmd, "cmd-11")==0||
  686. strcmp(szCmd, "cmd-12")==0||
  687. strcmp(szCmd, "cmd-13")==0||
  688. strcmp(szCmd, "cmd-14")==0||
  689. strcmp(szCmd, "cmd-15")==0||
  690. strcmp(szCmd, "cmd-16")==0||
  691. strcmp(szCmd, "cmd-17")==0||
  692. strcmp(szCmd, "cmd-18")==0||
  693. strcmp(szCmd, "cmd-19")==0||
  694. strcmp(szCmd, "cmd-20")==0||
  695. strcmp(szCmd, "cmd-21")==0||
  696. strcmp(szCmd, "cmd-22")==0
  697. )
  698. {
  699. #if IS_USE_READMSG_CS
  700. EnterCriticalSection( &m_csReadMsg );
  701. #endif
  702. memcpy(m_szSEC_ST2Msg, pBuff, sizeof(m_szSEC_ST2Msg));
  703. #if IS_USE_READMSG_CS
  704. LeaveCriticalSection(&m_csReadMsg);
  705. #endif
  706. }
  707. }
  708. void CSecUPS::SetSECST3VarMsg(char szCmd[MAX_CMD], char *pBuff)
  709. {
  710. if( strcmp(szCmd, "cmd-23")==0||
  711. strcmp(szCmd, "cmd-24")==0||
  712. strcmp(szCmd, "cmd-25")==0||
  713. strcmp(szCmd, "cmd-26")==0||
  714. strcmp(szCmd, "cmd-27")==0||
  715. strcmp(szCmd, "cmd-28")==0||
  716. strcmp(szCmd, "cmd-29")==0||
  717. strcmp(szCmd, "cmd-30")==0||
  718. strcmp(szCmd, "cmd-31")==0||
  719. strcmp(szCmd, "cmd-32")==0||
  720. strcmp(szCmd, "cmd-33")==0||
  721. strcmp(szCmd, "cmd-34")==0||
  722. strcmp(szCmd, "cmd-35")==0||
  723. strcmp(szCmd, "cmd-36")==0||
  724. strcmp(szCmd, "cmd-37")==0
  725. )
  726. {
  727. #if IS_USE_READMSG_CS
  728. EnterCriticalSection( &m_csReadMsg );
  729. #endif
  730. memcpy(m_szSEC_ST3Msg, pBuff, sizeof(m_szSEC_ST3Msg));
  731. #if IS_USE_READMSG_CS
  732. LeaveCriticalSection(&m_csReadMsg);
  733. #endif
  734. }
  735. }
  736. void CSecUPS::SetSECST4VarMsg(char szCmd[MAX_CMD], char *pBuff)
  737. {
  738. if( strcmp(szCmd, "cmd-38")==0||
  739. strcmp(szCmd, "cmd-39")==0||
  740. strcmp(szCmd, "cmd-40")==0||
  741. strcmp(szCmd, "cmd-41")==0||
  742. strcmp(szCmd, "cmd-42")==0||
  743. strcmp(szCmd, "cmd-43")==0||
  744. strcmp(szCmd, "cmd-44")==0||
  745. strcmp(szCmd, "cmd-45")==0||
  746. strcmp(szCmd, "cmd-46")==0||
  747. strcmp(szCmd, "cmd-47")==0||
  748. strcmp(szCmd, "cmd-48")==0
  749. )
  750. {
  751. #if IS_USE_READMSG_CS
  752. EnterCriticalSection( &m_csReadMsg );
  753. #endif
  754. memcpy(m_szSEC_ST4Msg, pBuff, sizeof(m_szSEC_ST4Msg));
  755. #if IS_USE_READMSG_CS
  756. LeaveCriticalSection(&m_csReadMsg);
  757. #endif
  758. }
  759. }
  760. void CSecUPS::SetSECST5VarMsg(char szCmd[MAX_CMD], char *pBuff)
  761. {
  762. if(strcmp(szCmd, "cmd-49")==0||
  763. strcmp(szCmd, "cmd-50")==0||
  764. strcmp(szCmd, "cmd-51")==0||
  765. strcmp(szCmd, "cmd-52")==0||
  766. strcmp(szCmd, "cmd-53")==0||
  767. strcmp(szCmd, "cmd-54")==0||
  768. strcmp(szCmd, "cmd-55")==0||
  769. strcmp(szCmd, "cmd-56")==0||
  770. strcmp(szCmd, "cmd-57")==0||
  771. strcmp(szCmd, "cmd-58")==0||
  772. strcmp(szCmd, "cmd-59")==0||
  773. strcmp(szCmd, "cmd-60")==0||
  774. strcmp(szCmd, "cmd-61")==0||
  775. strcmp(szCmd, "cmd-62")==0||
  776. strcmp(szCmd, "cmd-63")==0
  777. )
  778. {
  779. #if IS_USE_READMSG_CS
  780. EnterCriticalSection( &m_csReadMsg );
  781. #endif
  782. memcpy(m_szSEC_ST5Msg, pBuff, sizeof(m_szSEC_ST5Msg));
  783. #if IS_USE_READMSG_CS
  784. LeaveCriticalSection(&m_csReadMsg);
  785. #endif
  786. }
  787. }
  788. void CSecUPS::SetSECNOMVarMsg(char szCmd[MAX_CMD], char *pBuff)
  789. {
  790. if( strcmp(szCmd, "cmd-64")==0||
  791. strcmp(szCmd, "cmd-65")==0||
  792. strcmp(szCmd, "cmd-66")==0||
  793. strcmp(szCmd, "cmd-67")==0||
  794. strcmp(szCmd, "cmd-68")==0||
  795. strcmp(szCmd, "cmd-69")==0||
  796. strcmp(szCmd, "cmd-70")==0||
  797. strcmp(szCmd, "cmd-71")==0||
  798. strcmp(szCmd, "cmd-72")==0
  799. )
  800. {
  801. #if IS_USE_READMSG_CS
  802. EnterCriticalSection( &m_csReadMsg );
  803. #endif
  804. memcpy(m_szSEC_NOMMsg, pBuff, sizeof(m_szSEC_NOMMsg));
  805. #if IS_USE_READMSG_CS
  806. LeaveCriticalSection(&m_csReadMsg);
  807. #endif
  808. }
  809. }
  810. void CSecUPS::SetSECATRVarMsg(char szCmd[MAX_CMD], char *pBuff)
  811. {
  812. if( strcmp(szCmd, "cmd-73")==0 )
  813. {
  814. #if IS_USE_READMSG_CS
  815. EnterCriticalSection( &m_csReadMsg );
  816. #endif
  817. memcpy(m_szSEC_ATRMsg, pBuff, sizeof(m_szSEC_ATRMsg));
  818. #if IS_USE_READMSG_CS
  819. LeaveCriticalSection(&m_csReadMsg);
  820. #endif
  821. }
  822. }
  823. int CSecUPS::GetSECFromIni(
  824. char szPath[MAX_PATH], //服务器程序所在目录
  825. char szIniName[MAX_PATH], //配置文件名
  826. char szCmd[MAX_CMD], //命令
  827. char szSendMsg[MAX_CMD], //发送Buffer
  828. int &nIndex
  829. )
  830. {
  831. CHAR szFile[MAX_PATH + 1] = "";
  832. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  833. GetPrivateProfileString(szCmd, "SenCmd", "", szSendMsg, 10, szFile);
  834. szSendMsg[strlen(szSendMsg)] = '\0';
  835. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  836. return 0;
  837. }
  838. void CSecUPS::SimulationCommData(void)
  839. {
  840. strcpy(m_szSEC_ST1Msg,"^D015,0,,,10,90,,,35");
  841. strcpy(m_szSEC_ST2Msg,"^D007,,,1180");
  842. strcpy(m_szSEC_ST3Msg,"^D007,,,1200");
  843. strcpy(m_szSEC_ST4Msg,"");
  844. strcpy(m_szSEC_ST5Msg,"^D013,,,1,,,,,,,,0");
  845. strcpy(m_szSEC_NOMMsg,"");
  846. strcpy(m_szSEC_ATRMsg,"");
  847. }