SF6.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*************************************************
  2. /* Copyright (C), 2010-2011,StoneU. Co., Ltd.
  3. /* File name: SF6.h
  4. /* Author: Zero.t
  5. /* Version: Ver1.0.0.1
  6. /* Date: 2011-10-19
  7. /* Description: ##
  8. /* Others: RS485;
  9. /* Function List: ###
  10. /* History:
  11. 1.Date: ###
  12. Author: ###
  13. Modification: ###
  14. *************************************************/
  15. #include "StdAfx.h"
  16. #include "CommProcess.h"
  17. #include ".\sf6.h"
  18. CSF6::CSF6(
  19. char szPath[MAX_PATH],
  20. char szIniName[MAX_PATH],
  21. int nCommPort, int nAddr,
  22. int nRate, int nDataBit,
  23. int nStopBit, int nParity,
  24. int nInterval
  25. )
  26. {
  27. #if IS_USE_READMSG_CS
  28. InitializeCriticalSection(&m_csReadMsg);
  29. #endif
  30. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  31. memset(m_szMsg, 0, sizeof(m_szMsg));
  32. m_dwOnlineTick = 0;
  33. m_devOnline = TRUE;
  34. }
  35. CSF6::~CSF6()
  36. {
  37. #if IS_USE_READMSG_CS
  38. DeleteCriticalSection(& m_csReadMsg);
  39. #endif
  40. MTVERIFY(CloseHandle(m_hSemComm));
  41. CloseComm();
  42. }
  43. BOOL CSF6::SF6OpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  44. {
  45. //LOG4C((LOG_NOTICE,"SF6OpenComm COM%d",nCommPort));
  46. BOOL bResult = FALSE;
  47. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  48. return bResult;
  49. }
  50. int CSF6::SendReadRequest(
  51. char szPath[MAX_PATH],
  52. char szIniName[MAX_PATH],
  53. int nCommPort,
  54. int nAddr,
  55. char szCmd[MAX_CMD],
  56. char szMsg[VAR_MSG],
  57. int nReversed1,
  58. int nReversed2,
  59. int nReversed3,
  60. int nReversed4,
  61. int nReversed5,
  62. float fReversed1,
  63. float fReversed2,
  64. float fReversed3,
  65. char szReversed1[MAX_RESERVED1],
  66. char szReversed2[MAX_RESERVED2],
  67. char szReversed3[MAX_RESERVED3],
  68. char szReversed4[MAX_RESERVED4],
  69. char szReversed5[MAX_RESERVED5]
  70. )
  71. {
  72. int nIndex = 0, nLen = 0;
  73. char szType[CMD_TYPE] = {0};
  74. m_address = nAddr;
  75. GetParFromIni(szPath, szIniName, szCmd, szType, nIndex, nLen);
  76. CCommProcess *pComm = FindComm(nCommPort);
  77. if( pComm == NULL ) return -1;
  78. int nRet = -1;
  79. if(
  80. ( strlen((char*)m_szMsg) == 0 && (
  81. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  82. strcmp(szCmd,"cmd-4") == 0
  83. ))||
  84. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-3") == 0
  85. )
  86. {
  87. nRet = GetDeviceParam(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType );
  88. if( nRet != 0 )
  89. return nRet;
  90. }
  91. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 ){
  92. m_devOnline = FALSE;
  93. }
  94. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 ){
  95. m_devOnline = TRUE;
  96. }
  97. if( m_devOnline == FALSE ){
  98. return -1;
  99. }
  100. nRet = GetMsg(nAddr,szCmd, szMsg, nIndex, nLen, szType);
  101. //LOG4C((LOG_NOTICE,"szCmd = %s, szMsg = %s",szCmd, szMsg));
  102. return nRet;
  103. }
  104. int CSF6::GetDeviceParam(
  105. int nAddr,
  106. CCommProcess *pComm, // 串口对象指针
  107. char szCmd[MAX_CMD], // 命令
  108. char szMsg[VAR_MSG], // 接收Buffer
  109. int &nIndex, // 变量索引,针对接收Buffer而言
  110. int &nLen, // 变量长度
  111. char szType[CMD_TYPE] // 变量数据类型
  112. )
  113. {
  114. int nRet = -1;
  115. nRet = RequestStatus(nAddr, pComm);
  116. if( nRet != 0 ) {
  117. return nRet; // 串口忙
  118. }
  119. nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
  120. return nRet;
  121. }
  122. int CSF6::GetParFromIni(
  123. char szPath[MAX_PATH], //服务器程序所在目录
  124. char szIniName[MAX_PATH], //配置文件名
  125. char szCmd[MAX_CMD], //命令
  126. char nType[MAX_CMD],
  127. int &nIndex,
  128. int &nLen
  129. )
  130. {
  131. CHAR szFile[MAX_PATH + 1] = "";
  132. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  133. TRACE("szFile =%s\n",szFile);
  134. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  135. nType[strlen(nType)] = '\0';
  136. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  137. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  138. return 0;
  139. }
  140. int CSF6::RequestStatus(int nAddr, CCommProcess *pComm )
  141. {
  142. #if SF6_DEBUG //开关量,用于测试模拟数据
  143. REQUEST_STRUCT structResuest;
  144. int nLen = sizeof(REQUEST_STRUCT);
  145. memset(&structResuest,0,nLen);
  146. structResuest.Start = 0xF7;
  147. structResuest.Add = nAddr+30;
  148. structResuest.End = 0x0D;
  149. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ){
  150. int nDatalen = (int)sizeof(structResuest);
  151. ResetEvent(m_hSemComm);
  152. int nResult = pComm->Write((BYTE *)&structResuest, nDatalen);
  153. if (nResult == nDatalen){
  154. }
  155. else{
  156. SetEvent(m_hSemComm);
  157. return EER_CODE_SF6_COM_REGNUM;
  158. }
  159. }
  160. else{
  161. return ERR_CODE_SF6_COM_BUSY; //串口忙
  162. }
  163. #endif
  164. return 0;
  165. }
  166. int CSF6::ResponseStatus(
  167. int nAddr,
  168. CCommProcess *pComm,
  169. char szCmd[MAX_CMD],
  170. char szMsg[VAR_MSG],
  171. int &nIndex,
  172. int &nLen,
  173. char szType[CMD_TYPE]
  174. )
  175. {
  176. #if SF6_DEBUG
  177. int nReceiveLen = 0;
  178. int nProcessLen = 0;
  179. int nReadLen = 0;
  180. RESPONE_STRUCT strcutResponse;
  181. memset(&strcutResponse, 0, sizeof(RESPONE_STRUCT));
  182. nReceiveLen = sizeof(RESPONE_STRUCT);
  183. char *pBuff = new char[nReceiveLen];
  184. memset(pBuff, 0 , nReceiveLen);
  185. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  186. if (nReadLen <= 0){
  187. SetEvent(m_hSemComm);
  188. if (pBuff != NULL){
  189. delete [] pBuff;
  190. pBuff = NULL;
  191. }
  192. return ERR_CODE_SF6_COM_READ_NO_DATA;
  193. }
  194. SetMsg(szCmd, pBuff);
  195. m_dwOnlineTick = GetTickCount();
  196. SetEvent( m_hSemComm );
  197. if( pBuff != NULL){
  198. delete[] pBuff;
  199. pBuff = NULL;
  200. }
  201. #else
  202. SimulationCommData();
  203. #endif
  204. return 0;
  205. }
  206. void CSF6::GetTHData(char szCmd[MAX_CMD],char *szType, char *szConvMsg,int nIndex, int nlen)
  207. {
  208. DataConversion(szType, (char*)(m_szMsg + nIndex), szConvMsg, nlen, 0, 0);
  209. unsigned char tmp = szConvMsg[0];
  210. if (strcmp(szCmd,"cmd-1") == 0)
  211. {
  212. double iData = tmp*0.24 -8;
  213. sprintf(szConvMsg,"%lf",iData);
  214. }
  215. else if (strcmp(szCmd,"cmd-2") == 0)
  216. {
  217. sprintf(szConvMsg,"%d",tmp);
  218. }
  219. }
  220. void CSF6::GetSF6Data(char szCmd[MAX_CMD],char *szType,char *szConvMsg,int nIndex ,int nlen)
  221. {
  222. DataConversion(szType, (char*)(m_szMsg + nIndex), szConvMsg, nlen);
  223. unsigned char tmp = szConvMsg[0];
  224. if (strcmp(szCmd,"cmd-3") == 0)
  225. {
  226. double iData = tmp*0.2;
  227. sprintf(szConvMsg,"%lf",iData);
  228. }
  229. else if(strcmp(szCmd,"cmd-4") == 0)
  230. {
  231. if (szConvMsg[0] >= 10){
  232. sprintf(szConvMsg,"%d",1);
  233. }
  234. else
  235. sprintf(szConvMsg,"%d",0);
  236. }
  237. }
  238. int CSF6::GetMsg(int nAddr,char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[CMD_TYPE] )
  239. {
  240. int nRet = 0;
  241. if (
  242. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  243. strcmp(szCmd,"cmd-4") == 0
  244. ){
  245. #if IS_USE_READMSG_CS
  246. EnterCriticalSection(&m_csReadMsg);
  247. #endif
  248. switch (nAddr){
  249. case 0:
  250. case 1:
  251. case 16:
  252. case 17:
  253. GetTHData(szCmd,szType,szMsg,nIndex,nLen);
  254. break;
  255. default: // SF6 O2;
  256. GetSF6Data(szCmd,szType,szMsg,nIndex,nLen);
  257. break;
  258. }
  259. #if IS_USE_READMSG_CS
  260. LeaveCriticalSection(&m_csReadMsg);
  261. #endif
  262. nRet = 0;
  263. }
  264. return nRet;
  265. }
  266. void CSF6::SetMsg(char szCmd[MAX_CMD], char *pBuff)
  267. {
  268. if (
  269. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd,"cmd-2") == 0 || strcmp(szCmd,"cmd-3") == 0 ||
  270. strcmp(szCmd,"cmd-4") == 0 ){
  271. #if IS_USE_READMSG_CS
  272. EnterCriticalSection(&m_csReadMsg);
  273. #endif
  274. memcpy(m_szMsg, pBuff, sizeof(m_szMsg));
  275. #if IS_USE_READMSG_CS
  276. LeaveCriticalSection(&m_csReadMsg);
  277. #endif
  278. }
  279. }
  280. void CSF6::SimulationCommData(void)
  281. {
  282. #if 1
  283. m_szMsg[0] = 0xF6;
  284. m_szMsg[1] = 0x20;
  285. m_szMsg[2] = 0x6D;
  286. m_szMsg[3] = 0x00;
  287. #else
  288. m_szMsg[0] = 0xF6;
  289. m_szMsg[1] = 0x2E;
  290. m_szMsg[2] = 0x86;
  291. m_szMsg[3] = 0x2F;
  292. #endif
  293. }