SK6000.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: SK6000.cpp //////
  4. ////// 作 者: Suguobing //////
  5. ////// 创建时间: //////
  6. ////// 说 明: 氰气检测协议 //////
  7. ////// //////
  8. ////// 修改时间:2010-11-27 //////
  9. ////// 修改说明: //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include "stdafx.h"
  13. #include "CommProcess.h"
  14. #include <math.h>
  15. #include "SK6000.h"
  16. CSk6000::CSk6000( char szPath[MAX_PATH],
  17. char szIniName[MAX_PATH],
  18. int nCommPort, int nAddr,
  19. int nRate, int nDataBit,
  20. int nStopBit, int nParity,
  21. int nInterval )
  22. {
  23. #if IS_USE_READMSG_CS
  24. InitializeCriticalSection(&m_csReadMsg);
  25. #endif
  26. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  27. for( int i = 0; i < MAX_ADDR; i++ )
  28. {
  29. memset(m_szSk_Msg[i], 0, sizeof(m_szSk_Msg[i]));
  30. m_devOnline[i] = TRUE;
  31. m_dwOnlineTick[i] = 0;
  32. }
  33. }
  34. CSk6000::~CSk6000()
  35. {
  36. #if IS_USE_READMSG_CS
  37. DeleteCriticalSection(& m_csReadMsg);
  38. #endif
  39. MTVERIFY(CloseHandle(m_hSemComm));
  40. CloseComm();
  41. }
  42. BOOL CSk6000::SkOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  43. {
  44. BOOL bResult = FALSE;
  45. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  46. return bResult;
  47. }
  48. int CSk6000::SendReadRequest(
  49. char szPath[MAX_PATH],
  50. char szIniName[MAX_PATH],
  51. int nCommPort,
  52. int nAddr,
  53. char szCmd[MAX_CMD],
  54. char szMsg[VAR_MSG],
  55. int nReversed1,
  56. int nReversed2,
  57. int nReversed3,
  58. int nReversed4,
  59. int nReversed5,
  60. float fReversed1,
  61. float fReversed2,
  62. float fReversed3,
  63. char szReversed1[MAX_RESERVED1],
  64. char szReversed2[MAX_RESERVED2],
  65. char szReversed3[MAX_RESERVED3],
  66. char szReversed4[MAX_RESERVED4],
  67. char szReversed5[MAX_RESERVED5]
  68. )
  69. {
  70. //LOG4C((LOG_NOTICE, "进入SK6000"));
  71. int nIndex = 0, nLen = 0;
  72. char szSendMsg[SK_SEND_MSG] = {0};
  73. char szCid[SK_SEND_MSG] = {0};
  74. char szType[TYPE_LENGTH] = {0};
  75. int startBit = 0;
  76. int endBit = 0;
  77. GetSkFromIni(szPath, szIniName, szCmd, szCid, szType, nIndex, nLen, startBit, endBit);
  78. CCommProcess *pComm = FindComm(nCommPort);
  79. if( pComm == NULL ) return -1;
  80. int nRet = -1;
  81. if ( strlen(m_szSk_Msg[nAddr - 1]) == 0 &&
  82. (strcmp(szCmd, "cmd-1") == 0 ) ||
  83. strcmp(szCmd, "cmd-1") == 0 )//UPS工作状态及传送顺序
  84. {
  85. nRet = GetDeviceParam(nAddr, pComm, szSendMsg, szCmd, szMsg, nIndex, nLen, szType );
  86. if( nRet != 0 )
  87. return nRet;
  88. }
  89. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  90. {
  91. m_devOnline[nAddr - 1] = FALSE;
  92. }
  93. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  94. {
  95. m_devOnline[nAddr - 1] = TRUE;
  96. }
  97. if( m_devOnline[nAddr - 1] == FALSE )
  98. {
  99. return -1;
  100. }
  101. nRet = GetSkMsg(nAddr, szCmd, szMsg, nIndex, nLen, szType, startBit, endBit);
  102. return nRet;
  103. }
  104. int CSk6000::GetSkMsg(int nAddr, char szCmd[MAX_CMD], char szMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startBit, int endBit)
  105. {
  106. int nRet = 0;
  107. if (strcmp(szCmd, "cmd-1") == 0 )
  108. {
  109. #if IS_USE_READMSG_CS
  110. EnterCriticalSection(&m_csReadMsg);
  111. #endif
  112. GetData(szType, m_szSk_Msg[nAddr - 1] , szMsg);
  113. #if IS_USE_READMSG_CS
  114. LeaveCriticalSection(&m_csReadMsg);
  115. #endif
  116. nRet = 0;
  117. }
  118. return nRet;
  119. }
  120. int CSk6000::GetDeviceParam(
  121. int nAddr,
  122. CCommProcess *pComm, //串口对象指针
  123. char szSendMsg[SK_SEND_MSG], //发送Buffer
  124. char szCmd[MAX_CMD], // 命令
  125. char szMsg[VAR_MSG], // 接收Buffer
  126. int &nIndex, // 变量索引,针对接收Buffer而言
  127. int &nLen, // 变量长度
  128. char szType[TYPE_LENGTH]) // 变量数据类型
  129. {
  130. int nRet = -1;
  131. nRet = RequestStatus(nAddr, pComm, szSendMsg);
  132. if( nRet != 0 )
  133. {
  134. return nRet; // 串口忙
  135. }
  136. nRet = ResponseStatus(nAddr, pComm, szCmd, szMsg, nIndex, nLen, szType);
  137. return nRet;
  138. }
  139. int CSk6000::GetSkFromIni( char szPath[MAX_PATH], //服务器程序所在目录
  140. char szIniName[MAX_PATH], //配置文件名
  141. char szCmd[MAX_CMD], //命令
  142. char szSanTakSendMsg[MAX_CMD], //发送Buffer
  143. char nType[MAX_CMD],
  144. int &nIndex,
  145. int &nLen,
  146. int &startBit,
  147. int &endBit)
  148. {
  149. CHAR szFile[MAX_PATH + 1] = "";
  150. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  151. TRACE("szFile =%s\n",szFile);
  152. GetPrivateProfileString(szCmd, "SendCmd", "", szSanTakSendMsg, 10, szFile);
  153. szSanTakSendMsg[strlen(szSanTakSendMsg)] = 0x0D;
  154. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  155. nType[strlen(nType)] = '\0';
  156. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  157. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  158. return 0;
  159. }
  160. int CSk6000::RequestStatus(int nAddr, CCommProcess *pComm, char chSendMsg[SK_SEND_MSG] )
  161. {
  162. #if DEBUG_SK //开关量,用于测试模拟数据
  163. char SendMsg[1];
  164. SendMsg[0] = nAddr | 0x80;
  165. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 )
  166. {
  167. int nDatalen = 1 ;//(int)strlen(chSendMsg);
  168. ResetEvent(m_hSemComm);
  169. int nResult = pComm->Write((BYTE *)SendMsg, nDatalen);
  170. //LOG4C((LOG_NOTICE,"write"));
  171. if (nResult == nDatalen)
  172. {
  173. }
  174. else
  175. {
  176. SetEvent(m_hSemComm);
  177. return EER_CODE_SK_COM_WRITE_DATA;
  178. }
  179. }
  180. else
  181. {
  182. return ERR_CODE_SK_COM_BUSY; //串口忙
  183. }
  184. #endif
  185. return 0;
  186. }
  187. int CSk6000::ResponseStatus(
  188. int nAddr,
  189. CCommProcess *pComm,
  190. char szCmd[MAX_CMD],
  191. char szMsg[VAR_MSG],
  192. int &nIndex,
  193. int &nLen,
  194. char szType[TYPE_LENGTH]
  195. )
  196. {
  197. #if DEBUG_SK
  198. int nReceiveLen = 0;
  199. int nProcessLen = 0;
  200. int nReadLen = 0;
  201. RESPONSE_STRUCT strcutResponse;
  202. memset(&strcutResponse, 0, sizeof(RESPONSE_STRUCT));
  203. nReceiveLen = sizeof(RESPONSE_STRUCT);
  204. char *pBuff = new char[nReceiveLen];
  205. memset(pBuff, 0 , nReceiveLen);
  206. nReadLen = pComm->Read((BYTE *)pBuff,nReceiveLen);
  207. if (nReadLen <= 0)
  208. {
  209. SetEvent(m_hSemComm);
  210. if (pBuff != NULL)
  211. {
  212. delete [] pBuff;
  213. pBuff = NULL;
  214. }
  215. return ERR_CODE_SK_COM_READ_NO_DATA;
  216. }
  217. //校验
  218. if (LengthCheck(pBuff))
  219. {
  220. SetEvent(m_hSemComm);
  221. if (pBuff != NULL)
  222. {
  223. delete [] pBuff;
  224. pBuff = NULL;
  225. }
  226. return ERR_CODE_RTN_LCHKSUM_ERROR;
  227. }
  228. SetSkMsg(nAddr, szCmd, pBuff);
  229. m_dwOnlineTick[nAddr - 1] = GetTickCount();
  230. // 设置串口等待事件为有信号
  231. SetEvent( m_hSemComm );
  232. if( pBuff != NULL)
  233. {
  234. delete[] pBuff;
  235. pBuff = NULL;
  236. }
  237. #else
  238. SimulationCommData(nAddr);
  239. #endif
  240. return 0;
  241. }
  242. void CSk6000::SetSkMsg(int nAddr, char szCmd[MAX_CMD], char *pBuff)
  243. {
  244. if (strcmp(szCmd, "cmd-1") == 0 )
  245. {
  246. #if IS_USE_READMSG_CS
  247. EnterCriticalSection(&m_csReadMsg);
  248. #endif
  249. memcpy(m_szSk_Msg[nAddr - 1], pBuff, sizeof(m_szSk_Msg[nAddr - 1]));
  250. #if IS_USE_READMSG_CS
  251. LeaveCriticalSection(&m_csReadMsg);
  252. #endif
  253. }
  254. }
  255. void CSk6000::SimulationCommData(int nAddr)
  256. {
  257. m_szSk_Msg[nAddr - 1][0] = 0x01;
  258. m_szSk_Msg[nAddr - 1][1] = 0x07;
  259. m_szSk_Msg[nAddr - 1][2] = 0x68;
  260. m_szSk_Msg[nAddr - 1][3] = 0x70;
  261. }
  262. int CSk6000::SendSetReuest(
  263. char szPath[MAX_PATH], // 程序所在路径
  264. char szIniName[MAX_PATH], // 配置文件名称
  265. int nCommPort, // 串行端口
  266. int nAddr, // 设备地址
  267. char szCmd[MAX_CMD], // 请求命令
  268. char szMsg[VAR_MSG], // 响应的值
  269. int nReversed1, // 预留整形参数1接口
  270. int nReversed2, // 预留整形参数2接口
  271. int nReversed3, // 预留整形参数3接口
  272. int nReversed4, // 预留整形参数4接口
  273. int nReversed5, // 预留整形参数5接口
  274. float fReversed1, // 预留float参数1接口
  275. float fReversed2, // 预留float参数2接口
  276. float fReversed3, // 预留float参数3接口
  277. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  278. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  279. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  280. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  281. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  282. )
  283. {
  284. return 0;
  285. }
  286. int CSk6000::LengthCheck(char szMsg[])
  287. {
  288. char ch;
  289. ch = szMsg[0] + szMsg[1] + szMsg[2];
  290. //ch &= 0x7F;
  291. if (ch == szMsg[3])
  292. {
  293. return 0;
  294. }
  295. else
  296. return 1;
  297. }
  298. int CSk6000::GetData(char *szType, char *szMsg, char *szConvMsg)
  299. {
  300. int sum = 0;
  301. CString sTotal;
  302. itoa(szMsg[1],szConvMsg,2);
  303. sTotal = szConvMsg;
  304. memset(szConvMsg,0,10);
  305. itoa(szMsg[2],szConvMsg,2);
  306. sTotal += szConvMsg;
  307. char a;
  308. int Len = sTotal.GetLength();
  309. for (int i = 0; i < Len; i++)
  310. {
  311. a = sTotal.GetAt(i);
  312. sum += (a-48) * static_cast<int>(pow(2.0,Len-i-1));
  313. }
  314. sprintf(szConvMsg, "%d", sum);
  315. return 0;
  316. }