jb1501a64p.cpp 26 KB

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