modbusrtu.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // MGE.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "CommRS232.h"
  5. #include "Head.h"
  6. #include "winsock2.h"
  7. #include "ProtocolModbus.h"
  8. #include <stdlib.h>
  9. #include ".\modbusrtu.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, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval );
  17. int CommDataCollect(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, char chMsg[80]);
  18. int GetCommIndex( int nCommPort, int nAddr );
  19. // CMGEApp
  20. BEGIN_MESSAGE_MAP(CModbusrtuApp, CWinApp)
  21. END_MESSAGE_MAP()
  22. // CMGEApp 构造
  23. CModbusrtuApp::CModbusrtuApp()
  24. {
  25. // TODO: 在此处添加构造代码,
  26. // 将所有重要的初始化放置在 InitInstance 中
  27. }
  28. // 唯一的一个 CMGEApp 对象
  29. CModbusrtuApp theApp;
  30. // CMGEApp 初始化
  31. BOOL CModbusrtuApp::InitInstance()
  32. {
  33. CWinApp::InitInstance();
  34. LOG4C((LOG_NOTICE, "Enter InitInstance"));
  35. return TRUE;
  36. }
  37. int CModbusrtuApp::ExitInstance()
  38. {
  39. // TODO: 在此添加专用代码和/或调用基类
  40. // 关闭已经打开的串口
  41. for( unsigned int i = 0; i < g_vtComStruct.size(); i++ )
  42. {
  43. if( g_vtComStruct[i].pComm )
  44. {
  45. delete g_vtComStruct[i].pComm;
  46. g_vtComStruct[i].pComm = NULL;
  47. }
  48. }
  49. return CWinApp::ExitInstance();
  50. }
  51. /************************************************************************/
  52. /* 函数:RTU_DLLInit[4/25/2016 Home];
  53. /* 描述:对RTU设备发送采集数据指令;
  54. /* 参数:;
  55. /* [IN] nCommPort:串口号;
  56. /* [IN] nAddr:指定的设备地址值;
  57. /* [IN] SetBasePara:用于设置RTU设备的参数;
  58. /* [IN] nDataLen:数据长度;
  59. /* [IN] chMsg:请求的命令行;
  60. /* 返回:void;
  61. /* 注意:;
  62. /* 示例:;
  63. /*
  64. /* 修改:;
  65. /* 日期:;
  66. /* 内容:;
  67. /************************************************************************/
  68. int RTU_DLLInit(IN int nCommPort, IN int nAddr, IN SETBASEPARAM SetBasePara, IN int nDataLen, IN char chMsg[80] )
  69. {
  70. return CommDataCollect(nCommPort, nAddr, SetBasePara, nDataLen, chMsg);
  71. }
  72. void RTU_DLLUnInit()
  73. {
  74. // 关闭串口,放到退出Dll时去关闭 ExitInstance
  75. return;
  76. }
  77. /************************************************************************/
  78. /* 函数:RTU_DLLInitCom[4/25/2016 Home];
  79. /* 描述:初始化串口;
  80. /* 参数:;
  81. /* [IN] nAddr:指定设备地址值;
  82. /* [IN] nCommPort:串口号;
  83. /* [IN] nRate:波特率;
  84. /* [IN] nDataBit:数据位;
  85. /* [IN] nStopBit:停止位;
  86. /* [IN] nParity:校验位;
  87. /* [IN] nInterval:超时值;
  88. /* 返回:void;
  89. /* 注意:;
  90. /* 示例:;
  91. /*
  92. /* 修改:;
  93. /* 日期:;
  94. /* 内容:;
  95. /************************************************************************/
  96. BOOL RTU_DLLInitCom( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  97. {
  98. if (InitComm( nAddr, nCommPort, nRate, nDataBit, nStopBit, nParity, nInterval ))
  99. {
  100. LOG4C((LOG_NOTICE, "RTU_DLLInitCom return true"));
  101. return TRUE;
  102. }
  103. else
  104. {
  105. LOG4C((LOG_NOTICE, "RTU_DLLInitCom return false"));
  106. return FALSE;
  107. }
  108. }
  109. BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval )
  110. {
  111. CCommAsyn *pComm = NULL;
  112. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  113. if( nCommIndex == -1 )
  114. {
  115. pComm = new CCommRS232;
  116. PORTPARAM portParam;
  117. portParam.StartAddr = nAddr; //起止地址
  118. portParam.PortNo = nCommPort; //Com端口
  119. portParam.BaudRate = nRate; //波特率
  120. portParam.ByteSize = nDataBit; // Number of bits/byte, 4-8
  121. portParam.StopBits = nStopBit; /* 结束位 0,1,2 = 1, 1.5, 2 */
  122. portParam.Parity = nParity; /* 校验位 0-4=None,Odd,Even,Mark,Space */
  123. portParam.Interval = nInterval; //间隔时间
  124. if(!pComm->InitParam(&portParam))
  125. {
  126. LOG4C((LOG_NOTICE, "Comm%d Open lost, nAddr = %d, Rate = %d, DataBit = %d, StopBit = %d, Parity = %d",
  127. nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity));
  128. delete pComm;
  129. pComm = NULL;
  130. return FALSE;
  131. }
  132. else
  133. {
  134. COM_STRUCT tagComStruct;
  135. tagComStruct.nAddr = nAddr;
  136. tagComStruct.nCommPort = nCommPort;
  137. tagComStruct.pComm = pComm;
  138. // 串口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
  139. g_vtComStruct.push_back( tagComStruct );
  140. }
  141. }
  142. return TRUE;
  143. }
  144. int RTU_DLLRequestWrStatusData(int nCommPort, int nAddr, char *pData)
  145. {
  146. return 0;
  147. }
  148. int RTU_DLLWrite(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, double Data, int nDataLen)
  149. {
  150. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  151. if( nCommIndex == -1 ) return ERR_CODE_MODBUS_RTU_COM_FAULT;
  152. CCommAsyn *pComm = NULL;
  153. pComm = g_vtComStruct[nCommIndex].pComm;
  154. int iResult = pComm->WriteCommand(SetBasePara, Data, nDataLen);
  155. return iResult;
  156. }
  157. int CommDataCollect(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, char chMsg[80])
  158. {
  159. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  160. if( nCommIndex == -1 ) return ERR_CODE_MODBUS_RTU_COM_FAULT;
  161. CCommAsyn *pComm = NULL;
  162. pComm = g_vtComStruct[nCommIndex].pComm;
  163. return pComm->WorkMain(SetBasePara, nDataLen, chMsg);
  164. }
  165. int GetCommIndex( int nCommPort, int nAddr )
  166. {
  167. for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
  168. {
  169. if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
  170. {
  171. return i;
  172. }
  173. }
  174. return -1;
  175. }