SerialFactory.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #include "StdAfx.h"
  2. #include ".\serialfactory.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #endif
  6. // 引用全局对象;
  7. extern CRITICAL_SECTION g_cs_vt;
  8. /************************************************************************/
  9. /*
  10. 函数:构造函数;
  11. 测试结果->
  12. 内存泄漏:无
  13. 使用正常:正常
  14. */
  15. /************************************************************************/
  16. CSerialFactory::CSerialFactory( )
  17. {
  18. InitializeCriticalSection( &g_cs_vt );
  19. }
  20. /************************************************************************/
  21. /*
  22. 函数:析构函数;
  23. 测试结果->
  24. 内存泄漏:无
  25. 使用正常:正常
  26. */
  27. /************************************************************************/
  28. CSerialFactory::~CSerialFactory(void)
  29. {
  30. list<COM_STRUCT>::iterator it;
  31. for( it = m_listComm.begin(); it != m_listComm.end(); )
  32. {
  33. CSerialProcess *pComm = (*it).pComm;
  34. if ( pComm )
  35. {
  36. delete pComm;
  37. pComm = NULL;
  38. }
  39. m_listComm.erase( it++ );
  40. }
  41. DeleteCriticalSection( &g_cs_vt );
  42. }
  43. /************************************************************************/
  44. /*
  45. 函数:查找串口是否在容器中;
  46. 参数:
  47. iCommPort: 要查找的串口号;
  48. 测试结果->
  49. 内存泄漏:无
  50. 使用正常:正常
  51. */
  52. /************************************************************************/
  53. const CSerialProcess *CSerialFactory::FindComm(const int &iCommPort)
  54. {
  55. list<COM_STRUCT>::iterator it;
  56. COM_STRUCT tagComm;
  57. for( it = m_listComm.begin(); it != m_listComm.end(); )
  58. {
  59. tagComm = *(it++);
  60. if( tagComm.iCommPort == iCommPort && tagComm.pComm != NULL )
  61. return tagComm.pComm;
  62. }
  63. return NULL;
  64. }
  65. /************************************************************************/
  66. /*
  67. 函数:打开串口中;
  68. 参数:
  69. iCommPort 串口号;
  70. iRate 波特率;
  71. iDataBit 数据位;
  72. iStopBit 停止位;
  73. iParity 校验码;
  74. iRespTime 响应时间;
  75. iInterval 采集频率;
  76. 测试结果->
  77. 内存泄漏:无
  78. 使用正常:正常
  79. */
  80. /************************************************************************/
  81. BOOL CSerialFactory::OpenSerialPort(
  82. const int &iCommPort,
  83. const int &iRate,
  84. const int &iDataBit,
  85. const int &iStopBit,
  86. const int &iParity,
  87. const int &iRespTime,
  88. const int &iInterval)
  89. {
  90. if( FindComm( iCommPort ) == NULL )
  91. {
  92. COM_STRUCT tagComm;
  93. tagComm.iCommPort = iCommPort;
  94. tagComm.pComm = new CSerialProcess();
  95. if( !tagComm.pComm->OpenComm( iCommPort,iRate, iDataBit, iStopBit, iParity,iRespTime, iInterval) )
  96. {
  97. delete tagComm.pComm;
  98. tagComm.pComm = NULL;
  99. return FALSE;
  100. }
  101. else
  102. {
  103. m_listComm.insert(m_listComm.end(), tagComm);
  104. }
  105. }
  106. return TRUE;
  107. }
  108. /************************************************************************/
  109. /*
  110. 函数:关闭串口;
  111. 参数:
  112. iCommPort: 要关闭的串口号;
  113. 当 =-1 时,表示关闭所有串口;
  114. 测试结果->
  115. 内存泄漏:无
  116. 使用正常:正常
  117. 注意:
  118. 先erase容器元素后,再对容器元素进行delete;
  119. */
  120. /************************************************************************/
  121. BOOL CSerialFactory::CloseSerialPort( const int &iCommPort )
  122. {
  123. list<COM_STRUCT>::iterator it;
  124. COM_STRUCT tagComm;
  125. if ( iCommPort == -1)
  126. {
  127. for( it = m_listComm.begin(); it != m_listComm.end(); )
  128. {
  129. tagComm = *(it);
  130. it = m_listComm.erase(it);
  131. if (tagComm.pComm)
  132. {
  133. delete tagComm.pComm;
  134. tagComm.pComm = NULL;
  135. }
  136. }
  137. }
  138. else
  139. {
  140. for( it = m_listComm.begin(); it != m_listComm.end(); it++)
  141. {
  142. tagComm = *(it);
  143. if ( iCommPort == tagComm.iCommPort )
  144. {
  145. m_listComm.erase(it);
  146. if (tagComm.pComm)
  147. {
  148. delete tagComm.pComm;
  149. tagComm.pComm = NULL;
  150. }
  151. break;
  152. }
  153. }
  154. }
  155. return FALSE;
  156. }
  157. /************************************************************************/
  158. /*
  159. 函数:启动串口线程循环采集;
  160. 1.在串口打开后,且串口插入命令完成;
  161. 2.可以在这里实现在线程运行前某些命令的发送
  162. 如卡乐,先请求数据强制转换命令,然后才正常循环发送请求;
  163. 测试结果->
  164. 内存泄漏:无
  165. 使用正常:待完善
  166. */
  167. /************************************************************************/
  168. int CSerialFactory::AutoCollect( const int &iCommPort )
  169. {
  170. // 1.先判断有无特殊命令;
  171. // 2.处理完毕特殊命令,进行常规采集;
  172. list<COM_STRUCT>::iterator it;
  173. COM_STRUCT tagComm;
  174. for( it = m_listComm.begin(); it != m_listComm.end(); )
  175. {
  176. tagComm = *(it++);
  177. if ( tagComm.iCommPort == iCommPort)
  178. {
  179. tagComm.pComm->AutoCollect();
  180. break;
  181. }
  182. }
  183. return 0;
  184. }
  185. /************************************************************************/
  186. /*
  187. 函数:停止采集;
  188. 测试结果->
  189. 内存泄漏:无
  190. 使用正常:待完善
  191. */
  192. /************************************************************************/
  193. void CSerialFactory::StopCollect(const int &iCommPort)
  194. {
  195. list<COM_STRUCT>::iterator it;
  196. COM_STRUCT tagComm;
  197. for( it = m_listComm.begin(); it != m_listComm.end(); )
  198. {
  199. tagComm = *(it++);
  200. if ( tagComm.iCommPort == iCommPort)
  201. {
  202. tagComm.pComm->StopCollect();
  203. break;
  204. }
  205. }
  206. }
  207. /************************************************************************/
  208. /*
  209. 函数:移除设备命令;
  210. 参数:
  211. iCommPort: 串口;
  212. iAddr: 串口地址;
  213. 已完善
  214. 1.如果删除了一个设备的发送命令,当该串口只有该设备时,
  215. 那么这个设备也要从list<COM_STRUCT>中删除,
  216. 同时,如果该设备串口已经打开,也要从关闭串口释放资源;
  217. 测试结果->
  218. 内存泄漏:无
  219. 使用正常:完善
  220. */
  221. /************************************************************************/
  222. void CSerialFactory::RemoveDeivce(const int &iCommPort, const int &iAddr)
  223. {
  224. COM_STRUCT tagComm;
  225. list<COM_STRUCT>::iterator it,it_cur;
  226. int nCount = 0;//用来获取有多少个设备在该串口;
  227. for( it = m_listComm.begin(); it != m_listComm.end(); )
  228. {
  229. tagComm = *(it++);
  230. if ( tagComm.iCommPort == iCommPort)
  231. {
  232. LOG4C((LOG_NOTICE,"开始删除 %d串口地址%d命令",iCommPort,iAddr));
  233. tagComm.pComm->RemoveDeivce(iAddr);
  234. *it_cur = tagComm;
  235. nCount++;
  236. break;
  237. }
  238. }
  239. if ( nCount == 1 )
  240. {
  241. CSerialProcess *pComm = (*it_cur).pComm;
  242. if ( pComm )
  243. {
  244. delete pComm;
  245. pComm = NULL;
  246. }
  247. m_listComm.erase(it_cur);
  248. }
  249. }
  250. /************************************************************************/
  251. /*
  252. 函数:获取指定串口所有设备命令总数;
  253. 参数:
  254. iCommPort: 要获取的串口;
  255. 测试结果->
  256. 内存泄漏:无
  257. 使用正常:正常
  258. */
  259. /************************************************************************/
  260. int CSerialFactory::GetCommandsCount(const int &iCommPort)
  261. {
  262. list<COM_STRUCT>::iterator it;
  263. COM_STRUCT tagComm;
  264. for( it = m_listComm.begin(); it != m_listComm.end(); )
  265. {
  266. tagComm = *(it++);
  267. if ( tagComm.iCommPort == iCommPort)
  268. return tagComm.pComm->GetCommandsCount();
  269. }
  270. return -1;
  271. }
  272. /************************************************************************/
  273. /*
  274. 函数:主机做为下位机,自动接收串口数据;
  275. 参数:
  276. iCommPort: 要接收数据的串口;
  277. 测试结果->
  278. 内存泄漏:无
  279. 使用正常:待完善
  280. */
  281. /************************************************************************/
  282. void CSerialFactory::AutoScatter(const int &iCommPort)
  283. {
  284. }
  285. /************************************************************************/
  286. /*
  287. 函数:主机做为下位机,停止自动接收串口数据;
  288. 参数:
  289. iCommPort: 要接收数据的串口;
  290. 测试结果->
  291. 内存泄漏:无
  292. 使用正常:待完善
  293. */
  294. /************************************************************************/
  295. void CSerialFactory::StopScatter(const int &iCommPort)
  296. {
  297. }
  298. /************************************************************************/
  299. /*
  300. 函数:对指定串口指定地址的设备发送设置/请求命令;
  301. 参数:
  302. iCommPort: 指定的串口;
  303. iAddr 指定的地址;
  304. szCmd 发送命令串;
  305. iLen 命令串的长度;
  306. iRequestType 命令的类型,设置/请求数据;
  307. 测试结果->
  308. 内存泄漏:无
  309. 使用正常:待完善
  310. */
  311. /************************************************************************/
  312. void CSerialFactory::SendRequest( const int &iCommPort, const int &iAddr, BYTE *szCmd,const int &iLen, const int &iRequestType )
  313. {
  314. list<COM_STRUCT>::iterator it;
  315. COM_STRUCT tagComm;
  316. for( it = m_listComm.begin(); it != m_listComm.end(); )
  317. {
  318. tagComm = *(it++);
  319. if ( tagComm.iCommPort == iCommPort)
  320. {
  321. if ( iRequestType == 0)
  322. {
  323. // 发送 设置命令;
  324. tagComm.pComm->SetCommand(szCmd,iLen,iAddr);
  325. }
  326. else
  327. {
  328. // 发送请求读取命令;
  329. }
  330. break;
  331. }
  332. }
  333. }
  334. /************************************************************************/
  335. /*
  336. 函数:读取串口的数据,与SendRequest配合使用;
  337. 参数:
  338. iCommPort: 指定的串口;
  339. iAddr 指定的地址;
  340. 测试结果->
  341. 内存泄漏:无
  342. 使用正常:待完善
  343. */
  344. /************************************************************************/
  345. void CSerialFactory::ReadRespone( const int &iCommPort, const int &iAddr )
  346. {
  347. }
  348. /************************************************************************/
  349. /*
  350. 函数:对指定串口地址添加设备命令;
  351. 参数:
  352. iCommPort: 指定串口;
  353. iAddr 指定地址;
  354. szCommand 命令串;
  355. iLen 命令串长度;
  356. szMark 命令串的特征码;
  357. szCheck 命令串的校验方式;
  358. bRecv 是否对该命令串返回的数据保存;
  359. iSNum 命令串的发送顺序;
  360. 测试结果->
  361. 内存泄漏:无
  362. 使用正常:待完善
  363. */
  364. /************************************************************************/
  365. void CSerialFactory::InserCommand(
  366. const int &iCommPort,
  367. const int &iAddr,
  368. BYTE *szCommand,
  369. const int &iLen,
  370. const char *szMark,
  371. const char *szCheck,
  372. bool bRecv ,
  373. const int &iSNum )
  374. {
  375. list<COM_STRUCT>::iterator it;
  376. COM_STRUCT tagComm;
  377. for( it = m_listComm.begin(); it != m_listComm.end(); )
  378. {
  379. tagComm = *(it++);
  380. if ( tagComm.iCommPort == iCommPort)
  381. {
  382. #if USE_INSERT_AUTORAISE
  383. tagComm.pComm->RaiseSize(); // 使该串口的容器大小自增;
  384. #endif
  385. tagComm.pComm->InsertCommand(szCommand,iLen,iAddr,szMark,szCheck,iSNum,bRecv);
  386. break;
  387. }
  388. }
  389. }
  390. /************************************************************************/
  391. /*
  392. 函数:删除指定串口地址指定的命令串;
  393. 参数:
  394. iCommPort: 指定的串口;
  395. iAddr 指定的地址;
  396. szCommand 指定的命令串;
  397. iLen 命令串长度;
  398. 测试结果->
  399. 内存泄漏:无
  400. 使用正常:待完善
  401. */
  402. /************************************************************************/
  403. void CSerialFactory::DeleteRequestCommannd( const int &iCommPort,const int &iAddr, BYTE *szCommand, const int &iLen )
  404. {
  405. }
  406. /************************************************************************/
  407. /*
  408. 函数:获取指定配置信息的变量值;
  409. 参数:
  410. iCommPort: 指定的串口;
  411. iAddr 指定地址;
  412. szMark 指定特征码;
  413. iIndex 指定索引;
  414. iCutLen 指定长度;
  415. iSBit 指定起始位;
  416. iEBit 指定终止位;
  417. szValue 返回的值;
  418. iConvertType 指定的数据转换类型;
  419. 测试结果->
  420. 内存泄漏:无
  421. 使用正常:待完善
  422. */
  423. /************************************************************************/
  424. void CSerialFactory::GetAnalogValue(
  425. const int &iCommPort,
  426. const int &iAddr,
  427. char *szMark,
  428. const int &iIndex,
  429. const int &iCutLen,
  430. const int &iSBit,
  431. const int &iEBit,
  432. char *szValue,
  433. const int &iConvertType)
  434. {
  435. // 1.根据参数,从数据池中取出对应的数据串;
  436. // 2.根据参数,从数据串中取出对应的字节内容;
  437. // 3.根据参数,进行字节内容 数据类型转换;
  438. // 4.返回最终分析结果;
  439. }
  440. /************************************************************************/
  441. /*
  442. 函数:获取指定配置信息的变量值;
  443. 参数:
  444. iCommPort: 指定的串口;
  445. iAddr 指定地址;
  446. szMark 指定特征码;
  447. iIndex 指定索引;
  448. iCutLen 指定长度;
  449. iSBit 指定起始位;
  450. iEBit 指定终止位;
  451. szValue 返回的值;
  452. iConvertType 指定的数据转换类型;
  453. 测试结果->
  454. 内存泄漏:无
  455. 使用正常:待完善
  456. */
  457. /************************************************************************/
  458. void CSerialFactory::SetAnalogValue(
  459. const int &iCommPort,
  460. const int &iAddr,
  461. char *szMark,
  462. const int &iIndex,
  463. const int &iCutLen,
  464. const int &iSBit,
  465. const int &iEBit,
  466. char *szSetValue,
  467. const int &iConvertType)
  468. {
  469. // 1.根据参数,分析最终发送的设置命令;
  470. // 2.调用CCommProcess::SetCommand(...)函数,进行设置发送;
  471. }