Sda_ii.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: Sda_ii.cpp //////
  4. ////// 作 者: wangjianfeng //////
  5. ////// 创建时间: //////
  6. ////// 说 明: Sda_ii协议 //////
  7. ////// //////
  8. ////// 修改时间:2010-08-16 //////
  9. ////// 修改说明:新版本 //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #include "StdAfx.h"
  13. #include "CommProcess.h"
  14. #include "Global.h"
  15. #include ".\sda_ii.h"
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #define new DEBUG_NEW
  20. #endif
  21. CSda_ii::CSda_ii(
  22. char szPath[MAX_PATH], // 程序所在路径
  23. char szIniName[MAX_PATH], // 配置文件名称
  24. int nCommPort, // 串行端口
  25. int nAddr, // 设备地址
  26. int nRate, // 波特率
  27. int nDataBit, // 数据位
  28. int nStopBit, // 停止位
  29. int nParity, // 校验位
  30. int nInterval // 时间间隔
  31. )
  32. {
  33. #if IS_USE_READMSG_CS
  34. InitializeCriticalSection( &m_csReadMsg ); //初始化一个临界资源对象
  35. #endif
  36. MTVERIFY( m_hSemComm = CreateEvent( NULL, TRUE, TRUE, 0 ) ); //CreateEvent()创建或打开一个命名的或无名的事件对象
  37. for(int i = 0;i < MAX_ADDR;i++)
  38. {
  39. memset(m_sz4CMsg[i], 0, sizeof(m_sz4CMsg[i]));
  40. memset(m_sz52Msg[i], 0, sizeof(m_sz52Msg[i]));
  41. m_devOnline[i] = TRUE;
  42. m_dwOnlineTick[i] = 0;
  43. }
  44. }
  45. CSda_ii::~CSda_ii()
  46. {
  47. #if IS_USE_READMSG_CS
  48. DeleteCriticalSection( &m_csReadMsg );
  49. #endif
  50. MTVERIFY( CloseHandle( m_hSemComm ) );
  51. CloseComm();
  52. }
  53. BOOL CSda_ii::AirSdaOpenComm(int nCommPort, int nAddr, int nRate, int nDataBit, int nStopBit, int nParity, int nInterval)
  54. {
  55. BOOL bResult = FALSE;
  56. bResult = OpenComm( nCommPort, nAddr, nRate, nDataBit, nStopBit, nParity, nInterval );
  57. if (bResult == FALSE)
  58. {
  59. //LOG4C((LOG_NOTICE,"依米康OpenCommFail"));
  60. }
  61. return bResult;
  62. }
  63. //请求数据
  64. int CSda_ii::SendReadRequest(
  65. char szPath[MAX_PATH],
  66. char szIniName[MAX_PATH],
  67. int nCommPort,
  68. int nAddr,
  69. char szCmd[MAX_CMD],
  70. char szMsg[VAR_MSG],
  71. int nReversed1,
  72. int nReversed2,
  73. int nReversed3,
  74. int nReversed4,
  75. int nReversed5,
  76. float fReversed1,
  77. float fReversed2,
  78. float fReversed3,
  79. char szReversed1[MAX_RESERVED1],
  80. char szReversed2[MAX_RESERVED2],
  81. char szReversed3[MAX_RESERVED3],
  82. char szReversed4[MAX_RESERVED4],
  83. char szReversed5[MAX_RESERVED5]
  84. )
  85. {
  86. int nIndex = 0, nLen = 0,Funid = 0,startbit = 0,endbit = 0;
  87. char SendCmd[SEND_MSG] = {0};
  88. char szType[TYPE_LENGTH] = {0};
  89. GetULFromIni(szPath, szIniName, szCmd,SendCmd, szType, nIndex, nLen,Funid,startbit, endbit);
  90. CCommProcess *pComm = FindComm(nCommPort);
  91. if( pComm == NULL )
  92. {
  93. //LOG4C((LOG_NOTICE,"串口句柄无效"));
  94. return -1;
  95. }
  96. int nRet = -1;
  97. if (
  98. (strlen(m_sz4CMsg[nAddr-1]) ==0 &&
  99. (
  100. strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  101. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  102. strcmp(szCmd, "cmd-5") == 0 || strcmp(szCmd, "cmd-6") == 0 ||
  103. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  104. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  105. strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  106. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  107. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  108. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  109. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  110. strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 ||
  111. strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  112. strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 ||
  113. strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 ||
  114. strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 ||
  115. strcmp(szCmd, "cmd-31") == 0 || strcmp(szCmd, "cmd-32") == 0 ||
  116. strcmp(szCmd, "cmd-33") == 0 || strcmp(szCmd, "cmd-34") == 0 ||
  117. strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0
  118. ))
  119. ||
  120. (strlen(m_sz52Msg[nAddr-1]) == 0 &&
  121. (
  122. strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 ||
  123. strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 ||
  124. strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0
  125. ))
  126. ||
  127. strcmp(szCmd,"cmd-1") == 0 || strcmp(szCmd, "cmd-37") == 0 ||
  128. strcmp(szCmd, "cmd-38") == 0 || strcmp(szCmd, "cmd-39") == 0 ||
  129. strcmp(szCmd, "cmd-40") == 0 || strcmp(szCmd, "cmd-41") == 0 ||
  130. strcmp(szCmd, "cmd-42") == 0
  131. )
  132. {
  133. nRet = GetDeviceParam( nAddr,pComm, SendCmd, szCmd, szMsg, nIndex, nLen, szType,Funid );
  134. if( nRet != 0 )
  135. return nRet;
  136. }
  137. if( GetTickCount() - m_dwOnlineTick[nAddr - 1] > 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  138. {
  139. m_devOnline[nAddr - 1] = FALSE;
  140. }
  141. else if( GetTickCount() - m_dwOnlineTick[nAddr - 1] < 60 *1000 && m_dwOnlineTick[nAddr - 1] > 0 )
  142. {
  143. m_devOnline[nAddr - 1] = TRUE;
  144. }
  145. if( m_devOnline[nAddr - 1] == FALSE )
  146. {
  147. return -1;
  148. }
  149. nRet = Get4CVarMsg(szCmd, szMsg, nIndex, nLen, szType,startbit, endbit,nAddr);
  150. nRet = Get52VarMsg(szCmd, szMsg, nIndex, nLen, szType,nAddr);
  151. return nRet;
  152. }
  153. int CSda_ii::GetDeviceParam(
  154. int nAddr,
  155. CCommProcess *pComm, //串口对象指针
  156. char SendCmd[SEND_MSG], //发送Buffer
  157. char szCmd[MAX_CMD], // 命令
  158. char szRecvMsg[VAR_MSG], // 接收Buffer
  159. int &nIndex, // 变量索引,针对接收Buffer而言
  160. int &nLen, // 变量长度
  161. char szType[TYPE_LENGTH], // 变量数据类型
  162. int Funid
  163. )
  164. {
  165. int nRet = -1;
  166. nRet = RequestReadStatus(nAddr,pComm, SendCmd,Funid);
  167. if( nRet != 0 )
  168. {
  169. return nRet;
  170. }
  171. nRet = ResponseReadStatus(pComm, szCmd, SendCmd, nIndex, nLen, szType,nAddr);
  172. return nRet;
  173. }
  174. int CSda_ii::Get4CVarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH], int startbit, int endbit,int nAddr)
  175. {
  176. int nRet = 0;
  177. if(
  178. strcmp(szCmd, "cmd-1") == 0 ||
  179. strcmp(szCmd, "cmd-2") == 0
  180. //温湿度;
  181. )
  182. {
  183. #if IS_USE_READMSG_CS
  184. EnterCriticalSection( &m_csReadMsg );
  185. #endif
  186. memcpy(szRecvMsg, m_sz4CMsg[nAddr - 1] + nIndex ,nLen);
  187. GetDataDi(szRecvMsg);
  188. #if IS_USE_READMSG_CS
  189. LeaveCriticalSection(&m_csReadMsg);
  190. #endif
  191. nRet = 0;
  192. }
  193. if(
  194. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  195. strcmp(szCmd, "cmd-5") == 0 || strcmp(szCmd, "cmd-6") == 0 ||
  196. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  197. //以上状态值;
  198. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  199. strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  200. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  201. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  202. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  203. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  204. strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 ||
  205. strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  206. strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 ||
  207. strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 ||
  208. strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 ||
  209. strcmp(szCmd, "cmd-31") == 0 || strcmp(szCmd, "cmd-32") == 0 ||
  210. strcmp(szCmd, "cmd-33") == 0 || strcmp(szCmd, "cmd-34") == 0 ||
  211. strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0
  212. //9~36 告警值;
  213. )
  214. {
  215. #if IS_USE_READMSG_CS
  216. EnterCriticalSection( &m_csReadMsg );
  217. #endif
  218. memcpy(szRecvMsg, m_sz4CMsg[nAddr - 1] + nIndex ,nLen);//命令对应的需要的数据szRecvMsg;
  219. //GetDataDi(szRecvMsg);
  220. GetByteHex(szRecvMsg);
  221. int iStatus = ::atoi(szRecvMsg);
  222. /*LOG4C((LOG_NOTICE , "szCmd = %s, iStatus = %d, szRecvMsg = %s" ,szCmd ,iStatus, szRecvMsg));*/
  223. GetBitStatu(iStatus,szRecvMsg,startbit,endbit);
  224. LOG4C((LOG_NOTICE , "szCmd = %s, iStatus = %d, 位信息 = %s" ,szCmd ,iStatus, szRecvMsg));
  225. #if IS_USE_READMSG_CS
  226. LeaveCriticalSection(&m_csReadMsg);
  227. #endif
  228. nRet = 0;
  229. }
  230. return nRet;
  231. }
  232. int CSda_ii::Get52VarMsg(char szCmd[MAX_CMD], char szRecvMsg[VAR_MSG], int &nIndex, int &nLen, char szType[TYPE_LENGTH],int nAddr)
  233. {
  234. int nRet = 0;
  235. if(
  236. strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 ||
  237. strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 || //温度与湿度设定值;
  238. strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0
  239. )
  240. {
  241. #if IS_USE_READMSG_CS
  242. EnterCriticalSection( &m_csReadMsg );
  243. #endif
  244. memcpy(szRecvMsg, m_sz52Msg[nAddr - 1] + nIndex ,nLen);
  245. //LOG4C((LOG_NOTICE, "szRecvMsg = %s",szRecvMsg));
  246. GetDataDi(szRecvMsg);
  247. //LOG4C((LOG_NOTICE, "szRecvMsg = %s",szRecvMsg));
  248. #if IS_USE_READMSG_CS
  249. LeaveCriticalSection(&m_csReadMsg);
  250. #endif
  251. nRet = 0;
  252. }
  253. return nRet;
  254. }
  255. // 发送设置设备参数请求
  256. int CSda_ii::SendSetReuest(
  257. char szPath[MAX_PATH], // 程序所在路径
  258. char szIniName[MAX_PATH], // 配置文件名称
  259. int nCommPort, // 串行端口
  260. int nAddr, // 设备地址
  261. char szCmd[MAX_CMD], // 请求命令
  262. char szRecvMsg[VAR_MSG], // 输入的值
  263. int nReversed1, // 预留整形参数1接口
  264. int nReversed2, // 预留整形参数2接口
  265. int nReversed3, // 预留整形参数3接口
  266. int nReversed4, // 预留整形参数4接口
  267. int nReversed5, // 预留整形参数5接口
  268. float fReversed1, // 预留float参数1接口
  269. float fReversed2, // 预留float参数2接口
  270. float fReversed3, // 预留float参数3接口
  271. char szReversed1[MAX_RESERVED1], // 预留字符数组参数1接口
  272. char szReversed2[MAX_RESERVED2], // 预留字符数组参数2接口
  273. char szReversed3[MAX_RESERVED3], // 预留字符数组参数3接口
  274. char szReversed4[MAX_RESERVED4], // 预留字符数组参数4接口
  275. char szReversed5[MAX_RESERVED5] // 预留字符数组参数5接口
  276. )
  277. {
  278. int nIndex = 0, nLen = 0,Funid = 0 ,startbit = 0,endbit = 0;
  279. char SendCmd[SEND_MSG] = {0};
  280. char szType[TYPE_LENGTH] = {0};
  281. GetULFromIni(szPath, szIniName, szCmd,SendCmd, szType, nIndex, nLen,Funid,startbit ,endbit);
  282. CCommProcess *pComm = FindComm(nCommPort);
  283. if( pComm == NULL ) return -1;
  284. int nRet = -1;
  285. if (strcmp(szCmd,"cmd-49") == 0 || strcmp(szCmd, "cmd-50") == 0 )
  286. {
  287. nRet = SetDeviceParam( nAddr,pComm, SendCmd, szCmd, SendCmd, nIndex, nLen, szType,Funid );
  288. if( nRet != 0 ) return nRet;
  289. }
  290. return 0;
  291. }
  292. int CSda_ii::SetDeviceParam(
  293. int nAddr,
  294. CCommProcess *pComm, //串口对象指针
  295. char SendCmd[SEND_MSG], //发送Buffer
  296. char szCmd[MAX_CMD], // 命令
  297. char szRecvMsg[VAR_MSG], // 接收Buffer
  298. int &nIndex, // 变量索引,针对接收Buffer而言
  299. int &nLen, // 变量长度
  300. char szType[TYPE_LENGTH], // 变量数据类型
  301. int Funid)
  302. {
  303. int nRet = -1;
  304. nRet = RequestWriteStatus(nAddr,pComm, SendCmd,Funid);
  305. if( nRet != 0 )
  306. {
  307. return nRet;
  308. }
  309. nRet = ResponseWriteStatus(pComm, szCmd, SendCmd, nIndex, nLen, szType,nAddr);
  310. return nRet;
  311. }
  312. int CSda_ii:: RequestWriteStatus( int nAddr,CCommProcess *pComm,char SendCmd[SEND_MSG], int Funid)
  313. {
  314. #if DEBUG_SDA
  315. RESPONSE_4CSTRUCT RequestPara;
  316. int iLen = sizeof(RequestPara);
  317. memset( &RequestPara, 0, iLen );
  318. RequestPara.SOI = 0x02;
  319. if (strcmp(SendCmd,"4F") == 0 )
  320. {
  321. RequestPara.CMD = 0x4F;
  322. }
  323. else if (strcmp(SendCmd, "46") == 0)
  324. {
  325. RequestPara.CMD = 0x46;
  326. }
  327. RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f);
  328. RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f);
  329. RequestPara.EOI = 0x03;
  330. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  331. {
  332. int nDataLen = (int)sizeof(RequestPara);
  333. ResetEvent( m_hSemComm );
  334. int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
  335. Sleep(3000);
  336. if( nResult != nDataLen )
  337. {
  338. SetEvent( m_hSemComm );
  339. return ERR_CODE_Sda_ii_COM_INVALIDRES;
  340. }
  341. }
  342. else
  343. return ERR_CODE_Sda_ii_COM_BUSY;
  344. #endif
  345. return 0;
  346. }
  347. int CSda_ii::ResponseWriteStatus(
  348. CCommProcess *pComm,
  349. char szCmd[MAX_CMD],
  350. char SendCmd[VAR_MSG],
  351. int &nIndex,
  352. int &nLen,
  353. char szType[TYPE_LENGTH],
  354. int nAddr
  355. )
  356. {
  357. #if DEBUG_SDA
  358. int nReceiveLen = 6;
  359. char *pBuffer = new char[ nReceiveLen ];
  360. memset(pBuffer, 0, nReceiveLen);
  361. int nReadLen = 0;
  362. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  363. if( nReadLen <= 0)
  364. {
  365. SetEvent( m_hSemComm );
  366. if( pBuffer != NULL)
  367. {
  368. delete[] pBuffer;
  369. pBuffer = NULL;
  370. }
  371. return ERR_CODE_Sda_ii_COM_READ_NO_DATA;
  372. }
  373. m_dwOnlineTick[nAddr -1] = GetTickCount();
  374. SetEvent( m_hSemComm );
  375. if( pBuffer != NULL)
  376. {
  377. delete[] pBuffer;
  378. pBuffer = NULL;
  379. }
  380. #endif
  381. return 0;
  382. }
  383. int CSda_ii::ResponseReadStatus( CCommProcess *pComm,
  384. char szCmd[MAX_CMD],
  385. char SendCmd[VAR_MSG],
  386. int &nIndex,
  387. int &nLen,
  388. char szType[TYPE_LENGTH],
  389. int nAddr)
  390. {
  391. #if DEBUG_SDA
  392. int nReceiveLen = 0;
  393. if (stricmp(SendCmd,"4C") == 0)
  394. {
  395. nReceiveLen = 25;
  396. }
  397. else if (strcmp(SendCmd ,"52") == 0)
  398. {
  399. nReceiveLen = 13;
  400. }
  401. if (nReceiveLen == 0)
  402. {
  403. LOG4C((LOG_NOTICE, "nReceiveLen = %d ", nReceiveLen));
  404. return -1;
  405. }
  406. char *pBuffer = new char[ nReceiveLen ];
  407. memset(pBuffer, 0, nReceiveLen);
  408. int nReadLen = 0;
  409. nReadLen = pComm->Read((BYTE *)pBuffer, nReceiveLen);
  410. if ( !GetCheckSum(pBuffer, nReceiveLen -1 ))
  411. {
  412. //LOG4C((LOG_NOTICE, "ResponseReadStatus::请求%s校验码校验出错", szCmd));
  413. SetEvent(m_hSemComm);
  414. if (pBuffer !=NULL)
  415. {
  416. delete []pBuffer;
  417. pBuffer = NULL;
  418. }
  419. return ERR_CODE_Sda_ii_COM_CHKSUM_LOST; //校验码校验出错
  420. }
  421. if( nReadLen <= 0)
  422. {
  423. //LOG4C((LOG_NOTICE, "ResponseReadStatus::串口没有读到数据"));
  424. SetEvent( m_hSemComm );
  425. if( pBuffer != NULL)
  426. {
  427. delete []pBuffer;
  428. pBuffer = NULL;
  429. }
  430. return ERR_CODE_Sda_ii_COM_READ_NO_DATA;
  431. }
  432. Set4CVarMsg( szCmd, pBuffer,nAddr);
  433. Set52VarMsg( szCmd, pBuffer,nAddr);
  434. m_dwOnlineTick[nAddr -1] = GetTickCount();
  435. SetEvent( m_hSemComm );
  436. if( pBuffer != NULL)
  437. {
  438. delete []pBuffer;
  439. pBuffer = NULL;
  440. }
  441. #else
  442. SimulationCommData();
  443. #endif
  444. return 0;
  445. }
  446. int CSda_ii::RequestReadStatus(int nAddr,CCommProcess *pComm,char SendCmd[SEND_MSG], int Funid)
  447. {
  448. #if DEBUG_SDA
  449. if (stricmp(SendCmd,"4C") == 0)
  450. {
  451. RESPONSE_4CSTRUCT RequestPara;
  452. int iLen = sizeof(RequestPara);
  453. memset( &RequestPara, 0, iLen );
  454. RequestPara.SOI = 0x02;
  455. RequestPara.CMD = 0x4C;
  456. RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f);;
  457. RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f);
  458. RequestPara.EOI = 0x03;
  459. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  460. {
  461. int nDataLen = (int)sizeof(RequestPara);
  462. ResetEvent( m_hSemComm );
  463. int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
  464. if( nResult != nDataLen )
  465. {
  466. SetEvent( m_hSemComm );
  467. return ERR_CODE_Sda_ii_COM_INVALIDRES;
  468. }
  469. }
  470. else
  471. return ERR_CODE_Sda_ii_COM_BUSY;
  472. }
  473. else if (strcmp(SendCmd,"52") == 0)
  474. {
  475. RESPONSE_53STRUCT RequestPara;
  476. int iLen = sizeof(RequestPara);
  477. memset( &RequestPara, 0, iLen );
  478. RequestPara.SOI = 0x02;
  479. RequestPara.CMD = 0x52;
  480. RequestPara.ADR[0] = ByteToAscii((nAddr >> 4) & 0x0f);;
  481. RequestPara.ADR[1] = ByteToAscii(nAddr & 0x0f);
  482. RequestPara.INDEX[0] = ByteToAscii((Funid >> 4) & 0x0f);;
  483. RequestPara.INDEX[1] = ByteToAscii(Funid & 0x0f);
  484. RequestPara.EOI = 0x03;
  485. if( WaitForSingleObject( m_hSemComm, 0 ) == WAIT_OBJECT_0 ) // 有信号才写串口
  486. {
  487. int nDataLen = (int)sizeof(RequestPara);
  488. ResetEvent( m_hSemComm );
  489. int nResult = pComm->Write((BYTE *)&RequestPara, nDataLen);
  490. if( nResult != nDataLen )
  491. {
  492. SetEvent( m_hSemComm );
  493. return ERR_CODE_Sda_ii_COM_INVALIDRES;
  494. }
  495. }
  496. else
  497. {
  498. return ERR_CODE_Sda_ii_COM_BUSY;
  499. }
  500. }
  501. #endif
  502. return 0;
  503. }
  504. void CSda_ii::Set4CVarMsg(char szCmd[MAX_CMD], char *pBuff,int nAddr)
  505. {
  506. if(
  507. strcmp(szCmd, "cmd-1") == 0 || strcmp(szCmd, "cmd-2") == 0 ||
  508. strcmp(szCmd, "cmd-3") == 0 || strcmp(szCmd, "cmd-4") == 0 ||
  509. strcmp(szCmd, "cmd-5") == 0 || strcmp(szCmd, "cmd-6") == 0 ||
  510. strcmp(szCmd, "cmd-7") == 0 || strcmp(szCmd, "cmd-8") == 0 ||
  511. strcmp(szCmd, "cmd-9") == 0 || strcmp(szCmd, "cmd-10") == 0 ||
  512. strcmp(szCmd, "cmd-11") == 0 || strcmp(szCmd, "cmd-12") == 0 ||
  513. strcmp(szCmd, "cmd-13") == 0 || strcmp(szCmd, "cmd-14") == 0 ||
  514. strcmp(szCmd, "cmd-15") == 0 || strcmp(szCmd, "cmd-16") == 0 ||
  515. strcmp(szCmd, "cmd-17") == 0 || strcmp(szCmd, "cmd-18") == 0 ||
  516. strcmp(szCmd, "cmd-19") == 0 || strcmp(szCmd, "cmd-20") == 0 ||
  517. strcmp(szCmd, "cmd-21") == 0 || strcmp(szCmd, "cmd-22") == 0 ||
  518. strcmp(szCmd, "cmd-23") == 0 || strcmp(szCmd, "cmd-24") == 0 ||
  519. strcmp(szCmd, "cmd-25") == 0 || strcmp(szCmd, "cmd-26") == 0 ||
  520. strcmp(szCmd, "cmd-27") == 0 || strcmp(szCmd, "cmd-28") == 0 ||
  521. strcmp(szCmd, "cmd-29") == 0 || strcmp(szCmd, "cmd-30") == 0 ||
  522. strcmp(szCmd, "cmd-31") == 0 || strcmp(szCmd, "cmd-32") == 0 ||
  523. strcmp(szCmd, "cmd-33") == 0 || strcmp(szCmd, "cmd-34") == 0 ||
  524. strcmp(szCmd, "cmd-35") == 0 || strcmp(szCmd, "cmd-36") == 0
  525. )
  526. {
  527. #if IS_USE_READMSG_CS
  528. EnterCriticalSection( &m_csReadMsg );
  529. #endif
  530. memcpy(m_sz4CMsg[nAddr-1],pBuff,sizeof(m_sz4CMsg[nAddr-1]));
  531. #if IS_USE_READMSG_CS
  532. LeaveCriticalSection(&m_csReadMsg);
  533. #endif
  534. }
  535. }
  536. void CSda_ii::Set52VarMsg(char szCmd[MAX_CMD], char *pBuff,int nAddr)
  537. {
  538. if(
  539. strcmp(szCmd, "cmd-37") == 0 || strcmp(szCmd, "cmd-38") == 0 ||
  540. strcmp(szCmd, "cmd-39") == 0 || strcmp(szCmd, "cmd-40") == 0 ||
  541. strcmp(szCmd, "cmd-41") == 0 || strcmp(szCmd, "cmd-42") == 0
  542. )
  543. {
  544. #if IS_USE_READMSG_CS
  545. EnterCriticalSection( &m_csReadMsg );
  546. #endif
  547. memcpy(m_sz52Msg[nAddr-1],pBuff,sizeof(m_sz52Msg[nAddr-1]));
  548. #if IS_USE_READMSG_CS
  549. LeaveCriticalSection(&m_csReadMsg);
  550. #endif
  551. }
  552. }
  553. //获取校验码;
  554. BOOL CSda_ii::GetCheckSum(char *pBuf, int iLen)
  555. {
  556. WORD iSum = 0;
  557. char chDest[3] = {0};
  558. for(int i=1; i< iLen - 2 ; i++)//求和
  559. {
  560. iSum += pBuf[i];
  561. }
  562. char chTemp[3] = {0};
  563. chTemp[0] = pBuf[iLen-2];
  564. chTemp[1] = pBuf[iLen-1];
  565. WORD iCompliment = iSum;
  566. chDest[0] = ByteToAscii((iSum >> 4) & 0x0f);
  567. chDest[1] = ByteToAscii(iSum & 0x0f);
  568. if ( chDest[0] == chTemp[0] && chDest[1] == chTemp[1])
  569. {
  570. return TRUE;
  571. }
  572. return FALSE;
  573. }
  574. int CSda_ii::GetULFromIni(
  575. char szPath[MAX_PATH], //服务器程序所在目录
  576. char szIniName[MAX_PATH], //配置文件名
  577. char szCmd[MAX_CMD], //命令
  578. char SendCmd[MAX_CMD], //发送Buffer
  579. char nType[MAX_CMD],
  580. int &nIndex,
  581. int &nLen,
  582. int &Funid,
  583. int &startbit,
  584. int &endbit
  585. )
  586. {
  587. CHAR szFile[MAX_PATH + 1] = "";
  588. wsprintf(szFile, "%s\\config\\%s", szPath, szIniName);
  589. GetPrivateProfileString(szCmd, "SendCmd", "", SendCmd, 10, szFile);
  590. SendCmd[strlen(SendCmd)] = '\0';
  591. GetPrivateProfileString(szCmd, "type", "", nType, 10, szFile);
  592. nType[strlen(nType)] = '\0';
  593. Funid = GetPrivateProfileInt(szCmd, "Funid", 0, szFile);
  594. nIndex = GetPrivateProfileInt(szCmd, "index", 0, szFile);
  595. nLen = GetPrivateProfileInt(szCmd, "len", 0, szFile);
  596. startbit = GetPrivateProfileInt(szCmd, "startbit", 0 ,szFile);
  597. endbit = GetPrivateProfileInt(szCmd, "endbit", 0 ,szFile);
  598. return 0;
  599. }
  600. //获取bit位状态值 ;
  601. void CSda_ii::GetBitStatu(int iVaule,char *bytV,int startbit,int endbit)
  602. {
  603. BYTE bytValue;
  604. bytValue = iVaule;
  605. int ByteBit = 0;
  606. char tempbyte[9] = {0};
  607. if (startbit == endbit)
  608. {
  609. switch(startbit)
  610. {
  611. case 0:
  612. ByteBit = bytValue & 0x01;
  613. break;
  614. case 1:
  615. ByteBit = (bytValue & 0x02) / 0x02 ;
  616. break;
  617. case 2:
  618. ByteBit = (bytValue & 0x04) / 0x04 ;
  619. break;
  620. case 3:
  621. ByteBit = (bytValue & 0x08) / 0x08 ;
  622. break;
  623. case 4:
  624. ByteBit = (bytValue & 0x10) / 0x10 ;
  625. break;
  626. case 5:
  627. ByteBit = (bytValue & 0x20) / 0x20 ;
  628. break;
  629. case 6:
  630. ByteBit = (bytValue & 0x40) / 0x40 ;
  631. break;
  632. case 7:
  633. ByteBit = (bytValue & 0x80) / 0x80;
  634. break;
  635. }
  636. _snprintf(bytV,sizeof(bytV),"%d",ByteBit);
  637. }
  638. else
  639. {
  640. int tempbit = startbit;
  641. int TiLen = endbit - startbit;
  642. for (int i = 0; i < TiLen;i++)
  643. {
  644. tempbit = tempbit + i;
  645. switch(tempbit)
  646. {
  647. case 0:
  648. ByteBit = bytValue & 0x01;
  649. break;
  650. case 1:
  651. ByteBit = (bytValue & 0x02) / 0x02 ;
  652. break;
  653. case 2:
  654. ByteBit = (bytValue & 0x04) / 0x04 ;
  655. break;
  656. case 3:
  657. ByteBit = (bytValue & 0x08) / 0x08 ;
  658. break;
  659. case 4:
  660. ByteBit = (bytValue & 0x10) / 0x10 ;
  661. break;
  662. case 5:
  663. ByteBit = (bytValue & 0x20) / 0x20 ;
  664. break;
  665. case 6:
  666. ByteBit = (bytValue & 0x40) / 0x40 ;
  667. break;
  668. case 7:
  669. ByteBit = (bytValue & 0x80) / 0x80;
  670. break;
  671. }
  672. _snprintf(tempbyte+i,sizeof(tempbyte),"%d",ByteBit);
  673. }
  674. strcpy(bytV,tempbyte);
  675. //bytValue = tempbyte;
  676. }
  677. }
  678. //十六进制转换十进制
  679. void CSda_ii::GetDataDi(char *szMsg)
  680. {
  681. char chDest[2] = {0};
  682. chDest[0] = szMsg[0];
  683. chDest[1] = szMsg[1];
  684. //LOG4C((LOG_NOTICE, "szMsg = %s",szMsg));
  685. int iConvMsg = 0;
  686. int iszMsg_1 = 0 ,iszMsg_2 = 0;
  687. //16进制转换十进制
  688. if ((szMsg[2] >= 'A')&&(szMsg[2] <= 'F'))
  689. {
  690. iszMsg_1 = szMsg[2] - 'A' + 10;
  691. }
  692. else if ((szMsg[2] >= 'a')&&(szMsg[2] <= 'f'))
  693. {
  694. iszMsg_1 = szMsg[2] - 'a' + 10;
  695. }
  696. else if ((szMsg[2] >= '0')&&(szMsg[2] <= '9'))
  697. {
  698. iszMsg_1 =szMsg[2] - '0';
  699. }
  700. if ((szMsg[3] >= 'A')&&(szMsg[3] <= 'F'))
  701. {
  702. iszMsg_2 = szMsg[3] - 'A' + 10;
  703. }
  704. else if ((szMsg[3] >= 'a')&&(szMsg[3] <= 'f'))
  705. {
  706. iszMsg_2 = szMsg[3] - 'a' + 10;
  707. }
  708. else if ((szMsg[3] >= '0')&&(szMsg[3] <= '9'))
  709. {
  710. iszMsg_2 = szMsg[3] - '0';
  711. }
  712. iConvMsg = iszMsg_1*16 + iszMsg_2;
  713. itoa(iConvMsg,szMsg,10);
  714. //LOG4C((LOG_NOTICE , "十六进制转换十进制 iConvMsg = %d",iConvMsg));
  715. }
  716. //十六进制转换十进制
  717. void CSda_ii::GetByteHex(char *szMsg)
  718. {
  719. char chDest[2] = {0};
  720. chDest[0] = szMsg[0];
  721. chDest[1] = szMsg[1];
  722. //LOG4C((LOG_NOTICE, "szMsg = %s",szMsg));
  723. int iConvMsg = 0;
  724. int iszMsg_1 = 0 ,iszMsg_2 = 0;
  725. //16进制转换十进制
  726. if ((szMsg[0] >= 'A')&&(szMsg[0] <= 'F'))
  727. {
  728. iszMsg_1 = szMsg[0] - 'A' + 10;
  729. }
  730. else if ((szMsg[0] >= 'a')&&(szMsg[0] <= 'f'))
  731. {
  732. iszMsg_1 = szMsg[0] - 'a' + 10;
  733. }
  734. else if ((szMsg[0] >= '0')&&(szMsg[0] <= '9'))
  735. {
  736. iszMsg_1 =szMsg[0] - '0';
  737. }
  738. if ((szMsg[1] >= 'A')&&(szMsg[1] <= 'F'))
  739. {
  740. iszMsg_2 = szMsg[1] - 'A' + 10;
  741. }
  742. else if ((szMsg[1] >= 'a')&&(szMsg[1] <= 'f'))
  743. {
  744. iszMsg_2 = szMsg[1] - 'a' + 10;
  745. }
  746. else if ((szMsg[1] >= '0')&&(szMsg[1] <= '9'))
  747. {
  748. iszMsg_2 = szMsg[1] - '0';
  749. }
  750. iConvMsg = iszMsg_1*16 + iszMsg_2;
  751. itoa(iConvMsg,szMsg,10);
  752. //LOG4C((LOG_NOTICE , "十六进制转换十进制 iConvMsg = %d",iConvMsg));
  753. }
  754. void CSda_ii::SimulationCommData(void)
  755. {
  756. m_sz4CMsg[0][0]=0x02 ;
  757. m_sz4CMsg[0][1]=0x4c;
  758. m_sz4CMsg[0][2]=0x30;
  759. m_sz4CMsg[0][3]=0x31;
  760. m_sz4CMsg[0][4]=0x30;
  761. m_sz4CMsg[0][5]=0x30;
  762. m_sz4CMsg[0][6]=0x43;
  763. m_sz4CMsg[0][7]=0x33;
  764. m_sz4CMsg[0][8]=0x30;
  765. m_sz4CMsg[0][9]=0x30;
  766. m_sz4CMsg[0][10]=0x32;
  767. m_sz4CMsg[0][11]=0x31;
  768. //.状态;
  769. m_sz4CMsg[0][12]=0x30;
  770. m_sz4CMsg[0][13]=0x42;
  771. m_sz4CMsg[0][14]=0x30;
  772. m_sz4CMsg[0][15]=0x30;
  773. m_sz4CMsg[0][16]=0x30;
  774. m_sz4CMsg[0][17]=0x31;
  775. m_sz4CMsg[0][18]=0x30;
  776. m_sz4CMsg[0][19]=0x30;
  777. m_sz4CMsg[0][20]=0x30;
  778. m_sz4CMsg[0][21]=0x30;
  779. m_sz4CMsg[0][22]=0x33;
  780. m_sz4CMsg[0][23]=0x39;
  781. m_sz4CMsg[0][24]=0x03;
  782. }