icpdas.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. delete pComm;
  118. pComm = NULL;
  119. return FALSE;
  120. }
  121. else
  122. {
  123. COM_STRUCT tagComStruct;
  124. tagComStruct.nAddr = nAddr;
  125. tagComStruct.nCommPort = nCommPort;
  126. tagComStruct.pComm = pComm;
  127. // 串口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
  128. g_vtComStruct.push_back( tagComStruct );
  129. }
  130. }
  131. return TRUE;
  132. }
  133. int ICPDAS_DLLRequestStatusData(int nCommPort, int nAddr, int nByteNum, char *pData, int ivarid)
  134. {
  135. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  136. if( nCommIndex == -1 ) return ERR_CODE_ICPDAS_COM_FAULT;
  137. CCommAsyn *pComm = NULL;
  138. pComm = g_vtComStruct[nCommIndex].pComm;
  139. nByteNum = pComm->m_pProtocol->m_structResponse.StatusStruct.RtnByteNum;
  140. if( nByteNum == 0) // 设备没有请求到数据
  141. {
  142. return ERR_CODE_ICPDAS_COM_READ_NO_DATA;
  143. }
  144. int i = 0;
  145. int nNum = nByteNum;
  146. while( i < nNum )
  147. {
  148. pData[i] = pComm->m_pProtocol->m_structResponse.StrRtnMsg[i];
  149. i++;
  150. }
  151. return 0;
  152. }
  153. int CommDataCollect(int nCommPort,
  154. int nAddr,
  155. BYTE Start,//起始位
  156. BYTE StartAddr[2],//起始地址
  157. int nRegNum,//读取的寄存器个数
  158. BYTE FuncCode[2],
  159. int nDataLen,
  160. BYTE byWithAddrFlag,
  161. char chMsg[80])
  162. {
  163. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  164. if( nCommIndex == -1 ) return ERR_CODE_ICPDAS_COM_FAULT;
  165. CCommAsyn *pComm = NULL;
  166. pComm = g_vtComStruct[nCommIndex].pComm;
  167. return pComm->WorkMain(nAddr, Start, StartAddr, nRegNum, FuncCode, nDataLen,byWithAddrFlag, chMsg);
  168. }
  169. int GetCommIndex( int nCommPort, int nAddr )
  170. {
  171. //BOOL bOpenFlag = FALSE;
  172. for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
  173. {
  174. if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
  175. {
  176. return i;
  177. // bOpenFlag = TRUE;
  178. }
  179. //else
  180. //{
  181. // if ((g_vtComStruct[i].nAddr == nAddr) && bOpenFlag)
  182. // return i;
  183. //}
  184. }
  185. return -1;
  186. }
  187. int ICPDAS_DLLWrCom(int nCommPort, int nAddr, char DataBuffer[80], int nDataLen)
  188. {
  189. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  190. if( nCommIndex == -1 ) return ERR_CODE_ICPDAS_COM_FAULT;
  191. CCommAsyn *pComm = NULL;
  192. pComm = g_vtComStruct[nCommIndex].pComm;
  193. int iResult = pComm->WriteCommand(DataBuffer, nDataLen);
  194. return iResult;
  195. }