icpdas.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // MGE.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "icpdas.h"
  5. #include "CommRS232.h"
  6. #include "Head.h"
  7. #include "winsock2.h"
  8. #include "ProtocolModbus.h"
  9. #include <stdlib.h>
  10. #pragma comment(lib, "ws2_32.lib ")
  11. vector<__COM_STRUCT> g_vtComStruct;
  12. //vector<__BASIC_STRUCT> g_vtReadBasicInfo;
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #endif
  16. BOOL InitComm( int nAddr,
  17. int nCommPort,
  18. int nRate,
  19. int nDataBit,
  20. int nStopBit,
  21. int nParity,
  22. int nInterval );
  23. int CommDataCollect(int nCommPort,
  24. int nAddr,
  25. BYTE Start,//起始位
  26. BYTE StartAddr[2],//起始地址
  27. int nRegNum,//读取的寄存器个数
  28. BYTE FuncCode[2],
  29. int nDataLen,
  30. BYTE byWithAddrFlag,
  31. char chMsg[80]);
  32. int GetCommIndex( int nCommPort, int nAddr );
  33. // CMGEApp
  34. BEGIN_MESSAGE_MAP(CIcpdasApp, CWinApp)
  35. END_MESSAGE_MAP()
  36. // CMGEApp 构造
  37. CIcpdasApp::CIcpdasApp()
  38. {
  39. // TODO: 在此处添加构造代码,
  40. // 将所有重要的初始化放置在 InitInstance 中
  41. }
  42. // 唯一的一个 CMGEApp 对象
  43. CIcpdasApp theApp;
  44. // CMGEApp 初始化
  45. BOOL CIcpdasApp::InitInstance()
  46. {
  47. CWinApp::InitInstance();
  48. return TRUE;
  49. }
  50. int CIcpdasApp::ExitInstance()
  51. {
  52. // TODO: 在此添加专用代码和/或调用基类
  53. // 关闭已经打开的串口
  54. for( unsigned int i = 0; i < g_vtComStruct.size(); i++ )
  55. {
  56. if( g_vtComStruct[i].pComm )
  57. {
  58. delete g_vtComStruct[i].pComm;
  59. g_vtComStruct[i].pComm = NULL;
  60. }
  61. }
  62. return CWinApp::ExitInstance();
  63. }
  64. int ICPDAS_DLLInit( int nCommPort,
  65. int nAddr,
  66. BYTE Start,//起始位
  67. BYTE StartAddr[2],//起始地址
  68. int nRegNum,//读取的寄存器个数
  69. BYTE FuncCode[2],
  70. int nDataLen,
  71. BYTE byWithAddrFlag,
  72. char chMsg[80])
  73. {
  74. return CommDataCollect(nCommPort,
  75. nAddr,
  76. Start,
  77. StartAddr,
  78. nRegNum,
  79. FuncCode,
  80. nDataLen,
  81. byWithAddrFlag, chMsg);
  82. }
  83. void ICPDAS_DLLUnInit()
  84. {
  85. // 关闭串口,放到退出Dll时去关闭 ExitInstance
  86. return;
  87. }
  88. //初始化串口
  89. BOOL ICPDAS_DLLInitCom( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  90. {
  91. if (InitComm( nAddr, nCommPort, nRate, nDataBit, nStopBit, nParity, nInterval ))
  92. {
  93. return TRUE;
  94. }
  95. else
  96. {
  97. return FALSE;
  98. }
  99. }
  100. BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval )
  101. {
  102. CCommAsyn *pComm = NULL;
  103. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  104. if( nCommIndex == -1 )
  105. {
  106. pComm = new CCommRS232;
  107. PORTPARAM portParam;
  108. portParam.StartAddr = nAddr; //起止地址
  109. portParam.PortNo = nCommPort; //Com端口
  110. portParam.BaudRate = nRate; //波特率
  111. portParam.ByteSize = nDataBit; // Number of bits/byte, 4-8
  112. portParam.StopBits = nStopBit; /* 结束位 0,1,2 = 1, 1.5, 2 */
  113. portParam.Parity = nParity; /* 校验位 0-4=None,Odd,Even,Mark,Space */
  114. portParam.Interval = nInterval; //间隔时间
  115. if(!pComm->InitParam(&portParam))
  116. {
  117. LOG4C((LOG_NOTICE, "InitParam lost"));
  118. delete pComm;
  119. pComm = NULL;
  120. return FALSE;
  121. }
  122. else
  123. {
  124. COM_STRUCT tagComStruct;
  125. tagComStruct.nAddr = nAddr;
  126. tagComStruct.nCommPort = nCommPort;
  127. tagComStruct.pComm = pComm;
  128. // 串口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
  129. g_vtComStruct.push_back( tagComStruct );
  130. }
  131. }
  132. return TRUE;
  133. }
  134. int ICPDAS_DLLRequestStatusData(int nCommPort, int nAddr, int nByteNum, char *pData, int ivarid)
  135. {
  136. return 0;
  137. }
  138. int CommDataCollect(int nCommPort,
  139. int nAddr,
  140. BYTE Start,//起始位
  141. BYTE StartAddr[2],//起始地址
  142. int nRegNum,//读取的寄存器个数
  143. BYTE FuncCode[2],
  144. int nDataLen,
  145. BYTE byWithAddrFlag,
  146. char chMsg[80])
  147. {
  148. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  149. if( nCommIndex == -1 ) return ERR_CODE_ICPDAS_COM_FAULT;
  150. CCommAsyn *pComm = NULL;
  151. pComm = g_vtComStruct[nCommIndex].pComm;
  152. return pComm->WorkMain(nAddr, Start, StartAddr, nRegNum, FuncCode, nDataLen,byWithAddrFlag, chMsg);
  153. }
  154. int GetCommIndex( int nCommPort, int nAddr )
  155. {
  156. //BOOL bOpenFlag = FALSE;
  157. for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
  158. {
  159. if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
  160. {
  161. return i;
  162. // bOpenFlag = TRUE;
  163. }
  164. //else
  165. //{
  166. // if ((g_vtComStruct[i].nAddr == nAddr) && bOpenFlag)
  167. // return i;
  168. //}
  169. }
  170. return -1;
  171. }
  172. int ICPDAS_DLLWrCom(int nCommPort, int nAddr, char DataBuffer[80], int nDataLen)
  173. {
  174. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  175. if( nCommIndex == -1 ) return ERR_CODE_ICPDAS_COM_FAULT;
  176. CCommAsyn *pComm = NULL;
  177. pComm = g_vtComStruct[nCommIndex].pComm;
  178. int iResult = pComm->WriteCommand(DataBuffer, nDataLen);
  179. return iResult;
  180. }