CommAsyn.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // CommAsyn.cpp: implementation of the CCommAsyn class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CommAsyn.h"
  6. #include <atlconv.h>
  7. #include "ProtocolModbus.h"
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CCommAsyn::CCommAsyn()
  17. {
  18. m_pProtocol=NULL;
  19. }
  20. CCommAsyn::~CCommAsyn()
  21. {
  22. if(m_pProtocol)
  23. {
  24. delete m_pProtocol;
  25. m_pProtocol=NULL;
  26. }
  27. }
  28. BOOL CCommAsyn::CloseComm()
  29. {
  30. return this->CloseComm();
  31. }
  32. BOOL CCommAsyn::InitParam(PPORTPARAM pPortParam)
  33. {
  34. if(m_pProtocol)
  35. {
  36. delete m_pProtocol;
  37. m_pProtocol=NULL;
  38. }
  39. m_pProtocol=new CProtocolModbus();
  40. return m_pProtocol->InitParam(pPortParam,this);
  41. }
  42. int CCommAsyn::WorkMain(
  43. int nAddr, //地址
  44. int nVer, //版本号
  45. int nCid1, //控制标识码
  46. int nCid2, //命令信息
  47. WORD wLenId, //INFO字节长度
  48. int nCmdID, //命令ID
  49. int nDataLen, //请求数据长度
  50. int nCmdPos, //变量索引
  51. int nCmdLen, //变量长度
  52. char chMsg[80], //读到的变量值
  53. char *byDataFlag) //保留未用
  54. {
  55. return m_pProtocol->WorkMain(nAddr,
  56. nVer,
  57. nCid1,
  58. nCid2,
  59. wLenId,
  60. nCmdID,
  61. nDataLen,
  62. nCmdPos,
  63. nCmdLen,
  64. chMsg,
  65. byDataFlag);
  66. }
  67. int CCommAsyn:: WriteCommand(
  68. int nCommPort, //端口
  69. int nAddr, //地址
  70. int nVer, //版本号
  71. int nCid1, //控制标识码
  72. int nCid2, //命令信息
  73. WORD wLenId, //INFO字节长度
  74. int nCmdType, //命令类型
  75. int nSetValue) //设定值
  76. {
  77. return m_pProtocol->WriteCommand( nCommPort,
  78. nAddr,
  79. nVer,
  80. nCid1,
  81. nCid2,
  82. wLenId,
  83. nCmdType,
  84. nSetValue );
  85. }
  86. int CCommAsyn:: WriteCommand(
  87. int nCommPort, //端口
  88. int nAddr, //地址
  89. int nSetType, //设定类型
  90. int nSetIndex, //设定值索引,只针对特殊变量(例如:空调制冷、制热温度)
  91. int nSetValue) //设定值
  92. {
  93. return m_pProtocol->WriteCommand( nCommPort,
  94. nAddr,
  95. nSetType,
  96. nSetIndex,
  97. nSetValue );
  98. }