1. 程式人生 > 其它 >MODBUS RTU MASTER的C語言程式碼

MODBUS RTU MASTER的C語言程式碼

轉自:https://blog.csdn.net/chuanshaoke/article/details/7352602 [csharp]view plaincopy
  1. 這是我幾年前寫的一個關於MODBUSRTUMASTER的C語言程式碼,用於嵌入式控制器內。另一個觸控式螢幕MCGS_E作為MODBUSRTUSLAVE。
[csharp]view plaincopy
  1. 下面是相關程式碼,以前測試過,可以執行。由於目前的程式碼尚有很多地方值得改進,所以近期想把它改進重寫。高手們有什麼建議,請多多賜教。
[csharp]view plaincopy
  1. 若您找到網路上有共享的MODBUS的原始碼,請與我分享。非常感謝。
[csharp]view plaincopy
  1. 下面沒有main()函式,大家可以自己寫個,把ListenToTouch函式放進去就可以了。
[csharp]view plaincopy
  1. /*********************************************************************************/
  2. /*函式名稱:strmcpy()
  3. *輸入引數:共個引數;
  4. *輸出引數:共個引數;
  5. *返回值:
  6. *需儲存的引數:共個引數;
  7. *功能介紹:
  8. (1)字元陣列拷貝;
  9. *修改日誌:
  10. *[2006-3-617:07]Ver.1.00
  11. 開始編寫;
  12. 完成;
  13. /**/
  14. /*********************************************************************************/
  15. voidstrmcpy(unsignedchardest[],unsignedcharsrc[],intcount)
  16. {
  17. inti;
  18. for(i=0;i<count;i++)
  19. {
  20. dest[i]=src[i];
  21. }
  22. dest[i]='/0';
  23. }
  24. /*****************************************************************************/
  25. /*********************************************************************************/
  26. /*函式名稱:bitmcpy()
  27. *輸入引數:共個引數;
  28. *輸出引數:共個引數;
  29. *返回值:
  30. *需儲存的引數:共個引數;
  31. *功能介紹:
  32. (1)開關量陣列拷貝;
  33. *修改日誌:
  34. *[2006-3-714:59]Ver.1.00
  35. 開始編寫;
  36. 完成;
  37. /**/
  38. /*********************************************************************************/
  39. voidbitmcpy(intdest[],intsrc[],intcount)
  40. {
  41. inti;
  42. for(i=0;i<count;i++)
  43. {
  44. dest[i]=src[i];
  45. }
  46. }
  47. /*****************************************************************************/
  48. /*********************************************************************************/
  49. /*函式名稱:strmcmp()
  50. *輸入引數:共個引數;
  51. *輸出引數:共個引數;
  52. *返回值:
  53. *需儲存的引數:共個引數;
  54. *功能介紹:
  55. (1)字元陣列比較;相同則返回0,不相同則返回1;
  56. *修改日誌:
  57. *[2006-3-617:41]Ver.1.00
  58. 開始編寫;
  59. 完成;
  60. /**/
  61. /*********************************************************************************/
  62. intstrmcmp(unsignedcharstr1[],unsignedcharstr2[],intcount)
  63. {
  64. inti;
  65. for(i=0;i<count;i++)
  66. {
  67. if(str1[i]!=str2[i])
  68. {
  69. return1;
  70. }
  71. }
  72. return0;
  73. }
  74. /*****************************************************************************/
  75. /*********************************************************************************/
  76. /*函式名稱:Datamcmp()
  77. *輸入引數:共個引數;
  78. *輸出引數:共個引數;
  79. *返回值:
  80. *需儲存的引數:共個引數;
  81. *功能介紹:
  82. (1)浮點陣列比較;相同則返回0,不相同則返回1;
  83. *修改日誌:
  84. *[2006-3-618:05]Ver.1.00
  85. 開始編寫;
  86. 完成;
  87. *[2006-3-913:52]
  88. 加了一層括號,以前是錯誤的
  89. if(!((data1[i]-data2[i]<0.0001)&&(data1[i]-data2[i]>-0.0001)))
  90. /**/
  91. /*********************************************************************************/
  92. intDatamcmp(floatdata1[],floatdata2[],intcount)
  93. {
  94. inti;
  95. for(i=0;i<count;i++)
  96. {
  97. if(!((data1[i]-data2[i]<0.0001)&&(data1[i]-data2[i]>-0.0001)))
  98. {
  99. return1;
  100. }
  101. }
  102. return0;
  103. }
  104. /*****************************************************************************/
  105. /*********************************************************************************/
  106. /*函式名稱:Bitmcmp()
  107. *輸入引數:共個引數;
  108. *輸出引數:共個引數;
  109. *返回值:
  110. *需儲存的引數:共個引數;
  111. *功能介紹:
  112. (1)開關量陣列比較;相同則返回0,不相同則返回1;
  113. *修改日誌:
  114. *[2006-3-618:10]Ver.1.00
  115. 開始編寫;
  116. 完成;
  117. /**/
  118. /*********************************************************************************/
  119. intBitmcmp(intdata1[],intdata2[],intcount)
  120. {
  121. inti;
  122. for(i=0;i<count;i++)
  123. {
  124. if(data1[i]!=data2[i])
  125. {
  126. return1;
  127. }
  128. }
  129. return0;
  130. }
  131. /*****************************************************************************/
  132. /*********************************************************************************/
  133. /*函式名稱:GetCRC16()
  134. *輸入引數:共個引數;
  135. *輸出引數:共個引數;
  136. *返回值:
  137. *需儲存的引數:共個引數;
  138. *功能介紹:
  139. (1)CRC16校驗;返回校驗碼;
  140. *修改日誌:
  141. *[2005-11-2816:40]Ver.1.00
  142. 開始編寫;
  143. 完成;
  144. /**/
  145. /*********************************************************************************/
  146. unsignedshortGetCRC16(unsignedchar*puchMsg,unsignedshortusDataLen)
  147. {
  148. /*CRC高位位元組值表*/
  149. unsignedcharauchCRCHi[256]={
  150. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  151. 0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  152. 0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
  153. 0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
  154. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,
  155. 0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,
  156. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,
  157. 0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  158. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  159. 0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
  160. 0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,
  161. 0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
  162. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  163. 0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
  164. 0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
  165. 0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
  166. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  167. 0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  168. 0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
  169. 0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  170. 0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
  171. 0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
  172. 0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,
  173. 0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
  174. 0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
  175. 0x80,0x41,0x00,0xC1,0x81,0x40
  176. };
  177. unsignedcharauchCRCLo[256]={
  178. 0x00,0xC0,0xC1,0x01,0xC3,0x03,0x02,0xC2,0xC6,0x06,
  179. 0x07,0xC7,0x05,0xC5,0xC4,0x04,0xCC,0x0C,0x0D,0xCD,
  180. 0x0F,0xCF,0xCE,0x0E,0x0A,0xCA,0xCB,0x0B,0xC9,0x09,
  181. 0x08,0xC8,0xD8,0x18,0x19,0xD9,0x1B,0xDB,0xDA,0x1A,
  182. 0x1E,0xDE,0xDF,0x1F,0xDD,0x1D,0x1C,0xDC,0x14,0xD4,
  183. 0xD5,0x15,0xD7,0x17,0x16,0xD6,0xD2,0x12,0x13,0xD3,
  184. 0x11,0xD1,0xD0,0x10,0xF0,0x30,0x31,0xF1,0x33,0xF3,
  185. 0xF2,0x32,0x36,0xF6,0xF7,0x37,0xF5,0x35,0x34,0xF4,
  186. 0x3C,0xFC,0xFD,0x3D,0xFF,0x3F,0x3E,0xFE,0xFA,0x3A,
  187. 0x3B,0xFB,0x39,0xF9,0xF8,0x38,0x28,0xE8,0xE9,0x29,
  188. 0xEB,0x2B,0x2A,0xEA,0xEE,0x2E,0x2F,0xEF,0x2D,0xED,
  189. 0xEC,0x2C,0xE4,0x24,0x25,0xE5,0x27,0xE7,0xE6,0x26,
  190. 0x22,0xE2,0xE3,0x23,0xE1,0x21,0x20,0xE0,0xA0,0x60,
  191. 0x61,0xA1,0x63,0xA3,0xA2,0x62,0x66,0xA6,0xA7,0x67,
  192. 0xA5,0x65,0x64,0xA4,0x6C,0xAC,0xAD,0x6D,0xAF,0x6F,
  193. 0x6E,0xAE,0xAA,0x6A,0x6B,0xAB,0x69,0xA9,0xA8,0x68,
  194. 0x78,0xB8,0xB9,0x79,0xBB,0x7B,0x7A,0xBA,0xBE,0x7E,
  195. 0x7F,0xBF,0x7D,0xBD,0xBC,0x7C,0xB4,0x74,0x75,0xB5,
  196. 0x77,0xB7,0xB6,0x76,0x72,0xB2,0xB3,0x73,0xB1,0x71,
  197. 0x70,0xB0,0x50,0x90,0x91,0x51,0x93,0x53,0x52,0x92,
  198. 0x96,0x56,0x57,0x97,0x55,0x95,0x94,0x54,0x9C,0x5C,
  199. 0x5D,0x9D,0x5F,0x9F,0x9E,0x5E,0x5A,0x9A,0x9B,0x5B,
  200. 0x99,0x59,0x58,0x98,0x88,0x48,0x49,0x89,0x4B,0x8B,
  201. 0x8A,0x4A,0x4E,0x8E,0x8F,0x4F,0x8D,0x4D,0x4C,0x8C,
  202. 0x44,0x84,0x85,0x45,0x87,0x47,0x46,0x86,0x82,0x42,
  203. 0x43,0x83,0x41,0x81,0x80,0x40
  204. };
  205. unsignedcharuchCRCHi=0xFF;/*高CRC位元組初始化*/
  206. unsignedcharuchCRCLo=0xFF;/*低CRC位元組初始化*/
  207. unsigneduIndex=0;/*CRC迴圈中的索引*/
  208. while(usDataLen--)/*傳輸訊息緩衝區*/
  209. {
  210. uIndex=uchCRCHi^*puchMsg++;/*計算CRC*/
  211. uchCRCHi=uchCRCLo^auchCRCHi[uIndex];
  212. uchCRCLo=auchCRCLo[uIndex];
  213. }
  214. return(unsignedshort)((unsignedshort)uchCRCHi<<8|uchCRCLo);
  215. }
  216. /*****************************************************************************/
  217. /*********************************************************************************/
  218. /*函式名稱:SendRtuCmdToModBus()
  219. *輸入引數:共個引數;
  220. *輸出引數:共個引數;
  221. *返回值:無
  222. *需儲存的引數:共個引數;
  223. *功能介紹:
  224. (1)傳送ModBusRTU指令到ModbusSlave,加上CRC16校驗碼;
  225. *修改日誌:
  226. *[2005-11-2816:40]Ver.1.00
  227. 開始編寫;
  228. 完成;
  229. *[2006-3-19:02]
  230. 返回型別改為void;
  231. /**/
  232. /*********************************************************************************/
  233. voidSendRtuCmdToModBus(intModPort,unsignedcharcmd[],unsignedshortlen)
  234. {
  235. unsignedshortCRC16;
  236. CRC16=GetCRC16(cmd,len);
  237. ToComBufn(ModPort,cmd,len);
  238. ToCom(ModPort,(unsignedchar)(CRC16>>8&0x00FF));/*sendCRC16high*/
  239. ToCom(ModPort,(unsignedchar)(CRC16&0x00FF));/*sendCRC16low*/
  240. }
  241. /*****************************************************************************/
  242. /*********************************************************************************/
  243. /*函式名稱:ReadModBusRtuSlave()
  244. *輸入引數:共8個引數;
  245. *輸出引數:共5個引數;
  246. *返回值:成功與否1:成功,2:失敗;
  247. *需儲存的引數:共0個引數;
  248. *功能介紹:
  249. (1)讀取ModBusRtuSlave,並解碼輸出反饋字串相關內容;
  250. ReadModBusRtuSlave(COMPORT1,ModAddr,ModFunction,ModByteNum,
  251. ModData,&ModDataLen,ModTimeout=10,waitTime=30)
  252. *修改日誌:
  253. *[2005-11-2816:40]Ver.1.00
  254. 開始編寫;
  255. 完成;
  256. *[2006-3-19:23]
  257. 增加了intcounter=0;
  258. unsignedcharModBusMessage[MOD_STR_MAX_LEN];
  259. *[2006-3-113:46]
  260. 增加了strmcpy(ModData,ModBusMessage+3,ModBusIdx-5);
  261. /**/
  262. /*********************************************************************************/
  263. intReadModBusRtuSlave(intModPort,unsignedchar*ModAddr,unsignedchar*ModFunction,unsignedchar*ModByteNum,
  264. unsignedcharModData[],unsignedchar*ModDataLen,unsignedModTimeout,unsignedwaitTime)
  265. {
  266. unsignedchardata;
  267. unsignedlongt;
  268. inti;
  269. unsignedshortCRC16;
  270. intModBusIdx=0;
  271. intcounter=0;
  272. unsignedcharModBusMessage[MOD_STR_MAX_LEN]={'/0'};
  273. while((!(IsCom(ModPort))&&(counter<waitTime)))
  274. {
  275. DelayMs(1);
  276. counter++;
  277. }
  278. StopWatchStart(MODBUS_STOP_WATCH);
  279. do
  280. {
  281. while(IsCom(ModPort)&&(ModBusIdx<MOD_STR_MAX_LEN-1))
  282. {
  283. data=ReadCom(ModPort);
  284. ModBusMessage[ModBusIdx++]=data;
  285. StopWatchStart(MODBUS_STOP_WATCH);
  286. /*Print("[%02X]",data);*/
  287. }
  288. StopWatchReadValue(MODBUS_STOP_WATCH,&t);
  289. }
  290. while(t<=ModTimeout);
  291. if(ModBusIdx==0)return0;
  292. ModBusMessage[ModBusIdx]='/0';
  293. StopWatchStop(MODBUS_STOP_WATCH);
  294. CRC16=GetCRC16(ModBusMessage,ModBusIdx-2);
  295. /*for(i=0;i>8&0x00FF)==ModBusMessage[ModBusIdx-2]&&(unsignedchar)(CRC16&0x00FF)==ModBusMessage[ModBusIdx-1])
  296. {
  297. *ModAddr=ModBusMessage[0];
  298. *ModFunction=ModBusMessage[1];
  299. *ModByteNum=ModBusMessage[2];
  300. strmcpy(ModData,ModBusMessage+3,ModBusIdx-5);
  301. /*Print("idx%d",ModBusIdx-5);/*test*/
  302. /*for(i=3;i<ModBusIdx-2;i++)
  303. {
  304. ModData[i-3]=ModBusMessage[i];
  305. }
  306. ModData[i]='/0';
  307. */
  308. *ModDataLen=ModBusIdx-5;
  309. /*Print("{DataLen=%d}/n/r",*ModDataLen);*/
  310. return1;
  311. }
  312. else
  313. {
  314. *ModDataLen=0;
  315. return0;
  316. /*CRC16error*/
  317. /*Print("{CRC16Error}/n/r");*/
  318. }
  319. }
  320. /*-------------------------------------------------------------------------------*/
  321. /*********************************************************************************/
  322. /*函式名稱:ResponseFromModBusRtuSlave()
  323. *輸入引數:共8個引數;
  324. *輸出引數:共5個引數;
  325. *返回值:成功與否1:成功,2:失敗;
  326. *需儲存的引數:共0個引數;
  327. *功能介紹:
  328. (1)寫引數到ModBusRtuSlave後,ModbusRtuSlave反饋字串;
  329. *修改日誌:
  330. *[2006-3-112:34]Ver.1.00
  331. 開始編寫;
  332. *[2006-3-112:48]
  333. 完成;
  334. *[2006-3-616:32]
  335. strcpy(totalStr,ModBusMessage);
  336. 改為strmcpy(totalStr,ModBusMessage,ModBusIdx);
  337. /**/
  338. /*********************************************************************************/
  339. intResponseFromModBusRtuSlave(intModPort,unsignedchartotalStr[],
  340. unsignedchar*ModAddr,unsignedchar*ModFunction,unsignedshort*ModDataAddr,
  341. unsignedshort*ModDataNum,unsignedModTimeout,unsignedwaitTime)
  342. {
  343. unsignedchardata;
  344. unsignedlongt;
  345. unsignedshortCRC16;
  346. intModBusIdx=0;
  347. intcounter=0;
  348. unsignedcharModBusMessage[MOD_STR_MAX_LEN]={'/0'};
  349. while((!(IsCom(ModPort))&&(counter<waitTime)))
  350. {
  351. DelayMs(1);
  352. counter++;
  353. }
  354. StopWatchStart(MODBUS_STOP_WATCH);
  355. do
  356. {
  357. while(IsCom(ModPort)&&(ModBusIdx<MOD_STR_MAX_LEN-1))
  358. {
  359. data=ReadCom(ModPort);
  360. ModBusMessage[ModBusIdx++]=data;
  361. StopWatchStart(MODBUS_STOP_WATCH);
  362. /*Print("[%02X]",data);*/
  363. }
  364. StopWatchReadValue(MODBUS_STOP_WATCH,&t);
  365. }
  366. while(t<=ModTimeout);
  367. if(ModBusIdx==0)return0;
  368. ModBusMessage[ModBusIdx]='/0';
  369. strmcpy(totalStr,ModBusMessage,ModBusIdx);
  370. StopWatchStop(MODBUS_STOP_WATCH);
  371. CRC16=GetCRC16(ModBusMessage,ModBusIdx-2);
  372. if((unsignedchar)(CRC16>>8&0x00FF)==ModBusMessage[ModBusIdx-2]&&(unsignedchar)(CRC16&0x00FF)==ModBusMessage[ModBusIdx-1])
  373. {
  374. *ModAddr=ModBusMessage[0];
  375. *ModFunction=ModBusMessage[1];
  376. *ModDataAddr=(unsignedshort)((unsignedshort)ModBusMessage[2]<<8|ModBusMessage[3]);
  377. *ModDataNum=(unsignedshort)((unsignedshort)ModBusMessage[4]<<8|ModBusMessage[5]);
  378. return1;
  379. }
  380. else
  381. {
  382. return0;
  383. /*CRC16error*/
  384. /*Print("{CRC16Error}/n/r");*/
  385. }
  386. }
  387. /*-------------------------------------------------------------------------------*/
  388. /*********************************************************************************/
  389. /*函式名稱:ResponseSingleBitFromModBus()
  390. *輸入引數:共8個引數;
  391. *輸出引數:共5個引數;
  392. *返回值:成功與否1:成功,2:失敗;
  393. *需儲存的引數:共0個引數;
  394. *功能介紹:
  395. (1)寫引數到ModBusRtuSlave後,ModbusRtuSlave反饋字串;
  396. *修改日誌:
  397. *[2006-3-714:40]Ver.1.00
  398. 開始編寫;
  399. *[2006-3-714:40]
  400. 完成;
  401. /**/
  402. /*********************************************************************************/
  403. intResponseSingleBitFromModBus(intModPort,unsignedchartotalStr[],
  404. unsignedchar*ModAddr,unsignedchar*ModFunction,unsignedshort*ModDataAddr,
  405. unsignedshort*ModDataState,unsignedModTimeout,unsignedwaitTime)
  406. {
  407. unsignedchardata;
  408. unsignedlongt;
  409. unsignedshortCRC16;
  410. intModBusIdx=0;
  411. intcounter=0;
  412. unsignedcharModBusMessage[MOD_STR_MAX_LEN]={'/0'};
  413. while((!(IsCom(ModPort))&&(counter<waitTime)))
  414. {
  415. DelayMs(1);
  416. counter++;
  417. }
  418. StopWatchStart(MODBUS_STOP_WATCH);
  419. do
  420. {
  421. while(IsCom(ModPort)&&(ModBusIdx<MOD_STR_MAX_LEN-1))
  422. {
  423. data=ReadCom(ModPort);
  424. ModBusMessage[ModBusIdx++]=data;
  425. StopWatchStart(MODBUS_STOP_WATCH);
  426. /*Print("[%02X]",data);*/
  427. }
  428. StopWatchReadValue(MODBUS_STOP_WATCH,&t);
  429. }
  430. while(t<=ModTimeout);
  431. if(ModBusIdx==0)return0;
  432. ModBusMessage[ModBusIdx]='/0';
  433. strmcpy(totalStr,ModBusMessage,ModBusIdx);
  434. StopWatchStop(MODBUS_STOP_WATCH);
  435. CRC16=GetCRC16(ModBusMessage,ModBusIdx-2);
  436. if((unsignedchar)(CRC16>>8&0x00FF)==ModBusMessage[ModBusIdx-2]&&(unsignedchar)(CRC16&0x00FF)==ModBusMessage[ModBusIdx-1])
  437. {
  438. *ModAddr=ModBusMessage[0];
  439. *ModFunction=ModBusMessage[1];
  440. *ModDataAddr=(unsignedshort)((unsignedshort)ModBusMessage[2]<<8|ModBusMessage[3]);
  441. if(ModBusMessage[4])*ModDataState=1;
  442. else*ModDataState=0;
  443. return1;
  444. }
  445. else
  446. {
  447. return0;
  448. /*CRC16error*/
  449. /*Print("{CRC16Error}/n/r");*/
  450. }
  451. }
  452. /*-------------------------------------------------------------------------------*/
  453. /*********************************************************************************/
  454. /*函式名稱:ChangeAllParameterDataByModBus()
  455. *輸入引數:共個引數;
  456. *輸出引數:共個引數;
  457. *返回值:無
  458. *需儲存的引數:共個引數;
  459. *功能介紹:
  460. (1)將MODBUSSLAVE返回的資料字元轉化為引數值;
  461. (2)void*memcpy(void*dest,constvoid*src,size_tn)
  462. (3)四個字元的順序應倒一下。
  463. *修改日誌:
  464. *[2006-3-114:10]Ver.1.00
  465. 開始編寫;
  466. *[2006-3-114:35]
  467. 完成;
  468. *[2006-3-615:57]
  469. 四個字元的順序應倒一下。
  470. /**/
  471. /*********************************************************************************/
  472. voidChangeAllParameterDataByModBus(floatparameterData[],unsignedcharModDataBack[],unsignedshortnum)
  473. {
  474. inti;
  475. charsingleStr[5]={'/0','/0','/0','/0','/0'};
  476. float*pf;
  477. floatdataf=0;
  478. intlen=4;
  479. pf=&dataf;
  480. for(i=0;i<num;i++)
  481. {
  482. singleStr[0]=ModDataBack[i*4+3];
  483. singleStr[1]=ModDataBack[i*4+2];
  484. singleStr[2]=ModDataBack[i*4+1];
  485. singleStr[3]=ModDataBack[i*4];
  486. singleStr[4]='/0';
  487. memcpy(pf,singleStr,len);
  488. parameterData[i]=*pf;
  489. /*Print("%-6.2f/t",parameterData[i]);/*test*/
  490. }
  491. }
  492. /*-------------------------------------------------------------------------------*/
  493. /*********************************************************************************/
  494. /*函式名稱:ChangeAllButtonsDataByModBus()
  495. *輸入引數:共個引數;
  496. *輸出引數:共個引數;
  497. *返回值:無
  498. *需儲存的引數:共個引數;
  499. *功能介紹:
  500. (1)將MODBUSSLAVE返回的資料字元轉化為按鈕值;
  501. *修改日誌:
  502. *[2006-3-114:03]Ver.1.00
  503. 開始編寫;
  504. *[2006-3-114:10]
  505. 完成;
  506. /**/
  507. /*********************************************************************************/
  508. voidChangeAllButtonsDataByModBus(intbuttonData[],unsignedcharModDataBack[],unsignedshortnum)
  509. {
  510. unsignedcharbutton8Data=0;
  511. inti,j;
  512. for(j=0;j<(int)(num/8);j++)
  513. {
  514. button8Data=ModDataBack[j];
  515. for(i=0;i<8;i++)
  516. {
  517. buttonData[i+j*8]=(int)(button8Data&(int)pow(2,i))/(int)pow(2,i);
  518. }
  519. }
  520. button8Data=ModDataBack[j];
  521. for(i=0;i<num%8;i++)
  522. {
  523. buttonData[i+j*8]=(int)(button8Data&(int)pow(2,i))/(int)pow(2,i);
  524. }
  525. }
  526. /*-------------------------------------------------------------------------------*/
  527. /*********************************************************************************/
  528. /*函式名稱:WordDataToModbusStr()
  529. *輸入引數:共個引數;
  530. *輸出引數:共個引數;
  531. *返回值:無
  532. *需儲存的引數:共個引數;
  533. *功能介紹:
  534. (1)parameterData或AIData轉化為MODBUSSLAVE的WORD值的STR;
  535. (2)void*memcpy(void*dest,constvoid*src,size_tn)
  536. (3)四個字元的順序應倒一下。
  537. *修改日誌:
  538. *[2006-3-114:37]Ver.1.00
  539. 開始編寫;
  540. *[2006-3-114:49]
  541. 完成;
  542. *[2006-3-615:57]
  543. 四個字元的順序應倒一下。
  544. /**/
  545. /*********************************************************************************/
  546. voidWordDataToModbusStr(unsignedcharwriteCmdStr[],floatdata[],unsignedshortnum,unsignedshortstartAddr)
  547. {
  548. inti;
  549. charsingleStr[5]={'/0','/0','/0','/0','/0'};
  550. float*pf;
  551. unsignedchar*pstr;
  552. floatdataf=0;
  553. intlen=4;
  554. pf=&dataf;
  555. pstr=singleStr;
  556. for(i=0;i<num;i++)
  557. {
  558. *pf=data[i];
  559. memcpy(pstr,pf,len);
  560. writeCmdStr[startAddr+i*4+3]=singleStr[0];
  561. writeCmdStr[startAddr+i*4+2]=singleStr[1];
  562. writeCmdStr[startAddr+i*4+1]=singleStr[2];
  563. writeCmdStr[startAddr+i*4]=singleStr[3];
  564. }
  565. }
  566. /*-------------------------------------------------------------------------------*/
  567. /*********************************************************************************/
  568. /*函式名稱:BitDataToModbusStr()
  569. *輸入引數:共個引數;
  570. *輸出引數:共個引數;
  571. *返回值:無
  572. *需儲存的引數:共個引數;
  573. *功能介紹:
  574. (1)data轉化為MODBUSSLAVE的WORD值的STR;
  575. *修改日誌:
  576. *[2006-3-114:51]Ver.1.00
  577. 開始編寫;
  578. *[2006-3-115:02]
  579. 完成;
  580. /**/
  581. /*********************************************************************************/
  582. voidBitDataToModbusStr(unsignedcharwriteCmdStr[],intdata[],unsignedshortnum,unsignedshortstartAddr)
  583. {
  584. inti,j;
  585. unsignedcharbutton8Data=0;
  586. for(j=0;j<(int)(num/8);j++)
  587. {
  588. button8Data=0;
  589. for(i=0;i<8;i++)
  590. {
  591. button8Data+=data[i+j*8]*(int)pow(2,i);
  592. }
  593. writeCmdStr[startAddr+j]=button8Data;
  594. }
  595. if(num%8)
  596. {
  597. button8Data=0;
  598. for(i=0;i<num%8;i++)
  599. {
  600. button8Data+=data[i+j*8]*(int)pow(2,i);
  601. }
  602. writeCmdStr[startAddr+j]=button8Data;
  603. }
  604. }
  605. /*-------------------------------------------------------------------------------*/
  606. /*********************************************************************************/
  607. /*函式名稱:ReadWordFromModBusSlave()
  608. *輸入引數:共2個引數;
  609. *輸出引數:共1個引數;
  610. *返回值:成功與否;
  611. *需儲存的引數:共1個引數;
  612. *功能介紹:
  613. (1)從觸控式螢幕中讀取設定引數值;
  614. (2)讀取指令為0103HALAHNLNHCLC;其中01為MODBUSSLAVE的地址,
  615. 03為讀連續多個WORD暫存器的功能碼,HALA為第一個暫存器的高低位地址,
  616. HNLN為暫存器數量的高低位,HCLC為CRC校驗碼;
  617. (3)觸控式螢幕reset後各個引數歸零,7188檢測到此狀態後向除摸屏傳送設定引數;
  618. *修改日誌:
  619. *[2005-11-2816:51]Ver.1.00
  620. 開始編寫;
  621. *[2005-11-2818:10]
  622. 完成初稿,未測試;
  623. *[2006-3-39:38]
  624. staticunsignedcharoldModDataBack[400];改為形參;
  625. *[2006-3-317:58]
  626. 增加intkind形參;
  627. *[2006-3-616:34]
  628. strmcpy(oldModDataBack,ModDataBack,ModDataLenBack);
  629. *[2006-3-912:33]
  630. 去掉
  631. (strmcmp(oldModDataBack,ModDataBack,ModDataLenBack))
  632. /**/
  633. /*********************************************************************************/
  634. intReadWordFromModBusSlave(intkind,intcomPort,floatparameterData[],unsignedshortnum,
  635. unsignedshortModDataAddr,intparameterEEPROMAddr,unsignedcharoldModDataBack[])
  636. {
  637. floatparameterJudge[50];
  638. unsignedcharreadCmdStr[15];
  639. unsignedcharModAddr=1;
  640. unsignedcharModFunction=3;
  641. unsignedshortModDataNum;
  642. unsignedshortreadCmdLen=6;
  643. intisReadOK=0;
  644. unsignedcharModAddrBack=0;
  645. unsignedcharModFunctionBack=0;
  646. unsignedcharModByteNumBack=0;
  647. unsignedcharModDataBack[MOD_STR_MAX_LEN]={'/0'};
  648. unsignedcharModDataLenBack=0;
  649. /*unsignedshortModWord[100];*/
  650. intparameterDecimalEEPROM[PARAMETER_DATA_NUM]=PARAMETER_DECIMAL_EEPROM;/*adjust*/
  651. intparameterStartAddr=0;
  652. chari7188Addr[5];
  653. strcpy(i7188Addr,I7188_ADDRESS);/*adjust*/
  654. if(kind==1)
  655. {
  656. strcpy(i7188Addr,I7188_ADDRESS);
  657. }
  658. elseif(kind==2)
  659. {
  660. strcpy(i7188Addr,I7188_ADDRESS_2);
  661. }
  662. elseif(kind==3)
  663. {
  664. strcpy(i7188Addr,I7188_ADDRESS_3);
  665. }
  666. ModDataNum=(unsignedshort)num*2;
  667. readCmdStr[0]=ModAddr;
  668. readCmdStr[1]=ModFunction;
  669. readCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
  670. readCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
  671. readCmdStr[4]=(unsignedchar)(ModDataNum>>8&0x00FF);
  672. readCmdStr[5]=(unsignedchar)(ModDataNum&0x00FF);
  673. readCmdStr[6]='/0';
  674. ClearCom(comPort);
  675. SendRtuCmdToModBus(comPort,readCmdStr,readCmdLen);
  676. isReadOK=ReadModBusRtuSlave(comPort,&ModAddrBack,&ModFunctionBack,&ModByteNumBack,
  677. ModDataBack,&ModDataLenBack,MOD_TIMEOUT,MOD_WAITTIME);
  678. if(1==isReadOK)
  679. {
  680. if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModByteNumBack==ModDataLenBack))
  681. {
  682. if(ModDataNum*2==ModDataLenBack)/*!!!*/
  683. {
  684. ChangeAllParameterDataByModBus(parameterJudge,ModDataBack,num);
  685. /*Print("%f%f%f/t",parameterData[0],parameterData[1],parameterData[2]);
  686. Print("/n%f%f%f/t",parameterJudge[0],parameterJudge[1],parameterJudge[2]);
  687. */if(parameterJudge[0]>=0.1)/*以溫度設定值為判斷點判斷正確性*/
  688. {
  689. /*Print("cp1.5/n");*/
  690. if(Datamcmp(parameterData,parameterJudge,num))
  691. {
  692. Print("cp1.6/n");
  693. ChangeAllParameterDataByModBus(parameterData,ModDataBack,num);
  694. if(IS_RS485)Set485DirToTransmit(COMPORT1);
  695. SendParameterDataToPCForwardly(COMPORT1,parameterData,i7188Addr);
  696. if(IS_RS485)WaitTransmitOver(COMPORT1);
  697. if(IS_RS485){DelayMs(2);Set485DirToReceive(COMPORT1);}
  698. WriteDataToEEPROM(parameterData,parameterDecimalEEPROM,num,parameterEEPROMAddr,parameterStartAddr);
  699. }
  700. strmcpy(oldModDataBack,ModDataBack,ModDataLenBack);
  701. }
  702. else
  703. {
  704. /*未初始化,傳送資料以初始*/
  705. WriteMultipleWordToModBusSlave(comPort,parameterData,num,ModDataAddr);
  706. }
  707. /*Print("/n%f%f%f/t",parameterData[0],parameterData[1],parameterData[2]);*/
  708. }
  709. return1;
  710. }
  711. else
  712. {
  713. /*收到的字串不匹配*/
  714. return0;
  715. }
  716. }
  717. else
  718. {
  719. /*未收到字串或CRC校驗出錯*/
  720. return0;
  721. }
  722. }
  723. /*-------------------------------------------------------------------------------*/
  724. /*********************************************************************************/
  725. /*函式名稱:ReadBitFromModBusSlave()
  726. *輸入引數:共2個引數;
  727. *輸出引數:共1個引數;
  728. *返回值:成功與否;
  729. *需儲存的引數:共1個引數;
  730. *功能介紹:
  731. (1)從觸控式螢幕中讀取按鈕值;
  732. (2)讀取指令為0101HALAHNLNHCLC;其中01為MODBUSSLAVE的地址,
  733. 01為讀連續多個BIT線圈的功能碼,HALA為第一個BIT的高低位地址,
  734. HNLN為BIT數量的高低位,HCLC為CRC校驗碼;
  735. *修改日誌:
  736. *[2005-11-2818:10]Ver.1.00
  737. 開始編寫;
  738. *[2005-11-2818:42]
  739. 完成初稿,未測試;
  740. *[2006-3-39:38]
  741. staticunsignedcharoldModDataBack[400];改為形參;
  742. *[2006-3-317:58]
  743. 增加intkind形參;
  744. *[2006-3-616:35]
  745. strmcpy(oldModDataBack,ModDataBack,ModDataLenBack);
  746. *[2006-3-912:35]
  747. 去掉(strmcmp(oldModDataBack,ModDataBack,ModDataLenBack))
  748. /**/
  749. /*********************************************************************************/
  750. intReadBitFromModBusSlave(intkind,intcomPort,intbuttonData[],unsignedshortnum,
  751. unsignedshortModDataAddr,intbuttonNVRAMAddr,unsignedcharoldModDataBack[])
  752. {
  753. intbuttonJudge[30];
  754. unsignedcharreadCmdStr[15];
  755. unsignedcharModAddr=1;
  756. unsignedcharModFunction=1;
  757. unsignedshortModDataNum;
  758. unsignedshortreadCmdLen=6;
  759. intisReadOK=0;
  760. unsignedcharModAddrBack=0;
  761. unsignedcharModFunctionBack=0;
  762. unsignedcharModByteNumBack=0;
  763. unsignedcharModDataBack[300]={'/0'};
  764. unsignedcharModDataLenBack=0;
  765. /*unsignedshortModBit[100];*/
  766. chari7188Addr[5];
  767. strcpy(i7188Addr,I7188_ADDRESS);/*adjust*/
  768. if(kind==1)
  769. {
  770. strcpy(i7188Addr,I7188_ADDRESS);
  771. }
  772. elseif(kind==2)
  773. {
  774. strcpy(i7188Addr,I7188_ADDRESS_2);
  775. }
  776. elseif(kind==3)
  777. {
  778. strcpy(i7188Addr,I7188_ADDRESS_3);
  779. }
  780. ModDataNum=(unsignedshort)num;
  781. readCmdStr[0]=ModAddr;
  782. readCmdStr[1]=ModFunction;
  783. readCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
  784. readCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
  785. readCmdStr[4]=(unsignedchar)(ModDataNum>>8&0x00FF);
  786. readCmdStr[5]=(unsignedchar)(ModDataNum&0x00FF);
  787. readCmdStr[6]='/0';
  788. ClearCom(comPort);
  789. SendRtuCmdToModBus(comPort,readCmdStr,readCmdLen);
  790. isReadOK=ReadModBusRtuSlave(comPort,&ModAddrBack,&ModFunctionBack,&ModByteNumBack,
  791. ModDataBack,&ModDataLenBack,MOD_TIMEOUT,MOD_WAITTIME);
  792. if(1==isReadOK)
  793. {
  794. if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModByteNumBack==ModDataLenBack))
  795. {
  796. ChangeAllButtonsDataByModBus(buttonJudge,ModDataBack,num);
  797. if(Bitmcmp(buttonData,buttonJudge,num))
  798. {
  799. ChangeAllButtonsDataByModBus(buttonData,ModDataBack,num);
  800. if(IS_RS485)Set485DirToTransmit(COMPORT1);
  801. SendButtonDataToPC(COMPORT1,buttonData,i7188Addr);
  802. if(IS_RS485)WaitTransmitOver(COMPORT1);
  803. if(IS_RS485){DelayMs(2);Set485DirToReceive(COMPORT1);}
  804. WriteButtonToNVRAM(buttonData,num,buttonNVRAMAddr);
  805. }
  806. strmcpy(oldModDataBack,ModDataBack,ModDataLenBack);
  807. return1;
  808. }
  809. else
  810. {
  811. /*收到的字串不匹配*/
  812. return1;
  813. }
  814. }
  815. else
  816. {
  817. /*未收到字串或CRC校驗出錯*/
  818. return0;
  819. }
  820. }
  821. /*-------------------------------------------------------------------------------*/
  822. /*********************************************************************************/
  823. /*函式名稱:WriteMultipleWordToModBusSlave()
  824. *輸入引數:共2個引數;
  825. *輸出引數:共1個引數;
  826. *返回值:成功與否;
  827. *需儲存的引數:共1個引數;
  828. *功能介紹:
  829. (1)從觸控式螢幕中寫入設定引數值;
  830. (2)指令為0116HALAHNLNHDLD......HCLC;其中01為MODBUSSLAVE的地址,
  831. 16為寫連續多個WORD暫存器的功能碼,HALA為第一個暫存器的高低位地址,
  832. HNLN為暫存器數量的高低位,HCLC為CRC校驗碼;
  833. HDLD為資料高低位;
  834. *修改日誌:
  835. *[2005-11-2818:43]Ver.1.00
  836. 開始編寫;
  837. *[2006-3-112:20]
  838. 中間隔了很多時間;
  839. 完成初稿,未測試;
  840. /**/
  841. /*********************************************************************************/
  842. intWriteMultipleWordToModBusSlave(intcomPort,floatparameterData[],unsignedshortnum,unsignedshortModDataAddr)
  843. {
  844. unsignedcharwriteCmdStr[MOD_STR_MAX_LEN];/*adjust*/
  845. unsignedcharModAddr=1;
  846. unsignedcharModFunction=16;
  847. unsignedshortModDataNum;
  848. unsignedshortreadCmdLen;
  849. intisReadOK=0;
  850. unsignedshortbyteCount;
  851. unsignedchartotalStrBack[300]={'/0'};
  852. unsignedcharModAddrBack=0;
  853. unsignedcharModFunctionBack=0;
  854. unsignedshortModDataAddrBack;
  855. unsignedshortModDataNumBack;
  856. ModDataNum=(unsignedshort)num*2;
  857. byteCount=(unsignedshort)num*4;
  858. readCmdLen=7+byteCount;
  859. writeCmdStr[0]=ModAddr;
  860. writeCmdStr[1]=ModFunction;
  861. writeCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
  862. writeCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
  863. writeCmdStr[4]=(unsignedchar)(ModDataNum>>8&0x00FF);
  864. writeCmdStr[5]=(unsignedchar)(ModDataNum&0x00FF);
  865. writeCmdStr[6]=(unsignedchar)(byteCount&0x00FF);
  866. WordDataToModbusStr(writeCmdStr,parameterData,num,7);
  867. writeCmdStr[7+byteCount]='/0';
  868. ClearCom(comPort);
  869. SendRtuCmdToModBus(comPort,writeCmdStr,readCmdLen);
  870. isReadOK=ResponseFromModBusRtuSlave(comPort,totalStrBack,
  871. &ModAddrBack,&ModFunctionBack,&ModDataAddrBack,
  872. &ModDataNumBack,MOD_TIMEOUT,MOD_WAITTIME);
  873. if(1==isReadOK)
  874. {
  875. if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModDataAddr==ModDataAddrBack)
  876. &&(ModDataNum==ModDataNumBack))
  877. {
  878. return1;
  879. }
  880. else
  881. {
  882. return0;
  883. }
  884. }
  885. else
  886. {
  887. /*未收到字串或CRC校驗出錯*/
  888. return0;
  889. }
  890. }
  891. /*-------------------------------------------------------------------------------*/
  892. /*********************************************************************************/
  893. /*函式名稱:WriteMultipleBitToModBusSlave()
  894. *輸入引數:共2個引數;
  895. *輸出引數:共1個引數;
  896. *返回值:成功與否;
  897. *需儲存的引數:共1個引數;
  898. *功能介紹:
  899. (1)從觸控式螢幕中寫入BIT;
  900. (2)指令為0115HALAHNLNHDLD......HCLC;其中01為MODBUSSLAVE的地址,
  901. 16為讀連續多個WORD暫存器的功能碼,HALA為第一個BIT的高低位地址,
  902. HNLN為BIT數量的高低位,HCLC為CRC校驗碼;
  903. HDLD為資料高低位;
  904. *修改日誌:
  905. *[2006-3-112:58]Ver.1.00
  906. 開始編寫;
  907. *[2006-3-113:29]
  908. 完成;
  909. /**/
  910. /*********************************************************************************/
  911. intWriteMultipleBitToModBusSlave(intcomPort,intDOData[],unsignedshortnum,unsignedshortModDataAddr)
  912. {
  913. unsignedcharwriteCmdStr[100];
  914. unsignedcharModAddr=1;
  915. unsignedcharModFunction=15;
  916. unsignedshortModDataNum;
  917. unsignedshortreadCmdLen;
  918. intisReadOK=0;
  919. unsignedshortbyteCount;
  920. unsignedchartotalStrBack[300]={'/0'};
  921. unsignedcharModAddrBack=0;
  922. unsignedcharModFunctionBack=0;
  923. unsignedshortModDataAddrBack;
  924. unsignedshortModDataNumBack;
  925. ModDataNum=(unsignedshort)num;
  926. if(num%8)
  927. byteCount=(unsignedshort)(num/8+1);
  928. elsebyteCount=(unsignedshort)(num/8);
  929. readCmdLen=7+byteCount;
  930. writeCmdStr[0]=ModAddr;
  931. writeCmdStr[1]=ModFunction;
  932. writeCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
  933. writeCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
  934. writeCmdStr[4]=(unsignedchar)(ModDataNum>>8&0x00FF);
  935. writeCmdStr[5]=(unsignedchar)(ModDataNum&0x00FF);
  936. writeCmdStr[6]=(unsignedchar)(byteCount&0x00FF);
  937. BitDataToModbusStr(writeCmdStr,DOData,num,7);
  938. writeCmdStr[7+byteCount]='/0';
  939. ClearCom(comPort);
  940. SendRtuCmdToModBus(comPort,writeCmdStr,readCmdLen);
  941. isReadOK=ResponseFromModBusRtuSlave(comPort,totalStrBack,
  942. &ModAddrBack,&ModFunctionBack,&ModDataAddrBack,
  943. &ModDataNumBack,MOD_TIMEOUT,MOD_WAITTIME);
  944. if(1==isReadOK)
  945. {
  946. if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModDataAddr==ModDataAddrBack)
  947. &&(ModDataNum==ModDataNumBack))
  948. {
  949. return1;
  950. }
  951. else
  952. {
  953. return0;
  954. }
  955. }
  956. else
  957. {
  958. /*未收到字串或CRC校驗出錯*/
  959. return0;
  960. }
  961. }
  962. /*-------------------------------------------------------------------------------*/
  963. /*********************************************************************************/
  964. /*函式名稱:WriteSingleBitToModBusSlave()
  965. *輸入引數:共2個引數;
  966. *輸出引數:共1個引數;
  967. *返回值:成功與否;
  968. *需儲存的引數:共1個引數;
  969. *功能介紹:
  970. (1)從觸控式螢幕中寫入BIT;
  971. (2)指令為0105HALAHDLD......HCLC;其中01為MODBUSSLAVE的地址,
  972. 05為寫單個BIT功能碼,HALA為第一個BIT的高低位地址,
  973. HNLN為BIT數量的高低位,HCLC為CRC校驗碼;
  974. HDLD為資料高低位;
  975. *修改日誌:
  976. *[2006-3-714:06]Ver.1.00
  977. 開始編寫;
  978. *[2006-3-714:40]
  979. 完成;
  980. *[2006-3-912:44]
  981. 去掉了等待接收反饋的功能;
  982. /**/
  983. /*********************************************************************************/
  984. intWriteSingleBitToModBusSlave(intcomPort,intDOData[],unsignedshortnum,unsignedshortModDataAddr)
  985. {
  986. unsignedcharwriteCmdStr[15];
  987. unsignedcharModAddr=1;
  988. unsignedcharModFunction=5;
  989. unsignedshortModDataState;
  990. unsignedshortreadCmdLen=6;
  991. /*intisReadOK=0;
  992. unsignedshortbyteCount;
  993. unsignedchartotalStrBack[100];
  994. unsignedcharModAddrBack=0;
  995. unsignedcharModFunctionBack=0;
  996. unsignedshortModDataAddrBack;
  997. unsignedshortModDataStateBack;
  998. */
  999. ModDataState=DOData[num];
  1000. writeCmdStr[0]=ModAddr;
  1001. writeCmdStr[1]=ModFunction;
  1002. writeCmdStr[2]=(unsignedchar)(ModDataAddr>>8&0x00FF);
  1003. writeCmdStr[3]=(unsignedchar)(ModDataAddr&0x00FF);
  1004. if(ModDataState)writeCmdStr[4]=(unsignedchar)(0x00FF);
  1005. elsewriteCmdStr[4]=(unsignedchar)(0x0000);
  1006. writeCmdStr[5]=(unsignedchar)(0x0000);
  1007. writeCmdStr[6]='/0';
  1008. /*ClearCom(comPort);*/
  1009. SendRtuCmdToModBus(comPort,writeCmdStr,readCmdLen);
  1010. return1;
  1011. /*
  1012. isReadOK=ResponseSingleBitFromModBus(comPort,totalStrBack,
  1013. &ModAddrBack,&ModFunctionBack,&ModDataAddrBack,
  1014. &ModDataStateBack,MOD_TIMEOUT,MOD_WAITTIME);
  1015. if(1==isReadOK)
  1016. {
  1017. Print("/tB%u%u%u%u%u%u%u%u",ModAddr,ModAddrBack,ModFunction,ModFunctionBack,ModDataAddr,ModDataAddrBack,ModDataState,ModDataStateBack);
  1018. if((ModAddr==ModAddrBack)&&(ModFunction==ModFunctionBack)&&(ModDataAddr==ModDataAddrBack)
  1019. &&(ModDataState==ModDataStateBack))
  1020. {
  1021. Print("/tretursB1/t");
  1022. return1;
  1023. }
  1024. else
  1025. {
  1026. Print("/tretursB0/t");
  1027. return0;
  1028. }
  1029. }
  1030. else
  1031. {
  1032. /*未收到字串或CRC校驗出錯*/
  1033. return0;
  1034. }
  1035. */
  1036. }
  1037. /*-------------------------------------------------------------------------------*/
  1038. /*********************************************************************************/
  1039. /*函式名稱:WriteBitByBitToModBusSlave()
  1040. *輸入引數:共個引數;
  1041. *輸出引數:共個引數;
  1042. *返回值:成功與否;
  1043. *需儲存的引數:共個引數;
  1044. *功能介紹:
  1045. (1)一個一個地將BIT寫如觸控式螢幕中;
  1046. (2)可能很耗時;
  1047. *修改日誌:
  1048. *[2006-3-714:40]Ver.1.00
  1049. 開始編寫;
  1050. *[2006-3-714:51]
  1051. 完成;
  1052. /**/
  1053. /*********************************************************************************/
  1054. intWriteBitByBitToModBusSlave(intcomPort,intDOData[],intdataOld[],unsignedshortnum,unsignedshortModDataAddr)
  1055. {
  1056. inti;
  1057. for(i=0;i<num;i++)
  1058. {
  1059. if(DOData[i]!=dataOld[i])
  1060. {
  1061. DelayMs(1);
  1062. WriteSingleBitToModBusSlave(comPort,DOData,i,ModDataAddr+i);
  1063. }
  1064. }
  1065. return1;
  1066. }
  1067. /*-------------------------------------------------------------------------------*/
  1068. /*********************************************************************************/
  1069. /*函式名稱:WriteAllBitByBitToModBusSlave()
  1070. *輸入引數:共個引數;
  1071. *輸出引數:共個引數;
  1072. *返回值:成功與否;
  1073. *需儲存的引數:共個引數;
  1074. *功能介紹:
  1075. (1)一個一個地將BIT寫如觸控式螢幕中;
  1076. (2)可能很耗時;
  1077. *修改日誌:
  1078. *[2006-3-912:52]Ver.1.00
  1079. 開始編寫;
  1080. *[2006-3-912:52]
  1081. 完成;
  1082. /**/
  1083. /*********************************************************************************/
  1084. intWriteAllBitByBitToModBusSlave(intcomPort,intDOData[],unsignedshortnum,unsignedshortModDataAddr)
  1085. {
  1086. inti;
  1087. for(i=0;i<num;i++)
  1088. {
  1089. DelayMs(1);
  1090. WriteSingleBitToModBusSlave(comPort,DOData,i,ModDataAddr+i);
  1091. }
  1092. return1;
  1093. }
  1094. /*-------------------------------------------------------------------------------*/
  1095. /*********************************************************************************/
  1096. /*函式名稱:ListenToTouch()
  1097. *輸入引數:共個引數;
  1098. *輸出引數:共個引數;
  1099. *返回值:無;
  1100. *需儲存的引數:共個引數;
  1101. *功能介紹:
  1102. (1)監控Touch觸控式螢幕;
  1103. (2)init的工作給WriteMultipleWordToModBusSlave是否成功。
  1104. (3)必須要DelayMs(50)才能使一下個response正確。
  1105. *修改日誌:
  1106. *[2006-3-117:19]Ver.1.00
  1107. 開始編寫;
  1108. *[2006-3-117:33]
  1109. 完成;
  1110. *[2006-3-914:10]
  1111. 測試通過;
  1112. /**/
  1113. /*********************************************************************************/
  1114. voidListenToTouch(intcomPort,intbuttonData[],floatparameterData[],intDOData[],floatAIData[])
  1115. {
  1116. intkind=1;/*adjust*/
  1117. unsignedshortparameterNum=PARAMETER_DATA_NUM;
  1118. unsignedshortparameterAddr=PARAMETER_MODBUS_ADDR_1;/*adjust*/
  1119. unsignedshortbuttonNum=BUTTON_DATA_NUM;
  1120. unsignedshortbuttonAddr=BUTTON_MODBUS_ADDR_1;/*adjust*/
  1121. unsignedshortDONum=DIGITAL_OUT_DATA_NUM;
  1122. unsignedshortDOAddr=DO_MODBUS_ADDR_1;/*adjust*/
  1123. unsignedshortAINum=ANALOG_IN_DATA_NUM;
  1124. unsignedshortAIAddr=AI_MODBUS_ADDR_1;/*adjust*/
  1125. intbuttonNVRAMAddr=BUTTON_ADDR_1;/*adjust*/
  1126. intparameterEEPROMAddr=EEPROM_BLOCK;/*adjust*/
  1127. staticunsignedcharoldModWordBack[MOD_STR_MAX_LEN]={'/0'};/*以前的str*/
  1128. staticunsignedcharoldModBitBack[MOD_STR_MAX_LEN]={'/0'};
  1129. staticintresponseFromTouch_init=0;/*首次初始化*/
  1130. /*staticintresponseFromTouch_P=0;
  1131. staticintresponseFromTouch_Btn=0;
  1132. staticintoldButtonData[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1133. staticintoldDOData[30]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1134. */
  1135. intresponse;/*test*/
  1136. /*Print("/n/n");*/
  1137. if(0==responseFromTouch_init)/*第一次寫入Touch_Btn失敗重新寫*/
  1138. {
  1139. /*responseFromTouch_Btn=WriteMultipleBitToModBusSlave(comPort,buttonData,buttonNum,buttonAddr);
  1140. */response=WriteAllBitByBitToModBusSlave(comPort,buttonData,buttonNum,buttonAddr);
  1141. /*bitmcpy(oldButtonData,buttonData,buttonNum);*/
  1142. }
  1143. else
  1144. {
  1145. /*讀取按鈕值*/
  1146. response=ReadBitFromModBusSlave(kind,comPort,buttonData,buttonNum,buttonAddr,buttonNVRAMAddr,oldModBitBack);
  1147. }
  1148. DelayMs(50);
  1149. /*Print("rb%d",response);*/
  1150. if(0==responseFromTouch_init)/*第一次寫入Touch_P失敗重新寫*/
  1151. {
  1152. responseFromTouch_init=WriteMultipleWordToModBusSlave(comPort,parameterData,parameterNum,parameterAddr);
  1153. response=responseFromTouch_init;
  1154. /*responseFromTouch_P=1;/*test*/
  1155. }
  1156. else
  1157. {
  1158. /*讀取設定引數*/
  1159. response=ReadWordFromModBusSlave(kind,comPort,parameterData,parameterNum,parameterAddr,parameterEEPROMAddr,oldModWordBack);
  1160. }
  1161. /*Print("p%d",responseFromTouch_P);
  1162. Print("rp%d",response);*/
  1163. DelayMs(2);
  1164. /*寫入開關量*/
  1165. /*response=WriteMultipleBitToModBusSlave(comPort,DOData,DONum,DOAddr);
  1166. */
  1167. response=WriteAllBitByBitToModBusSlave(comPort,DOData,DONum,DOAddr);
  1168. /*bitmcpy(oldDOData,DOData,DONum);*/
  1169. /*Print("d%d",response);*/
  1170. DelayMs(40);
  1171. /*寫入取樣值*/
  1172. response=WriteMultipleWordToModBusSlave(comPort,AIData,AINum,AIAddr);
  1173. /*Print("a%d",response);*/
  1174. if(response);
  1175. }
  1176. /*-------------------------------------------------------------------------------*/