Upskehua.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Upskehua.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "Upskehua.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. char szCmd[32],
  26. char chSendMsg[32],
  27. int nDataLen,
  28. char chMsg[UPS_KE_HUA_MAX_MSG]);
  29. int GetCommIndex( int nCommPort, int nAddr );
  30. // CMGEApp
  31. BEGIN_MESSAGE_MAP(CUpskehuaApp, CWinApp)
  32. END_MESSAGE_MAP()
  33. // CMGEApp 构造
  34. CUpskehuaApp::CUpskehuaApp()
  35. {
  36. // TODO: 在此处添加构造代码,
  37. // 将所有重要的初始化放置在 InitInstance 中
  38. }
  39. // 唯一的一个 CMGEApp 对象
  40. CUpskehuaApp theApp;
  41. // CMGEApp 初始化
  42. BOOL CUpskehuaApp::InitInstance()
  43. {
  44. CWinApp::InitInstance();
  45. return TRUE;
  46. }
  47. int CUpskehuaApp::ExitInstance()
  48. {
  49. // TODO: 在此添加专用代码和/或调用基类
  50. // 关闭已经打开的串口
  51. for( unsigned int i = 0; i < g_vtComStruct.size(); i++ )
  52. {
  53. if( g_vtComStruct[i].pComm )
  54. {
  55. delete g_vtComStruct[i].pComm;
  56. g_vtComStruct[i].pComm = NULL;
  57. }
  58. }
  59. return CWinApp::ExitInstance();
  60. }
  61. int UPSKEHUA_DLLInit(int nCommPort,
  62. int nAddr,
  63. char szCmd[32],
  64. char chSendMsg[32],
  65. int nDataLen,
  66. char chMsg[UPS_KE_HUA_MAX_MSG])
  67. {
  68. return CommDataCollect(nCommPort,
  69. nAddr,
  70. szCmd,
  71. chSendMsg,
  72. nDataLen,
  73. chMsg);
  74. }
  75. void UPSKEHUA_DLLUnInit()
  76. {
  77. // 关闭串口,放到退出Dll时去关闭 ExitInstance
  78. return;
  79. }
  80. //初始化串口
  81. BOOL UPSKEHUA_DLLInitCom( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  82. {
  83. if (InitComm( nAddr, nCommPort, nRate, nDataBit, nStopBit, nParity, nInterval ))
  84. {
  85. return TRUE;
  86. }
  87. else
  88. {
  89. return FALSE;
  90. }
  91. }
  92. BOOL InitComm( int nAddr, int nCommPort, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval )
  93. {
  94. CCommAsyn *pComm = NULL;
  95. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  96. if( nCommIndex == -1 )
  97. {
  98. pComm = new CCommRS232;
  99. PORTPARAM portParam;
  100. portParam.StartAddr = nAddr; //起止地址
  101. portParam.PortNo = nCommPort; //Com端口
  102. portParam.BaudRate = nRate; //波特率
  103. portParam.ByteSize = nDataBit; // Number of bits/byte, 4-8
  104. portParam.StopBits = nStopBit; /* 结束位 0,1,2 = 1, 1.5, 2 */
  105. portParam.Parity = nParity; /* 校验位 0-4=None,Odd,Even,Mark,Space */
  106. portParam.Interval = nInterval; //间隔时间
  107. if(!pComm->InitParam(&portParam))
  108. {
  109. delete pComm;
  110. pComm = NULL;
  111. return FALSE;
  112. }
  113. else
  114. {
  115. COM_STRUCT tagComStruct;
  116. tagComStruct.nAddr = nAddr;
  117. tagComStruct.nCommPort = nCommPort;
  118. tagComStruct.pComm = pComm;
  119. // 串口只打开一次且按顺序打开,不存在资源共享冲突,所以不用加保护
  120. g_vtComStruct.push_back( tagComStruct );
  121. }
  122. }
  123. return TRUE;
  124. }
  125. int CommDataCollect(int nCommPort,
  126. int nAddr,
  127. char szCmd[32],
  128. char chSendMsg[32],
  129. int nDataLen,
  130. char chMsg[UPS_KE_HUA_MAX_MSG])
  131. {
  132. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  133. if( nCommIndex == -1 ) return ERR_CODE_UPSKEHUA_COM_FAULT;
  134. CCommAsyn *pComm = NULL;
  135. pComm = g_vtComStruct[nCommIndex].pComm;
  136. return pComm->WorkMain(nCommPort, nAddr, szCmd, chSendMsg, nDataLen,chMsg);
  137. }
  138. int GetCommIndex( int nCommPort, int nAddr )
  139. {
  140. //BOOL bOpenFlag = FALSE;
  141. for( int i = 0; i < (int)g_vtComStruct.size(); i++ )
  142. {
  143. if( g_vtComStruct[i].nCommPort == nCommPort)// && g_vtComStruct[i].nAddr == nAddr)
  144. {
  145. if (g_vtComStruct[i].nAddr == nAddr)
  146. return i;
  147. // bOpenFlag = TRUE;
  148. }
  149. //else
  150. //{
  151. // if ((g_vtComStruct[i].nAddr == nAddr) && bOpenFlag)
  152. // return i;
  153. //}
  154. }
  155. return -1;
  156. }
  157. int UPSKEHUA_DLLWrCom(int nCommPort, int nAddr, char DataBuffer[80], int nDataLen)
  158. {
  159. int nCommIndex = GetCommIndex( nCommPort, nAddr );
  160. if( nCommIndex == -1 ) return ERR_CODE_UPSKEHUA_COM_FAULT;
  161. CCommAsyn *pComm = NULL;
  162. pComm = g_vtComStruct[nCommIndex].pComm;
  163. int iResult = pComm->WriteCommand(DataBuffer, nDataLen);
  164. return iResult;
  165. }