CommInterface.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // CommInterface.cpp: implementation of the CCommInterface class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CommInterface.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CCommInterface::CCommInterface()
  15. {
  16. }
  17. CCommInterface::~CCommInterface()
  18. {
  19. list<COM_STRUCT>::iterator it;
  20. for( it = m_listComm.begin(); it != m_listComm.end(); )
  21. {
  22. m_listComm.erase( it++ );
  23. }
  24. }
  25. CCommProcess* CCommInterface::FindComm(int nCommPort)
  26. {
  27. list<COM_STRUCT>::iterator it;
  28. COM_STRUCT tagComm;
  29. for( it = m_listComm.begin(); it != m_listComm.end(); )
  30. {
  31. tagComm = *(it++);
  32. if( tagComm.nCommPort == nCommPort && tagComm.pComm != NULL )
  33. {
  34. return tagComm.pComm;
  35. }
  36. else
  37. {
  38. //LOG4C((LOG_NOTICE,"串口不存在"));
  39. }
  40. }
  41. return NULL;
  42. }
  43. BOOL CCommInterface::OpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  44. {
  45. if( FindComm( nCommPort ) == NULL )
  46. {
  47. PORTPARAM portParam;
  48. portParam.StartAddr = nAddr; //起止地址
  49. portParam.PortNo = nCommPort; //Com端口
  50. portParam.BaudRate = nRate; //波特率
  51. portParam.ByteSize = nDataBit; // Number of bits/byte, 4-8
  52. portParam.StopBits = nStopBit; /* 结束位 0,1,2 = 1, 1.5, 2 */
  53. portParam.Parity = nParity; /* 校验位 0-4=None,Odd,Even,Mark,Space */
  54. portParam.Interval = nInterval; //间隔时间
  55. COM_STRUCT tagComm;
  56. tagComm.nCommPort = nCommPort;
  57. tagComm.pComm = new CCommProcess();
  58. if( !tagComm.pComm->OpenComm( &portParam ) )
  59. {
  60. //LOG4C((LOG_NOTICE , "CCommInterface : 打开串口失败"));
  61. delete tagComm.pComm;
  62. tagComm.pComm = NULL;
  63. return FALSE;
  64. }
  65. else
  66. {
  67. m_listComm.insert(m_listComm.end(), tagComm);
  68. }
  69. }
  70. return TRUE;
  71. }
  72. BOOL CCommInterface::CloseComm()
  73. {
  74. list<COM_STRUCT>::iterator it;
  75. COM_STRUCT tagComm;
  76. for( it = m_listComm.begin(); it != m_listComm.end(); )
  77. {
  78. tagComm = *(it++);
  79. tagComm.pComm->CloseComm();
  80. }
  81. return TRUE;
  82. }