Daikin.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Daikin.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "Daikin.h"
  5. #include ".\daikin.h"
  6. #include "CommRS232.h"
  7. #include "Head.h"
  8. #include "winsock2.h"
  9. #include "ProtocolModbus.h"
  10. vector<__COM_STRUCT> g_vtComStruct;
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #endif
  14. //
  15. // 注意!
  16. //
  17. // 如果此 DLL 动态链接到 MFC
  18. // DLL,从此 DLL 导出并
  19. // 调入 MFC 的任何函数在函数的最前面
  20. // 都必须添加 AFX_MANAGE_STATE 宏。
  21. //
  22. // 例如:
  23. //
  24. // extern "C" BOOL PASCAL EXPORT ExportedFunction()
  25. // {
  26. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  27. // // 此处为普通函数体
  28. // }
  29. //
  30. // 此宏先于任何 MFC 调用
  31. // 出现在每个函数中十分重要。这意味着
  32. // 它必须作为函数中的第一个语句
  33. // 出现,甚至先于所有对象变量声明,
  34. // 这是因为它们的构造函数可能生成 MFC
  35. // DLL 调用。
  36. //
  37. // 有关其他详细信息,
  38. // 请参阅 MFC 技术说明 33 和 58。
  39. //
  40. // CDaikinApp
  41. BOOL InitComm( int nAddr,
  42. int nCommPort,
  43. int nRate,
  44. int nDataBit,
  45. int nStopBit,
  46. int nParity,
  47. int nInterval );
  48. int CommDataCollect(
  49. int nCommPort, //串口号
  50. int nAddr, //地址
  51. int nVer, //版本号
  52. int nCid1, //控制标识码
  53. int nCid2, //命令信息
  54. WORD wLenId, //INFO字节长度
  55. int nCmdID, //命令ID
  56. int nDataLen, //请求数据长度
  57. int nCmdPos, //变量索引
  58. int nCmdLen, //变量长度
  59. char chMsg[80], //读到的变量值
  60. char *byDataFlag); //保留未用
  61. int GetCommIndex( int nCommPort, int nAddr );
  62. BEGIN_MESSAGE_MAP(CDaikinApp, CWinApp)
  63. END_MESSAGE_MAP()
  64. // CDaikinApp 构造
  65. CDaikinApp::CDaikinApp()
  66. {
  67. // TODO: 在此处添加构造代码,
  68. // 将所有重要的初始化放置在 InitInstance 中
  69. }
  70. // 唯一的一个 CDaikinApp 对象
  71. CDaikinApp theApp;
  72. // CDaikinApp 初始化
  73. BOOL CDaikinApp::InitInstance()
  74. {
  75. CWinApp::InitInstance();
  76. return TRUE;
  77. }
  78. int CDaikinApp::ExitInstance()
  79. {
  80. // TODO: 在此添加专用代码和/或调用基类
  81. for( unsigned int i = 0; i < g_vtComStruct.size(); i++ )
  82. {
  83. if( g_vtComStruct[i].pComm )
  84. {
  85. delete g_vtComStruct[i].pComm;
  86. g_vtComStruct[i].pComm = NULL;
  87. }
  88. }
  89. return CWinApp::ExitInstance();
  90. }
  91. int DAIKIN_DLLInit(
  92. int nCommPort, //串口号
  93. int nAddr, //地址
  94. int nVer, //版本号
  95. int nCid1, //控制标识码
  96. int nCid2, //命令信息
  97. WORD wLenId, //INFO字节长度
  98. int nCmdID, //命令ID
  99. int nDataLen, //请求数据长度
  100. int nCmdPos, //变量索引
  101. int nCmdLen, //变量长度
  102. char chMsg[80], //读到的变量值
  103. char *byDataFlag) //保留未用
  104. {
  105. return CommDataCollect( nCommPort,
  106. nAddr,
  107. nVer,
  108. nCid1,
  109. nCid2,
  110. wLenId,
  111. nCmdID,
  112. nDataLen,
  113. nCmdPos,
  114. nCmdLen,
  115. chMsg,
  116. byDataFlag);
  117. }
  118. void DAIKIN_DLLUnInit()
  119. {
  120. // 关闭串口,放到退出Dll时去关闭 ExitInstance
  121. return;
  122. }
  123. //初始化串口
  124. BOOL DAIKIN_DLLInitCom( int nAddr, int nCommPort, int nRate,
  125. int nDataBit, int nStopBit, int nParity, int nInterval)
  126. {
  127. if (InitComm( nAddr, nCommPort, nRate, nDataBit, nStopBit, nParity, nInterval ))
  128. {
  129. return TRUE;
  130. }
  131. else
  132. {
  133. return FALSE;
  134. }
  135. }
  136. int DAIKIN_DLLSetParam(
  137. int nCommPort, //端口
  138. int nAddr, //地址
  139. int nVer, //版本号
  140. int nCid1, //控制标识码
  141. int nCid2, //命令信息
  142. WORD wLenId, //INFO字节长度
  143. int nCmdType, //命令类型
  144. int nSetValue) //设定值
  145. {
  146. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  147. if( nCommIndex == -1 ) return ERR_CODE_DAIKIN_COM_FAULT;
  148. CCommAsyn *pComm = NULL;
  149. pComm = g_vtComStruct[nCommIndex].pComm;
  150. int iResult = pComm->WriteCommand( nCommPort, nAddr, nVer, nCid1, nCid2, wLenId, nCmdType, nSetValue );
  151. return iResult;
  152. }
  153. int DAIKIN_DLLRemoteCtrl(
  154. int nCommPort, //端口
  155. int nAddr, //地址
  156. int nSetType, //设定类型
  157. int nSetIndex, //设定值索引,只针对特殊变量(例如:空调制冷、制热温度)
  158. int nSetValue) //设定值
  159. {
  160. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  161. if( nCommIndex == -1 ) return ERR_CODE_DAIKIN_COM_FAULT;
  162. CCommAsyn *pComm = NULL;
  163. pComm = g_vtComStruct[nCommIndex].pComm;
  164. int iResult = pComm->WriteCommand( nCommPort, nAddr, nSetType, nSetIndex, nSetValue );
  165. return iResult;
  166. }
  167. BOOL InitComm( int nAddr, int nCommPort, int nRate,
  168. int nDataBit, int nStopBit, int nParity, int nInterval )
  169. {
  170. CCommAsyn *pComm = NULL;
  171. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  172. if( nCommIndex == -1 )
  173. {
  174. pComm = new CCommRS232;
  175. PORTPARAM portParam;
  176. portParam.StartAddr = nAddr; //起止地址
  177. portParam.PortNo = nCommPort; //Com端口
  178. portParam.BaudRate = nRate; //波特率
  179. portParam.ByteSize = nDataBit; // Number of bits/byte, 4-8
  180. portParam.StopBits = nStopBit; /* 结束位 0,1,2 = 1, 1.5, 2 */
  181. portParam.Parity = nParity; /* 校验位 0-4=None,Odd,Even,Mark,Space */
  182. portParam.Interval = nInterval; //间隔时间
  183. if(!pComm->InitParam(&portParam))
  184. {
  185. delete pComm;
  186. pComm = NULL;
  187. return FALSE;
  188. }
  189. else
  190. {
  191. COM_STRUCT tagComStruct;
  192. tagComStruct.nAddr = nAddr;
  193. tagComStruct.nCommPort = nCommPort;
  194. tagComStruct.pComm = pComm;
  195. // 串口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
  196. g_vtComStruct.push_back( tagComStruct );
  197. }
  198. }
  199. return TRUE;
  200. }
  201. int CommDataCollect(
  202. int nCommPort, //串口号
  203. int nAddr, //地址
  204. int nVer, //版本号
  205. int nCid1, //控制标识码
  206. int nCid2, //命令信息
  207. WORD wLenId, //INFO字节长度
  208. int nCmdID, //命令ID
  209. int nDataLen, //请求数据长度
  210. int nCmdPos, //变量索引
  211. int nCmdLen, //变量长度
  212. char chMsg[80], //读到的变量值
  213. char *byDataFlag) //保留未用
  214. {
  215. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  216. if( nCommIndex == -1 ) return ERR_CODE_DAIKIN_COM_FAULT;
  217. CCommAsyn *pComm = NULL;
  218. pComm = g_vtComStruct[nCommIndex].pComm;
  219. return pComm->WorkMain(nAddr,
  220. nVer,
  221. nCid1,
  222. nCid2,
  223. wLenId,
  224. nCmdLen,
  225. nDataLen,
  226. nCmdPos,
  227. nCmdLen,
  228. chMsg,
  229. byDataFlag);
  230. }
  231. int GetCommIndex( int nCommPort, int nAddr )
  232. {
  233. for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
  234. {
  235. if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
  236. {
  237. return i;
  238. }
  239. }
  240. return -1;
  241. }