modbusasc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // MGE.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "modbusasc.h"
  5. #include "CommRS232.h"
  6. #include "Head.h"
  7. #include "winsock2.h"
  8. #include "ProtocolModbus.h"
  9. #include <stdlib.h>
  10. #include ".\modbusasc.h"
  11. #pragma comment(lib, "ws2_32.lib ")
  12. vector<__COM_STRUCT> g_vtComStruct;
  13. //vector<__BASIC_STRUCT> g_vtReadBasicInfo;
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #endif
  17. BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval );
  18. int CommDataCollect(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, char chMsg[80]);
  19. int GetCommIndex( int nCommPort, int nAddr );
  20. // CMGEApp
  21. BEGIN_MESSAGE_MAP(CModbusascApp, CWinApp)
  22. END_MESSAGE_MAP()
  23. // CMGEApp 构造
  24. CModbusascApp::CModbusascApp()
  25. {
  26. // TODO: 在此处添加构造代码,
  27. // 将所有重要的初始化放置在 InitInstance 中
  28. }
  29. // 唯一的一个 CMGEApp 对象
  30. CModbusascApp theApp;
  31. // CMGEApp 初始化
  32. BOOL CModbusascApp::InitInstance()
  33. {
  34. CWinApp::InitInstance();
  35. return TRUE;
  36. }
  37. int CModbusascApp::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. int ASC_DLLInit( int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, char chMsg[80] )
  52. {
  53. return CommDataCollect(nCommPort, nAddr, SetBasePara, nDataLen, chMsg);
  54. }
  55. void ASC_DLLUnInit()
  56. {
  57. // 关闭串口,放到退出Dll时去关闭 ExitInstance
  58. return;
  59. }
  60. //初始化串口
  61. BOOL ASC_DLLInitCom( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  62. {
  63. if (InitComm( nAddr, nCommPort, nRate, nDataBit, nStopBit, nParity, nInterval ))
  64. {
  65. //LOG4C((LOG_NOTICE, "ASC_DLLInitCom return true"));
  66. return TRUE;
  67. }
  68. else
  69. {
  70. //LOG4C((LOG_NOTICE, "ASC_DLLInitCom return false"));
  71. return FALSE;
  72. }
  73. }
  74. BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval )
  75. {
  76. CCommAsyn *pComm = NULL;
  77. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  78. if( nCommIndex == -1 )
  79. {
  80. pComm = new CCommRS232;
  81. PORTPARAM portParam;
  82. portParam.StartAddr = nAddr; //起止地址
  83. portParam.PortNo = nCommPort; //Com端口
  84. portParam.BaudRate = nRate; //波特率
  85. portParam.ByteSize = nDataBit; // Number of bits/byte, 4-8
  86. portParam.StopBits = nStopBit; /* 结束位 0,1,2 = 1, 1.5, 2 */
  87. portParam.Parity = nParity; /* 校验位 0-4=None,Odd,Even,Mark,Space */
  88. portParam.Interval = nInterval; //间隔时间
  89. if(!pComm->InitParam(&portParam))
  90. {
  91. delete pComm;
  92. pComm = NULL;
  93. return FALSE;
  94. }
  95. else
  96. {
  97. COM_STRUCT tagComStruct;
  98. tagComStruct.nAddr = nAddr;
  99. tagComStruct.nCommPort = nCommPort;
  100. tagComStruct.pComm = pComm;
  101. // 串口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
  102. g_vtComStruct.push_back( tagComStruct );
  103. }
  104. }
  105. return TRUE;
  106. }
  107. int ASC_DLLRequestStatusData(int nCommPort, int nAddr, int nByteNum, char *pData)
  108. {
  109. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  110. if( nCommIndex == -1 ) return ERR_CODE_MODBUS_ASC_COM_FAULT;
  111. CCommAsyn *pComm = NULL;
  112. pComm = g_vtComStruct[nCommIndex].pComm;
  113. char DataLen[2] = {0};
  114. DataLen[0] = pComm->m_pProtocol->m_structResponse.StatusStruct.RtnByteNum[0];
  115. DataLen[1] = pComm->m_pProtocol->m_structResponse.StatusStruct.RtnByteNum[1];
  116. nByteNum = atoi((const char*)DataLen);
  117. if( nByteNum == 0 ) // 设备没有请求到数据
  118. {
  119. return ERR_CODE_MODBUS_ASC_COM_READ_NO_DATA;
  120. }
  121. int i = 0;
  122. int nNum = 2 * nByteNum;
  123. while( i < nNum )
  124. {
  125. pData[i] = pComm->m_pProtocol->m_structResponse.StrRtnMsg[i];
  126. i++;
  127. }
  128. return 0;
  129. }
  130. int ASC_DLLRequestWrStatusData(int nCommPort, int nAddr, char *pData)
  131. {
  132. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  133. if( nCommIndex == -1 ) return ERR_CODE_MODBUS_ASC_COM_FAULT;
  134. CCommAsyn *pComm = NULL;
  135. pComm = g_vtComStruct[nCommIndex].pComm;
  136. char DataLen[4] = {0};
  137. DataLen[0] = pComm->m_pProtocol->m_RequestWrParam.RequestRegNum[0];
  138. DataLen[1] = pComm->m_pProtocol->m_RequestWrParam.RequestRegNum[1];
  139. DataLen[2] = pComm->m_pProtocol->m_RequestWrParam.RequestRegNum[2];
  140. DataLen[3] = pComm->m_pProtocol->m_RequestWrParam.RequestRegNum[3];
  141. int nByteNum = atoi((const char*)DataLen);
  142. if( nByteNum == 0 ) // 设备没有请求到数据
  143. {
  144. return ERR_CODE_MODBUS_ASC_COM_READ_NO_DATA;
  145. }
  146. int i = 0;
  147. int nNum = 4 * nByteNum;
  148. while( i < nNum )
  149. {
  150. pData[i] = pComm->m_pProtocol->m_RequestWrParam.Data[i];
  151. i++;
  152. }
  153. return 0;
  154. }
  155. int ASC_DLLWrCom(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, double Data, int nDataLen)
  156. {
  157. // int iResult = g_pComm-> WriteCommand(SetBasePara, Data);
  158. // if (iResult == 0)
  159. // return TRUE;
  160. // else
  161. // return FALSE;
  162. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  163. if( nCommIndex == -1 ) return ERR_CODE_MODBUS_ASC_COM_FAULT;
  164. CCommAsyn *pComm = NULL;
  165. pComm = g_vtComStruct[nCommIndex].pComm;
  166. int iResult = pComm->WriteCommand(SetBasePara, Data, nDataLen);
  167. return iResult;
  168. }
  169. void ASC_DLLDetectOnlineCallBack()
  170. {
  171. ;
  172. }
  173. int CommDataCollect(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, char chMsg[80])
  174. {
  175. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  176. if( nCommIndex == -1 ) return ERR_CODE_MODBUS_ASC_COM_FAULT;
  177. CCommAsyn *pComm = NULL;
  178. pComm = g_vtComStruct[nCommIndex].pComm;
  179. return pComm->WorkMain(SetBasePara, nDataLen, chMsg);
  180. }
  181. int GetCommIndex( int nCommPort, int nAddr )
  182. {
  183. for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
  184. {
  185. if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
  186. {
  187. return i;
  188. }
  189. }
  190. return -1;
  191. }