lds2011.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include "StdAfx.h"
  2. #include ".\lds2011.h"
  3. lds2011::lds2011(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) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  9. for( int i = 0; i < MAX_ADDR; i++ )
  10. {
  11. memset(m_szMsg[i], 0, sizeof(m_szMsg[i]));
  12. m_devOnline[i] = TRUE;
  13. m_dwOnlineTick[i] = 0;
  14. }
  15. }
  16. lds2011::~lds2011()
  17. {
  18. #if IS_USE_READMSG_CS
  19. DeleteCriticalSection( &m_csReadMsg );
  20. #endif
  21. MTVERIFY( CloseHandle( m_hSemComm ) );
  22. CloseComm();
  23. }
  24. BOOL lds2011::LDS2011_OpenComm(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 lds2011::GetIniInfo(char *szPath,char *szIniName,char *szCmd,char *IniSendCMD,int &IniSendlen,char *szDataType,int &nIndex,int &nLen, int &iSBit, int &iEBit)
  31. {
  32. CHAR szFile[MAX_PATH + 1] = "";
  33. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  34. IniSendlen = GetPrivateProfileString(szCmd, "SendCmd", "", IniSendCMD, 10, szFile); // 返回的字符串是以\0结束的;
  35. GetPrivateProfileString(szCmd, "type", "", szDataType, 10, szFile);
  36. szDataType[strlen(szDataType)] = '\0';
  37. nIndex = GetPrivateProfileInt(szCmd, "Index", 0, szFile);
  38. nLen = GetPrivateProfileInt(szCmd, "Len", 0, szFile);
  39. iSBit = GetPrivateProfileInt(szCmd, "iSBit", 0, szFile);//从配置文件中取值
  40. iEBit = GetPrivateProfileInt(szCmd, "iEBit", 0, szFile);
  41. return 0;
  42. }
  43. BYTE lds2011::GetCheckCode(const BYTE *pSendBuf, const int &ilen)
  44. {
  45. BYTE byLrc = 0;
  46. char pBuf[4];
  47. int nData = 0;
  48. for(int i=1; i<ilen; i+=2) //i初始为1,避开“开始标记”冒号
  49. {
  50. //每两个需要发送的ASCII码转化为一个十六进制数
  51. pBuf [0] = pSendBuf [i];
  52. pBuf [1] = pSendBuf [i+1];
  53. pBuf [2] = '\0';
  54. sscanf(pBuf,"%x",& nData);
  55. byLrc += nData;
  56. }
  57. byLrc = ~byLrc; //求补码
  58. byLrc ++;
  59. return byLrc;
  60. }
  61. // 发送读取设备参数请求
  62. int lds2011::SendReadRequest(
  63. char szPath[MAX_PATH], // 程序所在路径
  64. char szIniName[MAX_PATH], // 配置文件名称
  65. int nCommPort, // 串行端口
  66. int nAddr, // 设备地址
  67. char szCmd[MAX_CMD], // 请求命令
  68. char szMsg[VAR_MSG], // 响应的值
  69. int nReversed1, // 预留整形参数1接口
  70. int nReversed2, // 预留整形参数2接口
  71. int nReversed3, // 预留整形参数3接口
  72. int nReversed4, // 预留整形参数4接口
  73. int nReversed5, // 预留整形参数5接口
  74. float fReversed1, // 预留float参数1接口
  75. float fReversed2, // 预留float参数2接口
  76. float fReversed3, // 预留float参数3接口
  77. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  78. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  79. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  80. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  81. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  82. )
  83. {
  84. CCommProcess *pComm = FindComm(nCommPort);
  85. if( pComm == NULL ) return -1;
  86. int nRet = -1;
  87. int nIndex(0), nLen(0), IniSendlen(0),iSBit(0), iEBit(0);
  88. char IniSendCMD[MAX_CMD] = {0}, szDataType[CMD_TYPE] = {0};
  89. GetIniInfo(szPath,szIniName,szCmd,IniSendCMD,IniSendlen,szDataType,nIndex,nLen,iSBit,iEBit);
  90. if( nAddr < 1 )
  91. return -1;
  92. int iCmd = atoi(szCmd + 4);
  93. if( (strlen(m_szMsg[nAddr -1]) == 0 && (iCmd >= 1 && iCmd <= 4)) || iCmd == 1 )
  94. {
  95. nRet = GetDeviceParam( nAddr,pComm, szCmd, IniSendCMD, IniSendlen );
  96. if ( nRet != 0) return nRet;
  97. }
  98. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  99. m_devOnline[nAddr - 1] = FALSE;
  100. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  101. m_devOnline[nAddr - 1] = TRUE;
  102. if( m_devOnline[nAddr - 1] == FALSE )
  103. return -1;
  104. nRet = GetLDSVarMsg( nAddr, szCmd, szMsg, nIndex, nLen, szDataType,iSBit,iEBit);
  105. //LOG4C((LOG_NOTICE,"nRet = %d,cmd = %s,值 = %s",nRet,szCmd,szMsg));
  106. return nRet;
  107. }
  108. int lds2011::GetDeviceParam(int nAddr,CCommProcess *pComm,char *szCmd,char *IniSendCMD,const int &IniSendlen)
  109. {
  110. int nRet = -1;
  111. nRet = Send_ReadDeviceData(nAddr, pComm, szCmd, IniSendCMD, IniSendlen);
  112. if( nRet != 0 )
  113. return nRet;
  114. nRet = Recv_ReadDeviceData( nAddr, pComm, szCmd);
  115. return nRet;
  116. }
  117. int lds2011::SendSetReuest(
  118. char *szPath, // 程序所在路径
  119. char *szIniName, // 配置文件名称
  120. int nCommPort, // 串行端口
  121. int nAddr, // 设备地址
  122. char *szCmd, // 请求命令
  123. char *szMsg, // 响应的值
  124. int nReversed1, // 预留整形参数1接口
  125. int nReversed2, // 预留整形参数2接口
  126. int nReversed3, // 预留整形参数3接口
  127. int nReversed4, // 预留整形参数4接口
  128. int nReversed5, // 预留整形参数5接口
  129. float fReversed1, // 预留float参数1接口
  130. float fReversed2, // 预留float参数2接口
  131. float fReversed3, // 预留float参数3接口
  132. char *szReversed1, // 预留字符数组参数1接口
  133. char *szReversed2, // 预留字符数组参数2接口
  134. char *szReversed3, // 预留字符数组参数3接口
  135. char *szReversed4, // 预留字符数组参数4接口
  136. char *szReversed5 // 预留字符数组参数5接口
  137. )
  138. {
  139. return 0;
  140. }
  141. int lds2011::Send_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd, const char *IniSendCMD,const int &IniSendlen)
  142. {
  143. #if DEBUG_LDS2011
  144. int iSendLen = 0;
  145. BYTE byArySend[17] = {0};
  146. // [:];
  147. byArySend[0] = 0x3A;
  148. // 地址;
  149. byArySend[1] = ByteToAscii((nAddr>>4) & 0x0f);
  150. byArySend[2] = ByteToAscii(nAddr & 0x0f);
  151. // 功能码;
  152. byArySend[3] = 0x30;
  153. byArySend[4] = 0x34;
  154. // 寄存器地址高位;
  155. byArySend[5] = 0x30;
  156. byArySend[6] = 0x30;
  157. // 寄存器地址低位
  158. byArySend[7] = 0x30;
  159. byArySend[8] = 0x32;
  160. // 寄存器长度高低位;
  161. byArySend[9] = 0x30;
  162. byArySend[10] = 0x30;
  163. byArySend[11] = 0x30;
  164. byArySend[12] = 0x33;
  165. // 校验;
  166. BYTE byCrc = GetCheckCode(byArySend,13);
  167. byArySend[13] = ByteToAscii((byCrc>>4) & 0x0f);
  168. byArySend[14] = ByteToAscii(byCrc& 0x0f);
  169. // 结束;
  170. byArySend[15] = 0x0D;
  171. byArySend[16] = 0x0A;
  172. iSendLen = 17;
  173. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  174. {
  175. ResetEvent( m_hSemComm );
  176. int nResult = pComm->Write(byArySend, iSendLen);
  177. if( nResult != iSendLen )
  178. {
  179. SetEvent( m_hSemComm );
  180. return ERR_CODE_COM_FAULT;
  181. }
  182. }
  183. else
  184. {
  185. return ERR_CODE_COM_BUSY;
  186. }
  187. #endif
  188. return 0;
  189. }
  190. int lds2011::Recv_ReadDeviceData(int nAddr,CCommProcess *pComm,char *szCmd)
  191. {
  192. #if DEBUG_LDS2011
  193. BYTE *byAryRecv = new BYTE[23];
  194. ZeroMemory(byAryRecv, 23);
  195. int iRecvLen = pComm->Read(byAryRecv, 23);
  196. if( iRecvLen <= 0)
  197. {
  198. SetEvent( m_hSemComm );
  199. if( iRecvLen != NULL)
  200. {
  201. delete[] byAryRecv;
  202. byAryRecv = NULL;
  203. }
  204. return ERR_CODE_COM_READ_NO_DATA;
  205. }
  206. BYTE byCrc[3] = {0};
  207. byCrc[2] = GetCheckCode(byAryRecv,19);
  208. byCrc[0] = ByteToAscii((byCrc[2] >> 4) & 0x0f);
  209. byCrc[1] = ByteToAscii(byCrc[2] & 0x0f);
  210. if ( byCrc[0] != byAryRecv[19] || byCrc[1] != byAryRecv[20])
  211. {
  212. SetEvent( m_hSemComm );
  213. if( iRecvLen != NULL)
  214. {
  215. delete[] byAryRecv;
  216. byAryRecv = NULL;
  217. }
  218. return ERR_CODE_COM_READ_NO_DATA;
  219. }
  220. SetLDSVarMsg( nAddr,szCmd, (char*)byAryRecv);
  221. m_dwOnlineTick[nAddr -1] = GetTickCount();
  222. SetEvent( m_hSemComm );
  223. if( byAryRecv != NULL)
  224. {
  225. delete[] byAryRecv;
  226. byAryRecv = NULL;
  227. }
  228. #else
  229. SimulationCommData();
  230. #endif
  231. return 0;
  232. }
  233. void lds2011::SetLDSVarMsg( int nAddr,char *szCmd, char *pBuffer)
  234. {
  235. int iCmd = atoi(szCmd + 4);
  236. if ( iCmd >= 1 && iCmd <=4)
  237. {
  238. #if IS_USE_READMSG_CS
  239. EnterCriticalSection( &m_csReadMsg );
  240. #endif
  241. memcpy(m_szMsg[nAddr-1], pBuffer, sizeof(m_szMsg[nAddr-1]));
  242. #if IS_USE_READMSG_CS
  243. LeaveCriticalSection(&m_csReadMsg);
  244. #endif
  245. }
  246. }
  247. int lds2011::GetLDSVarMsg(int nAddr,char *szCmd, char *szRecvMsg,int &nIndex,int &nLen,char *szDataType,const int &iSBit, const int &iEBit)
  248. {
  249. int nRet = 0;
  250. int iCmd = atoi(szCmd + 4);
  251. if ( iCmd >= 1 && iCmd <=4)
  252. {
  253. #if IS_USE_READMSG_CS
  254. EnterCriticalSection( &m_csReadMsg );
  255. #endif
  256. if ( iCmd == 1 || iCmd == 2)
  257. {
  258. char Cable[3] ={0};
  259. memcpy(Cable, m_szMsg[nAddr - 1] + nIndex, nLen);
  260. int iSour = HexStr2Dec(Cable);
  261. //LOG4C((LOG_NOTICE,"%s,iSour = %d, nIndex = %d, nLen = %d, iSBit = %d, iEBit = %d",szCmd,iSour, nIndex, nLen, iSBit, iEBit));
  262. ONEBYTE_BIT(szRecvMsg,iSour,iSBit,iEBit);
  263. //GetWORDBit(szRecvMsg,iSour,iSBit,iEBit);
  264. }
  265. else
  266. {
  267. char Leak[5] ={0};
  268. memcpy(Leak, m_szMsg[nAddr - 1] + nIndex, nLen);
  269. itoa( HexStr2Dec(Leak),szRecvMsg,10 );
  270. }
  271. #if IS_USE_READMSG_CS
  272. LeaveCriticalSection(&m_csReadMsg);
  273. nRet = 0;
  274. #endif
  275. }
  276. return nRet;
  277. }
  278. void lds2011::SimulationCommData(void)
  279. {
  280. memcpy(m_szMsg,":0104061F0801030109EF..",sizeof(":010406120801030109EF.."));
  281. }