stulz6000.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Stulz6000.cpp: implementation of the CStulz6000 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Stulz6000.h"
  6. #include "CommProcess.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. CStulz6000::CStulz6000(char *szPath,char *szIniName,int nCommPort,int nAddr,int nRate,int nDataBit,int nStopBit,int nParity,int nInterval)
  13. {
  14. #if IS_USE_READMSG_CS
  15. InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
  16. #endif
  17. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  18. for( int i = 0; i < MAX_ADDR; i++ )
  19. {
  20. memset(m_szStulz6000_12Msg[i], 0, sizeof(m_szStulz6000_12Msg[i]));
  21. m_devOnline[i] = TRUE;
  22. m_dwOnlineTick[i] = 0;
  23. }
  24. }
  25. CStulz6000::~CStulz6000()
  26. {
  27. #if IS_USE_READMSG_CS
  28. DeleteCriticalSection( &m_csReadMsg );
  29. #endif
  30. MTVERIFY( CloseHandle( m_hSemComm ) );
  31. CloseComm();
  32. }
  33. BOOL CStulz6000::Stulz6000OpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  34. {
  35. BOOL bResult = FALSE;
  36. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  37. return bResult;
  38. }
  39. int CStulz6000::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
  40. {
  41. CHAR szFile[MAX_PATH + 1] = "";
  42. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  43. IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  44. GetPrivateProfileString(szCmd, "Type", "", szDataType, 10, szFile);
  45. szDataType[strlen(szDataType)] = '\0';
  46. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  47. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  48. iSBit = GetPrivateProfileInt(szCmd, "StaBit", 0, szFile);//从配置文件中取值
  49. iEBit = GetPrivateProfileInt(szCmd, "EndBit", 0, szFile);
  50. return 0;
  51. }
  52. // 发送读取设备参数请求
  53. int CStulz6000::SendReadRequest(
  54. char *szPath,
  55. char *szIniName,
  56. int nCommPort,
  57. int nAddr,
  58. char *szCmd,
  59. char *szMsg,
  60. int nReversed1,
  61. int nReversed2,
  62. int nReversed3,
  63. int nReversed4,
  64. int nReversed5,
  65. float fReversed1,
  66. float fReversed2,
  67. float fReversed3,
  68. char *szReversed1,
  69. char *szReversed2,
  70. char *szReversed3,
  71. char *szReversed4,
  72. char *szReversed5 )
  73. {
  74. CCommProcess *pComm = FindComm(nCommPort);
  75. if( pComm == NULL ) return -1;
  76. int nRet = -1;
  77. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  78. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  79. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  80. if( ( strlen(m_szStulz6000_12Msg[nAddr-1]) == 0 && strcmp(IniSendCMD,"12")==0 ) || strcmp(szCmd, "cmd-1") == 0)
  81. {
  82. nRet = GetDeviceParam(nAddr,pComm,szCmd,IniSendCMD,IniSendlen);
  83. if( nRet != 0 )
  84. return nRet;
  85. }
  86. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  87. m_devOnline[nAddr - 1] = FALSE;
  88. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  89. m_devOnline[nAddr - 1] = TRUE;
  90. if( m_devOnline[nAddr - 1] == FALSE )
  91. {
  92. LOG4C((LOG_NOTICE,"Stulz->SendReadRequest::返回超时"));
  93. return -1;
  94. }
  95. nRet = GetStulz6000_12VarMsg(nAddr,szCmd,IniSendCMD,szMsg,nIndex,nLen,szDataType,iSBit,iEBit);
  96. LOG4C((LOG_NOTICE,"Stulz-> Port = %d, Addr = %d, %s = %s",nCommPort, nAddr, szCmd, szMsg));
  97. return nRet;
  98. }
  99. int CStulz6000::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD, const int &IniSendlen)
  100. {
  101. int nRet = -1;
  102. nRet = Send_ReadDeviceData(nAddr,pComm,szCmd,IniSendCMD,IniSendlen);
  103. if( nRet != 0 )
  104. return nRet;
  105. nRet = Recv_ReadDeviceData(nAddr,pComm,szCmd,IniSendCMD);
  106. return nRet;
  107. }
  108. int CStulz6000::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen)
  109. {
  110. #if DEBUG_Stulz6000
  111. int nRet = -1;
  112. RESPONSE_STRUCT szSend = {0};
  113. //设备地址描述;
  114. szSend.Adr = nAddr;
  115. //cid2
  116. szSend.Cid2[0] = IniSendCMD[0] - '0';
  117. szSend.Cid2[1] = IniSendCMD[1] - '0';
  118. //chksum;
  119. szSend.Chksum[0] = ~(szSend.Adr + szSend.Cid2[0] + szSend.Cid2[1]) + 1;
  120. szSend.Chksum[1] = 255;
  121. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 )
  122. {
  123. int nDataLen = (int)sizeof(szSend);
  124. ResetEvent( m_hSemComm );
  125. int nResult = pComm->Write((unsigned char *)&szSend, nDataLen);
  126. if( nResult == nDataLen )
  127. {
  128. nRet = 0;
  129. }
  130. else
  131. {
  132. LOG4C((LOG_NOTICE,"Stulz->Send_ReadDeviceData::Wirte Error"));
  133. SetEvent( m_hSemComm );
  134. return ERR_CODE_COM_INVALIDRES;
  135. }
  136. }
  137. else
  138. {
  139. LOG4C((LOG_NOTICE,"Stulz->Send_ReadDeviceData::Thread busy"));
  140. return ERR_CODE_COM_BUSY;
  141. }
  142. return nRet;
  143. #endif
  144. return 0;
  145. }
  146. int CStulz6000::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd,char *szSendMsg)
  147. {
  148. #if DEBUG_Stulz6000
  149. int nReceiveLen = 0,nReadLen = 0;
  150. nReceiveLen = Stulz6000_RECEIVE_MSG;
  151. char *pBuffer = new char[ nReceiveLen ];
  152. memset(pBuffer, 0, nReceiveLen);
  153. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  154. if( nReadLen <= 0)
  155. {
  156. SetEvent( m_hSemComm );
  157. if( pBuffer != NULL)
  158. {
  159. delete[] pBuffer;
  160. pBuffer = NULL;
  161. }
  162. LOG4C((LOG_NOTICE,"Stulz->Recv_ReadDeviceData::No data to read"));
  163. return ERR_CODE_COM_READ_NO_DATA;
  164. }
  165. SetStulz6000_12VarMsg(nAddr, szCmd,szSendMsg,pBuffer);
  166. m_dwOnlineTick[nAddr - 1] = GetTickCount();
  167. // 设置串口等待事件为有信号;
  168. SetEvent( m_hSemComm );
  169. if( pBuffer != NULL)
  170. {
  171. delete[] pBuffer;
  172. pBuffer = NULL;
  173. }
  174. #else
  175. SimulationCommData(nAddr);
  176. #endif
  177. return 0;
  178. }
  179. void CStulz6000::SetStulz6000_12VarMsg( int nAddr, char *szCmd, char *szSendCMD, char *pBuffer)
  180. {
  181. if( strcmp(szSendCMD, "12") == 0 )
  182. {
  183. #if IS_USE_READMSG_CS
  184. EnterCriticalSection( &m_csReadMsg );
  185. #endif
  186. memcpy(m_szStulz6000_12Msg[nAddr - 1], pBuffer, sizeof(m_szStulz6000_12Msg[nAddr - 1]));
  187. #if IS_USE_READMSG_CS
  188. LeaveCriticalSection(&m_csReadMsg);
  189. #endif
  190. }
  191. }
  192. int CStulz6000::GetStulz6000_12VarMsg( int nAddr, char *szCmd, char *szSendCMD, char *szMsg,int &nIndex,int &nLen,char *szType,int startBit,int endBit )
  193. {
  194. int nRet = 0;
  195. if( strcmp(szSendCMD, "12") == 0 )
  196. {
  197. #if IS_USE_READMSG_CS
  198. EnterCriticalSection( &m_csReadMsg );
  199. #endif
  200. if ( strcmp(szType,"DWORD") == 0)
  201. {
  202. //WORD dvl = ReturnNASCIIWORD( (BYTE*)(m_szStulz6000_12Msg[nAddr - 1] + nIndex));
  203. sprintf(szMsg,"%d",ReturnNASCIIWORD((BYTE*)(m_szStulz6000_12Msg[nAddr - 1] + nIndex)));
  204. }
  205. else
  206. {
  207. DataConversion(szType, m_szStulz6000_12Msg[nAddr - 1] + nIndex, szMsg, nLen, startBit, endBit);
  208. }
  209. #if IS_USE_READMSG_CS
  210. LeaveCriticalSection(&m_csReadMsg);
  211. #endif
  212. nRet = 0;
  213. }
  214. return nRet;
  215. }
  216. // 发送设置设备参数请求
  217. int CStulz6000::SendSetReuest(
  218. char *szPath,
  219. char *szIniName,
  220. int nCommPort,
  221. int nAddr,
  222. char *szCmd,
  223. char *szSetMsg,
  224. int nReversed1,
  225. int nReversed2,
  226. int nReversed3,
  227. int nReversed4,
  228. int nReversed5,
  229. float fReversed1,
  230. float fReversed2,
  231. float fReversed3,
  232. char *szReversed1,
  233. char *szReversed2,
  234. char *szReversed3,
  235. char *szReversed4,
  236. char *szReversed5
  237. )
  238. {
  239. CCommProcess *pComm = FindComm(nCommPort);
  240. if( pComm == NULL ) return -1;
  241. int nRet = -1;
  242. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  243. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  244. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  245. char szRecvMsg[10] = {0};
  246. if( strcmp(IniSendCMD, "73") == 0 )
  247. {
  248. LOG4C((LOG_NOTICE,"Stulz->Control ON/OFF, %s",szSetMsg));
  249. SetDeviceParam(nAddr,pComm,szCmd,szSetMsg,szRecvMsg,IniSendCMD,IniSendlen);
  250. }
  251. return 0;
  252. }
  253. int CStulz6000::SetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *szSetMsg,char *szRecvMsg,char *IniSendCMD, const int &IniSendlen)
  254. {
  255. int nRet = -1;
  256. nRet = Send_WriteDeviceData(nAddr, pComm, szCmd, szSetMsg,IniSendCMD, IniSendlen );
  257. if( nRet != 0 )
  258. return nRet;
  259. nRet = Recv_WriteDeviceData(nAddr, pComm, szCmd, szRecvMsg);
  260. return nRet;
  261. }
  262. int CStulz6000::Send_WriteDeviceData(int naddr, CCommProcess *pComm, char *szCmd, char *szSetMsg, char *IniSendCMD, const int &IniSendlen )
  263. {
  264. #if DEBUG_Stulz6000
  265. REQUEST_STRUCT szSend = {0};
  266. //设备地址描述
  267. szSend.Adr = naddr;
  268. //cid2;
  269. szSend.Cid2[0] = IniSendCMD[0] - '0';
  270. szSend.Cid2[1] = IniSendCMD[1] - '0';
  271. //szInfo;
  272. szSend.szInfo = atoi(szSetMsg);
  273. //chksum;
  274. szSend.Chksum[0] = ~(szSend.Adr + szSend.Cid2[0] + szSend.Cid2[1] + szSend.szInfo) + 1;
  275. szSend.Chksum[1] = 255;
  276. int nDataLen = (int)sizeof(REQUEST_STRUCT);
  277. ResetEvent( m_hSemComm );
  278. int nResult = pComm->Write((unsigned char *)&szSend, nDataLen);
  279. if( nResult == nDataLen )
  280. {
  281. }
  282. else
  283. {
  284. SetEvent( m_hSemComm );
  285. LOG4C((LOG_NOTICE,"Stulz->Send_WriteDeviceData::Write Error"));
  286. return EER_CODE_COM_WRITE_DATA;
  287. }
  288. #endif
  289. return 0;
  290. }
  291. int CStulz6000::Recv_WriteDeviceData(int nAddr, CCommProcess *pComm, char *szCmd, char *szRecvMsg )
  292. {
  293. #if DEBUG_Stulz6000
  294. int nReceiveLen = 0;
  295. int nReadLen = 0;
  296. nReceiveLen = Stulz6000_RECEIVE_MSG;
  297. char *pBuffer = new char[ nReceiveLen ];
  298. memset(pBuffer, 0, nReceiveLen);
  299. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  300. if( nReadLen <= 0)
  301. {
  302. SetEvent( m_hSemComm );
  303. if( pBuffer != NULL)
  304. {
  305. delete[] pBuffer;
  306. pBuffer = NULL;
  307. }
  308. LOG4C((LOG_NOTICE,"Stulz->Recv_WriteDeviceData::No data to read"));
  309. return ERR_CODE_COM_READ_NO_DATA;
  310. }
  311. // 设置串口等待事件为有信号;
  312. SetEvent( m_hSemComm );
  313. if( pBuffer != NULL)
  314. {
  315. delete[] pBuffer;
  316. pBuffer = NULL;
  317. }
  318. #else
  319. SimulationCommData(nAddr);
  320. #endif
  321. return 0;
  322. }
  323. void CStulz6000::SimulationCommData(int nAddr)
  324. {
  325. //CString str = "01 01 89 FF FF E0 00 00 00 0A 02 00 00 FF FF FF FF 00 00 00 00 00 00 00 BA 52 78 48 78 48 78 48 78 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 73 32 0D 01 11 0C 01 23 23 05 05 2D EC 5A 5A 0A 05 07 07 00 05 01 07 03 17 01 06 0F 05 14 05 0A 05 0F 05 0A 05 0A 05 08 05 0B 07 04 05 01 14 05 19 05 0F 05 0F 0A 0A 05 0F 07 08 05 01 19 05 1E 05 14 05 14 0A 0A 05 13 07 0C 05 01 1E 05 23 05 19 05 19 0A 0A 05 0F 00 00 00 07 EF";
  326. //"";
  327. CString str = "";
  328. CString str1 = "01 01 89 ";
  329. CString str2 = "FF FF E0 00 00 00 0A 02 00 00 FF FF FF FF ";
  330. CString str3 = "00 00 "; // 温湿度设置偏移;
  331. CString str4 = "00 00 00 "; //预留3字节;
  332. CString str5 = "00 00 "; //模块压缩机2位状态
  333. CString str6 = "BA "; // 版本V1.86;
  334. CString str7 = "52 78 48 78 48 78 48 78 00 00 00 00 00 00 00 00 ";//模块输入输出状态;
  335. CString str8 = "00 00 00 00 00 00 00 73 32 ";
  336. CString str9 = "0D 01 11 0C 01 "; //年月日时分;
  337. CString strA = "23 23 05 05 2D EC 5A 5A 0A 05 "; // 温湿度报警
  338. CString strB = "07 07 00 05 01 07 03 17 01 06 0F 05 14 05 0A 05 0F 05 0A 05 "; // 模块1信息;
  339. CString strC = "0A 05 08 05 0B 07 04 05 01 14 05 19 05 0F 05 0F 0A 0A 05 0F 07 08 05 01 19 05 1E 05 14 05 14 0A 0A 05 13 07 0C 05 01 1E 05 23 05 19 05 19 0A 0A 05 ";//模块2~4信息;
  340. CString strD = "1F 00 AA 55 07 EF"; // 状态1/2 + 错误码1/2 + 校验1/2;
  341. str = str1 + str2 +str3 +str4 +str5 +str6 +str7 +str8 +str9 +strA+ strB + strC +strD;
  342. int nLen = str.GetLength();
  343. for ( int i = 0,j =0; i < nLen; i = i+3)
  344. {
  345. m_szStulz6000_12Msg[nAddr -1][j++] = ASCII_to_Byte(str.GetAt(i)) * 16 + ASCII_to_Byte(str.GetAt(i+1));
  346. }
  347. }