fad.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "StdAfx.h"
  2. #include ".\fad.h"
  3. other_fad::other_fad(char *szPath,char *szIniName,int nCommPort, int nAddr,int nRate, int nDataBit,int nStopBit, int nParity,int nInterval)
  4. {
  5. #if IS_USE_READMSG_CS
  6. InitializeCriticalSection(&m_csReadMsg);
  7. #endif
  8. MTVERIFY(m_hSemComm = CreateEvent(NULL, TRUE, TRUE, 0));
  9. memset(m_sz01Msg, 0, sizeof(m_sz01Msg));
  10. memset(m_sz02Msg, 0, sizeof(m_sz02Msg));
  11. memset(m_sz10Msg, 0, sizeof(m_sz10Msg));
  12. memset(m_sz11Msg, 0, sizeof(m_sz11Msg));
  13. m_dwOnlineTick = 0;
  14. m_devOnline = TRUE;
  15. }
  16. other_fad::~other_fad()
  17. {
  18. #if IS_USE_READMSG_CS
  19. DeleteCriticalSection(& m_csReadMsg);
  20. #endif
  21. MTVERIFY(CloseHandle(m_hSemComm));
  22. CloseComm();
  23. }
  24. BOOL other_fad::OpenOtherComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  25. {
  26. BOOL bResult = FALSE;
  27. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  28. return bResult;
  29. }
  30. int other_fad::GetIniInfo(char *szPath,char *szIniName,char *szCmd,int &IniSendCMD, char *nType,int &nIndex,int &nLen)
  31. {
  32. CHAR szFile[MAX_PATH + 1] = "";
  33. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  34. TRACE("szFile =%s\n",szFile);
  35. IniSendCMD = GetPrivateProfileInt(szCmd, "SendCmd", 0, szFile); // 返回的字符串是以\0结束的;
  36. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  37. nType[strlen(nType)] = '\0';
  38. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  39. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  40. return 0;
  41. }
  42. int other_fad::SendReadRequest(
  43. char *szPath,
  44. char *szIniName,
  45. int nCommPort,
  46. int nAddr,
  47. char *szCmd,
  48. char *szMsg,
  49. int nReversed1,
  50. int nReversed2,
  51. int nReversed3,
  52. int nReversed4,
  53. int nReversed5,
  54. float fReversed1,
  55. float fReversed2,
  56. float fReversed3,
  57. char *szReversed1,
  58. char *szReversed2,
  59. char *szReversed3,
  60. char *szReversed4,
  61. char *szReversed5 )
  62. {
  63. CCommProcess *pComm = FindComm(nCommPort);
  64. if( pComm == NULL ) return -1;
  65. int nRet(-1);
  66. int nIndex(0), nLen(0), IniSendCMD(0);
  67. char szDataType[CMD_TYPE] = {0};
  68. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,szDataType,nIndex,nLen);
  69. if (
  70. ( strlen(m_sz01Msg) == 0 && (strcmp(szCmd, "cmd-1") == 0 )) ||
  71. ( strlen(m_sz02Msg) == 0 && (strcmp(szCmd, "cmd-2") == 0 )) ||
  72. ( strlen(m_sz10Msg) == 0 && (strcmp(szCmd, "cmd-3") == 0 )) ||
  73. ( strlen(m_sz11Msg) == 0 && (strcmp(szCmd, "cmd-4") == 0 )) ||
  74. strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  75. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 )
  76. {
  77. nRet = GetDeviceParam(nAddr ,pComm, IniSendCMD, szCmd );
  78. if( nRet != 0 )
  79. return nRet;
  80. }
  81. if( GetTickCount() - m_dwOnlineTick > 60 * 1000 && m_dwOnlineTick > 0 ) {
  82. m_devOnline = FALSE;
  83. }
  84. else if( GetTickCount() - m_dwOnlineTick < 60 * 1000 && m_dwOnlineTick > 0 ) {
  85. m_devOnline = TRUE;
  86. }
  87. if( m_devOnline == FALSE ) {
  88. return -1;
  89. }
  90. nRet = GetFAD_01Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType);
  91. nRet = GetFAD_02Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType);
  92. nRet = GetFAD_10Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType);
  93. nRet = GetFAD_11Msg(nAddr, szCmd, szMsg, nIndex, nLen, szDataType);
  94. //LOG4C((LOG_NOTICE, "%s=%s", szCmd, szMsg));
  95. return nRet;
  96. }
  97. // 发送写设置;
  98. int other_fad::SendSetReuest(
  99. char *szPath,
  100. char *szIniName,
  101. int nCommPort,
  102. int nAddr,
  103. char *szCmd,
  104. char *szMsg,
  105. int nReversed1,
  106. int nReversed2,
  107. int nReversed3,
  108. int nReversed4,
  109. int nReversed5,
  110. float fReversed1,
  111. float fReversed2,
  112. float fReversed3,
  113. char *szReversed1,
  114. char *szReversed2,
  115. char *szReversed3,
  116. char *szReversed4,
  117. char *szReversed5
  118. )
  119. {
  120. return 0;
  121. }
  122. int other_fad::GetDeviceParam(int naddr, CCommProcess *pComm,const int &IniSendCMD,char *szCmd)
  123. {
  124. int nRet(-1);
  125. nRet = Send_ReadDeviceData(naddr, pComm, IniSendCMD);
  126. if( nRet != 0 ) {
  127. return nRet;
  128. }
  129. nRet = Recv_ReadDeviceData(naddr, pComm, szCmd);
  130. return nRet;
  131. }
  132. int other_fad::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,const int &IniSendCMD)
  133. {
  134. #if DEBUG_FADCTRL
  135. int nSLen = sizeof(SEND_DEVICEDATA);
  136. SEND_DEVICEDATA SendDeviceData;
  137. memset(&SendDeviceData, 0, sizeof(SEND_DEVICEDATA));
  138. SendDeviceData.Start = 0xFF;
  139. SendDeviceData.Addr = nAddr;
  140. SendDeviceData.Cid = IniSendCMD;
  141. memset(&SendDeviceData.Data,0,2);
  142. // 结束符;
  143. SendDeviceData.End = nAddr ^ IniSendCMD ^ 0 ^ 0;
  144. if (WaitForSingleObject(m_hSemComm,0) == WAIT_OBJECT_0 ) {
  145. ResetEvent(m_hSemComm);
  146. int nResult = pComm->Write((BYTE *)&SendDeviceData, nSLen);
  147. if (nResult != nSLen){
  148. SetEvent(m_hSemComm);
  149. return EER_CODE_COM_REGNUM;
  150. }
  151. }
  152. else{
  153. return ERR_CODE_COM_BUSY;
  154. }
  155. #endif
  156. return 0;
  157. }
  158. int other_fad::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
  159. {
  160. #if DEBUG_FADCTRL
  161. int nProcessLen(0),nReadLen(0);
  162. char *pBuff = new char[MAX_RECE_MSG];
  163. memset(pBuff, 0 , MAX_RECE_MSG);
  164. nReadLen = pComm->Read((BYTE *)pBuff,MAX_RECE_MSG);
  165. if (nReadLen <= 0){
  166. SetEvent(m_hSemComm);
  167. if (pBuff != NULL) {
  168. delete [] pBuff;
  169. pBuff = NULL;
  170. }
  171. return ERR_CODE_COM_READ_NO_DATA;
  172. } //长度校验出错 ;
  173. CopyFAD_01_Msg(nAddr, szCmd, pBuff);
  174. CopyFAD_02_Msg(nAddr, szCmd, pBuff);
  175. CopyFAD_10_Msg(nAddr, szCmd, pBuff);
  176. CopyFAD_11_Msg(nAddr, szCmd, pBuff);
  177. m_dwOnlineTick = GetTickCount();
  178. SetEvent( m_hSemComm );
  179. if( pBuff != NULL) {
  180. delete[] pBuff;
  181. pBuff = NULL;
  182. }
  183. #else
  184. SimulationCommData();
  185. #endif
  186. return 0;
  187. }
  188. void other_fad::CopyFAD_01_Msg( int naddr, char *szCmd, char *pBuffer)
  189. {
  190. if ( strcmp(szCmd, "cmd-1") == 0 )
  191. {
  192. #if IS_USE_READMSG_CS
  193. EnterCriticalSection(&m_csReadMsg);
  194. #endif
  195. memcpy(m_sz01Msg, pBuffer, sizeof(m_sz01Msg));
  196. #if IS_USE_READMSG_CS
  197. LeaveCriticalSection(&m_csReadMsg);
  198. #endif
  199. }
  200. }
  201. void other_fad::CopyFAD_02_Msg( int naddr, char *szCmd, char *pBuffer)
  202. {
  203. if (strcmp(szCmd, "cmd-2") == 0 )
  204. {
  205. #if IS_USE_READMSG_CS
  206. EnterCriticalSection(&m_csReadMsg);
  207. #endif
  208. memcpy(m_sz02Msg, pBuffer, sizeof(m_sz02Msg));
  209. #if IS_USE_READMSG_CS
  210. LeaveCriticalSection(&m_csReadMsg);
  211. #endif
  212. }
  213. }
  214. void other_fad::CopyFAD_10_Msg( int naddr, char *szCmd, char *pBuffer)
  215. {
  216. if ( strcmp(szCmd, "cmd-3") == 0 )
  217. {
  218. #if IS_USE_READMSG_CS
  219. EnterCriticalSection(&m_csReadMsg);
  220. #endif
  221. memcpy(m_sz10Msg, pBuffer, sizeof(m_sz10Msg));
  222. #if IS_USE_READMSG_CS
  223. LeaveCriticalSection(&m_csReadMsg);
  224. #endif
  225. }
  226. }
  227. void other_fad::CopyFAD_11_Msg( int naddr, char *szCmd, char *pBuffer)
  228. {
  229. if ( strcmp(szCmd, "cmd-4") == 0 )
  230. {
  231. #if IS_USE_READMSG_CS
  232. EnterCriticalSection(&m_csReadMsg);
  233. #endif
  234. memcpy(m_sz11Msg, pBuffer, sizeof(m_sz11Msg));
  235. #if IS_USE_READMSG_CS
  236. LeaveCriticalSection(&m_csReadMsg);
  237. #endif
  238. }
  239. }
  240. int other_fad::GetFAD_01Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  241. {
  242. int nRet(0);
  243. if (strcmp(szCmd, "cmd-1") == 0 )
  244. {
  245. #if IS_USE_READMSG_CS
  246. EnterCriticalSection(&m_csReadMsg);
  247. #endif
  248. hl_to_hex(m_sz01Msg[3],m_sz01Msg[4],szMsg);
  249. #if IS_USE_READMSG_CS
  250. LeaveCriticalSection(&m_csReadMsg);
  251. #endif
  252. nRet = 0;
  253. }
  254. return nRet;
  255. }
  256. int other_fad::GetFAD_02Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  257. {
  258. int nRet(0);
  259. if (strcmp(szCmd, "cmd-2") == 0 )
  260. {
  261. #if IS_USE_READMSG_CS
  262. EnterCriticalSection(&m_csReadMsg);
  263. #endif
  264. hl_to_hex(m_sz02Msg[3],m_sz02Msg[4],szMsg);
  265. #if IS_USE_READMSG_CS
  266. LeaveCriticalSection(&m_csReadMsg);
  267. #endif
  268. nRet = 0;
  269. }
  270. return nRet;
  271. }
  272. int other_fad::GetFAD_10Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  273. {
  274. int nRet(0);
  275. if (strcmp(szCmd, "cmd-3") == 0 )
  276. {
  277. #if IS_USE_READMSG_CS
  278. EnterCriticalSection(&m_csReadMsg);
  279. #endif
  280. hl_to_hex(m_sz10Msg[3],m_sz10Msg[4],szMsg);
  281. #if IS_USE_READMSG_CS
  282. LeaveCriticalSection(&m_csReadMsg);
  283. #endif
  284. nRet = 0;
  285. }
  286. return nRet;
  287. }
  288. int other_fad::GetFAD_11Msg(int nAddr, char *szCmd, char *szMsg, int &nIndex, int &nLen, char *szType )
  289. {
  290. int nRet(0);
  291. if (strcmp(szCmd, "cmd-4") == 0 )
  292. {
  293. #if IS_USE_READMSG_CS
  294. EnterCriticalSection(&m_csReadMsg);
  295. #endif
  296. hl_to_hex(m_sz11Msg[3],m_sz11Msg[4],szMsg);
  297. #if IS_USE_READMSG_CS
  298. LeaveCriticalSection(&m_csReadMsg);
  299. #endif
  300. nRet = 0;
  301. }
  302. return nRet;
  303. }
  304. void other_fad::SimulationCommData(void)
  305. {
  306. #if 0
  307. m_sz10Msg[0] = 0xFF;
  308. m_sz10Msg[1] = 0x01;
  309. m_sz10Msg[2] = 0x10;
  310. m_sz10Msg[3] = 0x01;
  311. m_sz10Msg[4] = 0x1A;
  312. #endif
  313. }