stulz.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 ".\stulz.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, int iCmdPos, int iCmdLen, char chMsg[10]);
  18. int GetCommIndex( int nCommPort, int nAddr );
  19. // CMGEApp
  20. BEGIN_MESSAGE_MAP(CStulzApp, CWinApp)
  21. END_MESSAGE_MAP()
  22. // CMGEApp 构造
  23. CStulzApp::CStulzApp()
  24. {
  25. // TODO: 在此处添加构造代码,
  26. // 将所有重要的初始化放置在 InitInstance 中
  27. }
  28. // 唯一的一个 CMGEApp 对象
  29. CStulzApp theApp;
  30. // CMGEApp 初始化
  31. BOOL CStulzApp::InitInstance()
  32. {
  33. CWinApp::InitInstance();
  34. return TRUE;
  35. }
  36. int CStulzApp::ExitInstance()
  37. {
  38. // TODO: 在此添加专用代码和/或调用基类
  39. // 关闭已经打开的串口
  40. for( unsigned int i = 0; i < g_vtComStruct.size(); i++ )
  41. {
  42. if( g_vtComStruct[i].pComm )
  43. {
  44. delete g_vtComStruct[i].pComm;
  45. g_vtComStruct[i].pComm = NULL;
  46. }
  47. }
  48. return CWinApp::ExitInstance();
  49. }
  50. int STULZ_DLLInit( int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, int iCmdPos, int iCmdLen, char chMsg[10] )
  51. {
  52. return CommDataCollect(nCommPort, nAddr, SetBasePara, nDataLen, iCmdPos, iCmdLen,chMsg);
  53. }
  54. void STULZ_DLLUnInit()
  55. {
  56. // 关闭串口,放到退出Dll时去关闭 ExitInstance
  57. return;
  58. }
  59. //初始化串口
  60. BOOL STULZ_DLLInitCom( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  61. {
  62. if (InitComm( nAddr, nCommPort, nRate, nDataBit, nStopBit, nParity, nInterval ))
  63. {
  64. return TRUE;
  65. }
  66. else
  67. {
  68. return FALSE;
  69. }
  70. }
  71. BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval )
  72. {
  73. CCommAsyn *pComm = NULL;
  74. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  75. if( nCommIndex == -1 )
  76. {
  77. pComm = new CCommRS232;
  78. PORTPARAM portParam;
  79. portParam.StartAddr = nAddr; //起止地址
  80. portParam.PortNo = nCommPort; //Com端口
  81. portParam.BaudRate = nRate; //波特率
  82. portParam.ByteSize = nDataBit; // Number of bits/byte, 4-8
  83. portParam.StopBits = nStopBit; /* 结束位 0,1,2 = 1, 1.5, 2 */
  84. portParam.Parity = nParity; /* 校验位 0-4=None,Odd,Even,Mark,Space */
  85. portParam.Interval = nInterval; //间隔时间
  86. if(!pComm->InitParam(&portParam))
  87. {
  88. delete pComm;
  89. pComm = NULL;
  90. return FALSE;
  91. }
  92. else
  93. {
  94. COM_STRUCT tagComStruct;
  95. tagComStruct.nAddr = nAddr;
  96. tagComStruct.nCommPort = nCommPort;
  97. tagComStruct.pComm = pComm;
  98. // 串口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
  99. g_vtComStruct.push_back( tagComStruct );
  100. }
  101. }
  102. return TRUE;
  103. }
  104. int STULZ_DLLRequestStatusData(int nCommPort, int nAddr, int nByteNum, char *pData)
  105. {
  106. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  107. if( nCommIndex == -1 ) return ERR_CODE_AIR_STULZ_COM_FAULT;
  108. CCommAsyn *pComm = NULL;
  109. pComm = g_vtComStruct[nCommIndex].pComm;
  110. nByteNum = pComm->m_pProtocol->m_structResponse.StatusStruct.RtnByteNum;
  111. if( nByteNum == 0 ) // 设备没有请求到数据
  112. {
  113. return ERR_CODE_AIR_STULZ_COM_READ_NO_DATA;
  114. }
  115. int i = 0;
  116. int nNum = nByteNum;
  117. while( i < nNum )
  118. {
  119. pData[i] = pComm->m_pProtocol->m_structResponse.StrRtnMsg[i];
  120. i++;
  121. }
  122. return 0;
  123. }
  124. int STULZ_DLLWrCom(int nCommPort, int nAddr, char DataBuffer[80], char ResDataBuffer[2])
  125. {
  126. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  127. if( nCommIndex == -1 ) return ERR_CODE_AIR_STULZ_COM_FAULT;
  128. CCommAsyn *pComm = NULL;
  129. pComm = g_vtComStruct[nCommIndex].pComm;
  130. int iResult = pComm->WriteCommand(DataBuffer, ResDataBuffer);
  131. return iResult;
  132. }
  133. int CommDataCollect(int nCommPort, int nAddr, SETBASEPARAM SetBasePara, int nDataLen, int iCmdPos, int iCmdLen, char chMsg[10])
  134. {
  135. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  136. if( nCommIndex == -1 ) return ERR_CODE_AIR_STULZ_COM_FAULT;
  137. CCommAsyn *pComm = NULL;
  138. pComm = g_vtComStruct[nCommIndex].pComm;
  139. return pComm->WorkMain(SetBasePara, nDataLen, iCmdPos, iCmdLen, chMsg);
  140. }
  141. int GetCommIndex( int nCommPort, int nAddr )
  142. {
  143. for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
  144. {
  145. if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
  146. {
  147. return i;
  148. }
  149. }
  150. return -1;
  151. }