PLCManagementClass.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. using CCDCount.DLL.Tools;
  2. using CCDCount.MODEL.PlcModel;
  3. using System;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. namespace CCDCount.DLL
  7. {
  8. public class PLCManagementClass
  9. {
  10. private bool isConnect = false;
  11. public bool IsConnect
  12. {
  13. get
  14. {
  15. return isConnect;
  16. }
  17. }
  18. public ModbusTcpClient modbusTcpClient = new ModbusTcpClient();
  19. public PLCManagementClass(string ipAddress)
  20. {
  21. //ConnectModbus("127.0.0.1");
  22. ConnectModbus(ipAddress);
  23. //ConnectModbus("192.168.1.88");
  24. }
  25. public void ConnectModbus(string ipAddress)
  26. {
  27. if (!modbusTcpClient.Connect(ipAddress))
  28. {
  29. FaultLog.RecordErrorMessage($"Modbus通讯连接失败,目标IP:{ipAddress}");
  30. isConnect = false;
  31. return;
  32. }
  33. isConnect = true;
  34. }
  35. /// <summary>
  36. /// 归零置为True
  37. /// </summary>
  38. public void ReturnToZeroToTrue()
  39. {
  40. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 0, values: true);
  41. }
  42. /// <summary>
  43. /// 归零置为False
  44. /// </summary>
  45. public void ReturnToZeroToFalse()
  46. {
  47. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 0, values: false);
  48. }
  49. /// <summary>
  50. /// 复位置为True
  51. /// </summary>
  52. public void RepositionToTrue()
  53. {
  54. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 1, values: true);
  55. }
  56. /// <summary>
  57. /// 复位置为False
  58. /// </summary>
  59. public void RepositionToFalse()
  60. {
  61. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 1, values: false);
  62. }
  63. /// <summary>
  64. /// 轴停止置为True
  65. /// </summary>
  66. public void ShaftStopsToTrue()
  67. {
  68. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 2, values: true);
  69. }
  70. /// <summary>
  71. /// 轴停止置为False
  72. /// </summary>
  73. public void ShaftStopsToFalse()
  74. {
  75. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 2, values: false);
  76. }
  77. /// <summary>
  78. /// 正转点动置为True
  79. /// </summary>
  80. public void ForwardRotatingJogToTrue()
  81. {
  82. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 3, values: true);
  83. }
  84. /// <summary>
  85. /// 正转点动置为False
  86. /// </summary>
  87. public void ForwardRotatingJogToFalse()
  88. {
  89. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 3, values: false);
  90. }
  91. /// <summary>
  92. /// 反转点动置为True
  93. /// </summary>
  94. public void ReversalReverseJogToTrue()
  95. {
  96. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 4, values: true);
  97. }
  98. /// <summary>
  99. /// 反转点动置为False
  100. /// </summary>
  101. public void ReversalReverseJogToFalse()
  102. {
  103. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 4, values: false);
  104. }
  105. /// <summary>
  106. /// 速度运行设置为True
  107. /// </summary>
  108. public void SpeedRunToTrue()
  109. {
  110. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 5, values: true);
  111. }
  112. /// <summary>
  113. /// 速度运行设置为False
  114. /// </summary>
  115. public void SpeedRunToFalse()
  116. {
  117. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 5, values: false);
  118. }
  119. /// <summary>
  120. /// 启动设置为True
  121. /// </summary>
  122. public void InitiateToTrue()
  123. {
  124. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 6, values: true);
  125. }
  126. /// <summary>
  127. /// 启动设置为False
  128. /// </summary>
  129. public void InitiateToFalse()
  130. {
  131. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 6, values: false);
  132. }
  133. /// <summary>
  134. /// 停止设置为True
  135. /// </summary>
  136. public void StopToTrue()
  137. {
  138. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 7, values: true);
  139. }
  140. /// <summary>
  141. /// 停止设置为False
  142. /// </summary>
  143. public void StopToFalse()
  144. {
  145. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 7, values: false);
  146. }
  147. /// <summary>
  148. /// 设备运行设置为True
  149. /// </summary>
  150. public void EquipmentOperationToTrue()
  151. {
  152. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 8, values: true);
  153. }
  154. /// <summary>
  155. /// 设备运行设置为False
  156. /// </summary>
  157. public void EquipmentOperationToFalse()
  158. {
  159. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 8, values: false);
  160. }
  161. /// <summary>
  162. /// 写入完成设置为True
  163. /// </summary>
  164. public void WriteDoneToTrue()
  165. {
  166. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 11, values: true);
  167. }
  168. /// <summary>
  169. /// 写入一级震台启动为True
  170. /// </summary>
  171. public void SwitchLevelOneVibrationTable()
  172. {
  173. bool Enable = ReadLevelOneVibrationTable();
  174. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 25, values: !Enable);
  175. }
  176. /// <summary>
  177. /// 二级震台启动
  178. /// </summary>
  179. public void SwitchLevelTwoVibrationTable()
  180. {
  181. bool Enable = ReadLevelTwoVibrationTable();
  182. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 26, values: !Enable);
  183. }
  184. /// <summary>
  185. /// 三级震台启动
  186. /// </summary>
  187. public void SwitchLevelThreeVibrationTable()
  188. {
  189. bool Enable = ReadLevelThreeVibrationTable();
  190. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 27, values: !Enable);
  191. }
  192. /// <summary>
  193. /// 设备上升
  194. /// </summary>
  195. public void WriteDeviceUpToTrue()
  196. {
  197. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 28, values: true);
  198. }
  199. public void WriteDeviceUpToFalse()
  200. {
  201. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 28, values: false);
  202. }
  203. /// <summary>
  204. /// 设备下降
  205. /// </summary>
  206. public void WriteDeviceDownToTrue()
  207. {
  208. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 29, values: true);
  209. }
  210. public void WriteDeviceDownToFalse()
  211. {
  212. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 29, values: false);
  213. }
  214. /// <summary>
  215. /// 传送带启动
  216. /// </summary>
  217. public void SwitchTransferStart()
  218. {
  219. bool Enable = ReadTransferStart();
  220. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 30, values: !Enable);
  221. }
  222. /// <summary>
  223. /// 气锁关
  224. /// </summary>
  225. public void SwitchAirValveClose()
  226. {
  227. bool Enable = ReadAirValveClose();
  228. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 31, values: !Enable);
  229. }
  230. /// <summary>
  231. /// 送瓶轮复位
  232. /// </summary>
  233. public void WriteBottleFeedingWheelResetToTrue()
  234. {
  235. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 32, values: true);
  236. }
  237. public void WriteBottleFeedingWheelResetToFalse()
  238. {
  239. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 32, values: false);
  240. }
  241. /// <summary>
  242. /// 送瓶轮回零
  243. /// </summary>
  244. public void WriteBottleFeedingWheelReturnToZeroToTrue()
  245. {
  246. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 33, values: true);
  247. }
  248. public void WriteBottleFeedingWheelReturnToZeroToFalse()
  249. {
  250. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 33, values: false);
  251. }
  252. /// <summary>
  253. /// 送瓶轮轴停止
  254. /// </summary>
  255. public void WriteBottleFeedingWheelShaftStopsToTrue()
  256. {
  257. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 34, values: true);
  258. }
  259. public void WriteBottleFeedingWheelShaftStopsToFalse()
  260. {
  261. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 34, values: false);
  262. }
  263. /// <summary>
  264. /// 送瓶轮正转点动
  265. /// </summary>
  266. public void WriteBottleFeedingWheelPositiveRotationJogToTrue()
  267. {
  268. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 36, values: true);
  269. }
  270. public void WriteBottleFeedingWheelPositiveRotationJogToFalse()
  271. {
  272. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 36, values: false);
  273. }
  274. /// <summary>
  275. /// 送瓶轮反转点动
  276. /// </summary>
  277. public void WriteBottleFeedingWheelReverseRotationJogToTrue()
  278. {
  279. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 37, values: true);
  280. }
  281. public void WriteBottleFeedingWheelReverseRotationJogToFalse()
  282. {
  283. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 37, values: false);
  284. }
  285. /// <summary>
  286. /// 送瓶轮使能
  287. /// </summary>
  288. public void SwitchBottleFeedingWheelEnable()
  289. {
  290. bool Enable = ReadBottleFeedingWheelEnable();
  291. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 38, values: !Enable);
  292. }
  293. /// <summary>
  294. /// 读取写入完成
  295. /// </summary>
  296. /// <returns></returns>
  297. public bool ReadAllowWrite()
  298. {
  299. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress:12, numRegisters: 1)[0];
  300. }
  301. /// <summary>
  302. /// 读取使能完成
  303. /// </summary>
  304. /// <returns></returns>
  305. public bool ReadEnableCompletion()
  306. {
  307. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 14, numRegisters: 1)[0];
  308. }
  309. /// <summary>
  310. /// 读取归零完成
  311. /// </summary>
  312. /// <returns></returns>
  313. public bool ReadReturnToZeroCompletion()
  314. {
  315. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 15, numRegisters: 1)[0];
  316. }
  317. /// <summary>
  318. /// 读取停止完成
  319. /// </summary>
  320. /// <returns></returns>
  321. public bool ReadStopCompletion()
  322. {
  323. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 16, numRegisters: 1)[0];
  324. }
  325. /// <summary>
  326. /// 读取速度运行中
  327. /// </summary>
  328. public bool ReadSpeedRunning()
  329. {
  330. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 17, numRegisters: 1)[0];
  331. }
  332. /// <summary>
  333. /// 读取归零中
  334. /// </summary>
  335. /// <returns></returns>
  336. public bool ReadReturnToZero()
  337. {
  338. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 18, numRegisters: 1)[0];
  339. }
  340. /// <summary>
  341. /// 读取中转阀开定位完成
  342. /// </summary>
  343. /// <returns></returns>
  344. public bool ReadTransferValveOpenCompletion()
  345. {
  346. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 19, numRegisters: 1)[0];
  347. }
  348. /// <summary>
  349. /// 读取中转阀关定位完成
  350. /// </summary>
  351. public bool ReadTransferValveCloseCompletion()
  352. {
  353. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 20, numRegisters: 1)[0];
  354. }
  355. /// <summary>
  356. /// 读取允许数据交换
  357. /// </summary>
  358. public bool ReadAllowsDataExchange()
  359. {
  360. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 21, numRegisters: 1)[0];
  361. }
  362. /// <summary>
  363. /// 读取暂停数据交换
  364. /// </summary>
  365. public bool ReadPauseDataExchange()
  366. {
  367. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 22, numRegisters: 1)[0];
  368. }
  369. /// <summary>
  370. /// 缓存计数延时完成
  371. /// </summary>
  372. /// <returns></returns>
  373. public bool ReadCacheCountDelayed()
  374. {
  375. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 23, numRegisters: 1)[0];
  376. }
  377. /// <summary>
  378. /// 读取使能
  379. /// </summary>
  380. /// <returns></returns>
  381. public bool ReadEnable()
  382. {
  383. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 24, numRegisters: 1)[0];
  384. }
  385. /// <summary>
  386. /// 读取一级震台启动
  387. /// </summary>
  388. public bool ReadLevelOneVibrationTable()
  389. {
  390. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 25, numRegisters: 1)[0];
  391. }
  392. /// <summary>
  393. /// 读取二级震台启动
  394. /// </summary>
  395. public bool ReadLevelTwoVibrationTable()
  396. {
  397. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 26, numRegisters: 1)[0];
  398. }
  399. /// <summary>
  400. /// 读取三级震台启动
  401. /// </summary>
  402. public bool ReadLevelThreeVibrationTable()
  403. {
  404. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 27, numRegisters: 1)[0];
  405. }
  406. /// <summary>
  407. /// 读取传送带启动
  408. /// </summary>
  409. public bool ReadTransferStart()
  410. {
  411. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 30, numRegisters: 1)[0];
  412. }
  413. /// <summary>
  414. /// 读取气锁关
  415. /// </summary>
  416. public bool ReadAirValveClose()
  417. {
  418. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 31, numRegisters: 1)[0];
  419. }
  420. /// <summary>
  421. /// 读取送瓶轮使能
  422. /// </summary>
  423. public bool ReadBottleFeedingWheelEnable()
  424. {
  425. return modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 38, numRegisters: 1)[0];
  426. }
  427. /// <summary>
  428. /// 读取所有状态
  429. /// </summary>
  430. public PlcStaticModelClass ReadAllState()
  431. {
  432. PlcStaticModelClass result = null;
  433. bool[] Readresult = modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 14, numRegisters: 11);
  434. if (Readresult == null) return null;
  435. result = new PlcStaticModelClass()
  436. {
  437. EnableCompletion = Readresult[0],
  438. ReturnToZeroCompletion = Readresult[1],
  439. StopCompletion = Readresult[2],
  440. SpeedRunning = Readresult[3],
  441. ReturnToZero = Readresult[4],
  442. TransferValveOpenCompletion = Readresult[5],
  443. TransferValveCloseCompletion = Readresult[6],
  444. AllowsDataExchange = Readresult[7],
  445. PauseDataExchange = Readresult[8],
  446. CacheCountDelayed = Readresult[9],
  447. Enable = Readresult[10]
  448. };
  449. Readresult = modbusTcpClient.ReadCoilsRegister(slaveId: 1, startAddress: 35, numRegisters: 5);
  450. result.BottleFeedingWheelEnableCompletion = Readresult[0];
  451. result.BottleFeedingWheelStopCompletion = Readresult[4];
  452. return result;
  453. }
  454. /// <summary>
  455. /// 写入速度模式运行速度
  456. /// </summary>
  457. /// <param name="Value"></param>
  458. public void WriteSpeedModeRunningSpeed(float Value)
  459. {
  460. modbusTcpClient.WriteMultipleReals(1, 110, new float[] { Value }, "ABCD");
  461. }
  462. /// <summary>
  463. /// 写入点动速度
  464. /// </summary>
  465. /// <param name="Value"></param>
  466. public void WriteJogSpeed(float Value)
  467. {
  468. modbusTcpClient.WriteMultipleReals(1, 114, new float[] { Value }, "ABCD");
  469. }
  470. /// <summary>
  471. /// 写入罐装设定值
  472. /// </summary>
  473. /// <param name="Value"></param>
  474. public void WriteBottValueSet(UInt16 Value)
  475. {
  476. modbusTcpClient.WriteSingleRegister(1, 118, Value);
  477. }
  478. /// <summary>
  479. /// 写入中转阀打开速度
  480. /// </summary>
  481. /// <param name="Value"></param>
  482. public void WriteTransferValveOpeningSpeed(float Value)
  483. {
  484. modbusTcpClient.WriteMultipleReals(1, 122, new float[] { Value }, "ABCD");
  485. }
  486. /// <summary>
  487. /// 写入中转阀打开时间
  488. /// </summary>
  489. public void WriteTransferValveOpeningTime(UInt32 Value)
  490. {
  491. modbusTcpClient.WriteSingleInt32(1, 126, Value);
  492. }
  493. /// <summary>
  494. /// 写入罐装减速值
  495. /// </summary>
  496. /// <param name="Value"></param>
  497. public void WriteBottingDecelerationValue(ushort Value)
  498. {
  499. modbusTcpClient.WriteSingleRegister(1, 130, Value);
  500. }
  501. /// <summary>
  502. /// 写入缓存减速值
  503. /// </summary>
  504. /// <param name="Value"></param>
  505. public void WriteCacheDecelerationValue(ushort Value)
  506. {
  507. modbusTcpClient.WriteSingleRegister(1, 134, Value);
  508. }
  509. /// <summary>
  510. /// 写入缓存计时延时时间
  511. /// </summary>
  512. /// <param name="Value"></param>
  513. public void WriteCacheCountDelayTiming(UInt32 Value)
  514. {
  515. modbusTcpClient.WriteSingleInt32(1, 138, Value);
  516. }
  517. /// <summary>
  518. /// 写入缓存停机值
  519. /// </summary>
  520. /// <param name="Value"></param>
  521. public void WriteCacheShutdownValue(ushort Value)
  522. {
  523. modbusTcpClient.WriteSingleRegister(1, 142, Value);
  524. }
  525. /// <summary>
  526. /// 写入罐装料筒震台高速值
  527. /// </summary>
  528. /// <param name="Value"></param>
  529. public void WriteBottingMaterialCylinderVibrationTableHighSpeedValue(ushort Value)
  530. {
  531. modbusTcpClient.WriteSingleRegister(1, 146, Value);
  532. }
  533. /// <summary>
  534. /// 写入罐装过滤震台高速值
  535. /// </summary>
  536. /// <param name="Value"></param>
  537. public void WriteBottingFilterVibrationTableHighSpeedValue(ushort Value)
  538. {
  539. modbusTcpClient.WriteSingleRegister(1, 150, Value);
  540. }
  541. /// <summary>
  542. /// 写入罐装计数震台高速值
  543. /// </summary>
  544. /// <param name="Value"></param>
  545. public void WriteBottingCountVibrationTableHighSpeedValue(ushort Value)
  546. {
  547. modbusTcpClient.WriteSingleRegister(1, 154, Value);
  548. }
  549. /// <summary>
  550. /// 写入罐装料筒震台减速值
  551. /// </summary>
  552. /// <param name="Value"></param>
  553. public void WriteBottingMaterialCylinderVibrationTableDecelerationSpeedValue(ushort Value)
  554. {
  555. modbusTcpClient.WriteSingleRegister(1, 158, Value);
  556. }
  557. /// <summary>
  558. /// 写入罐装过滤震台减速值
  559. /// </summary>
  560. /// <param name="Value"></param>
  561. public void WriteBottingFilterVibrationTableDecelerationSpeedValue(ushort Value)
  562. {
  563. modbusTcpClient.WriteSingleRegister(1, 162, Value);
  564. }
  565. /// <summary>
  566. /// 写入罐装计数震台减速值
  567. /// </summary>
  568. /// <param name="Value"></param>
  569. public void WriteBottingCountVibrationTableDecelerationSpeedValue(ushort Value)
  570. {
  571. modbusTcpClient.WriteSingleRegister(1, 166, Value);
  572. }
  573. /// <summary>
  574. /// 写入缓存料筒震台高速值
  575. /// </summary>
  576. /// <param name="Value"></param>
  577. public void WriteCacheMaterialCylinderVibrationTableHighSpeedValue(ushort Value)
  578. {
  579. modbusTcpClient.WriteSingleRegister(1, 170, Value);
  580. }
  581. /// <summary>
  582. /// 写入缓存过滤震台高速值
  583. /// </summary>
  584. /// <param name="Value"></param>
  585. public void WriteCacheFilterVibrationTableHighSpeedValue(ushort Value)
  586. {
  587. modbusTcpClient.WriteSingleRegister(1, 174, Value);
  588. }
  589. /// <summary>
  590. /// 写入缓存计数震台高速值
  591. /// </summary>
  592. /// <param name="Value"></param>
  593. public void WriteCacheCountVibrationTableHighSpeedValue(ushort Value)
  594. {
  595. modbusTcpClient.WriteSingleRegister(1, 178, Value);
  596. }
  597. /// <summary>
  598. /// 写入缓存料筒震台减速值
  599. /// </summary>
  600. /// <param name="Value"></param>
  601. public void WriteCacheMaterialCylinderVibrationTableDecelerationSpeedValue(ushort Value)
  602. {
  603. modbusTcpClient.WriteSingleRegister(1, 182, Value);
  604. }
  605. /// <summary>
  606. /// 写入缓存过滤震台减速值
  607. /// </summary>
  608. /// <param name="Value"></param>
  609. public void WriteCacheFilterVibrationTableDecelerationSpeedValue(ushort Value)
  610. {
  611. modbusTcpClient.WriteSingleRegister(1, 186, Value);
  612. }
  613. /// <summary>
  614. /// 写入缓存计数震台减速值
  615. /// </summary>
  616. /// <param name="Value"></param>
  617. public void WriteCacheCountVibrationTableDecelerationSpeedValue(ushort Value)
  618. {
  619. modbusTcpClient.WriteSingleRegister(1, 190, Value);
  620. }
  621. /// <summary>
  622. /// 写入闸门打开延时
  623. /// </summary>
  624. /// <param name="Value"></param>
  625. public void WriteGateOpeningDelay(UInt32 Value)
  626. {
  627. modbusTcpClient.WriteSingleInt32(1, 194, Value);
  628. }
  629. /// <summary>
  630. /// 写入回零偏移值
  631. /// </summary>
  632. /// <param name="Value"></param>
  633. public void WriteReturnToZeroOffsetValue(float Value)
  634. {
  635. modbusTcpClient.WriteSingleReal(1, 198, Value);
  636. }
  637. /// <summary>
  638. /// 写入回零偏移速度
  639. /// </summary>
  640. /// <param name="Value"></param>
  641. public void WriteReturnToZeroOffsetSpeed(float Value)
  642. {
  643. modbusTcpClient.WriteSingleReal(1, 202, Value);
  644. }
  645. /// <summary>
  646. /// 写入中转阀关闭速度
  647. /// </summary>
  648. /// <param name="Value"></param>
  649. public void WriteTransferValveClosingSpeed(float Value)
  650. {
  651. modbusTcpClient.WriteSingleReal(1, 206, Value);
  652. }
  653. /// <summary>
  654. /// 写入中转阀开位置
  655. /// </summary>
  656. /// <param name="Value"></param>
  657. public void WriteTransferValveOpenPosition(float Value)
  658. {
  659. modbusTcpClient.WriteSingleReal(1, 210, Value);
  660. }
  661. /// <summary>
  662. /// 写入中转阀关位置
  663. /// </summary>
  664. /// <param name="Value"></param>
  665. public void WriteTransferValveClosePosition(float Value)
  666. {
  667. modbusTcpClient.WriteSingleReal(1, 214, Value);
  668. }
  669. /// <summary>
  670. /// 写入空气阀开启延时
  671. /// </summary>
  672. public void WriteAirValveOpeningDelay(UInt32 Value)
  673. {
  674. modbusTcpClient.WriteSingleInt32(1, 218, Value);
  675. }
  676. /// <summary>
  677. /// 写入罐装停机值
  678. /// </summary>
  679. /// <param name="Value"></param>
  680. public void WriteBottlingShutdownValue(ushort Value)
  681. {
  682. modbusTcpClient.WriteSingleRegister(1, 230, Value);
  683. }
  684. /// <summary>
  685. /// 写入罐装停机时间
  686. /// </summary>
  687. /// <param name="Value"></param>
  688. public void WriteBottlingShutdownTime(UInt32 Value)
  689. {
  690. modbusTcpClient.WriteSingleInt32(1, 234, Value);
  691. }
  692. /// <summary>
  693. /// 读取所有参数(参数数据表)
  694. /// </summary>
  695. public PlcParaModelClass ReadAllPara()
  696. {
  697. PlcParaModelClass result = null;
  698. var results = modbusTcpClient.ReadHoldingRegisters(slaveId: 1, startAddress: 110, numRegisters: 124);
  699. if (results == null) return null;
  700. result = new PlcParaModelClass()
  701. {
  702. SpeedModeRunningSpeed = GetFloatFromRegisters(results.Take(2).ToArray()),
  703. JogSpeed = GetFloatFromRegisters(results.Skip(4).Take(2).ToArray()),
  704. BottValueSet = results[8],
  705. TransferValveOpeningSpeed = GetFloatFromRegisters(results.Skip(12).Take(2).ToArray()),
  706. TransferValveOpeningTime = GetInt32FromRegisters(results.Skip(16).Take(2).ToArray()),
  707. BottingDecelerationValue = results[20],
  708. CacheDecelerationValue = results[24],
  709. CacheCountDelayTiming = GetInt32FromRegisters(results.Skip(28).Take(2).ToArray()),
  710. CacheShutdownValue = results[32],
  711. BottingMaterialCylinderVibrationTableHighSpeedValue = results[36],
  712. BottingFilterVibrationTableHighSpeedValue = results[40],
  713. BottingCountVibrationTableHighSpeedValue = results[44],
  714. BottingMaterialCylinderVibrationTableDecelerationSpeedValue = results[48],
  715. BottingFilterVibrationTableDecelerationSpeedValue = results[52],
  716. BottingCountVibrationTableDecelerationSpeedValue = results[56],
  717. CacheMaterialCylinderVibrationTableHighSpeedValue = results[60],
  718. CacheFilterVibrationTableHighSpeedValue = results[64],
  719. CacheCountVibrationTableHighSpeedValue = results[68],
  720. CacheMaterialCylinderVibrationTableDecelerationSpeedValue = results[72],
  721. CacheFilterVibrationTableDecelerationSpeedValue = results[76],
  722. CacheCountVibrationTableDecelerationSpeedValue = results[80],
  723. GateOpeningDelay = GetInt32FromRegisters(results.Skip(84).Take(2).ToArray()),
  724. ReturnToZeroOffsetValue = GetFloatFromRegisters(results.Skip(88).Take(2).ToArray()),
  725. ReturnToZeroOffsetSpeed = GetFloatFromRegisters(results.Skip(92).Take(2).ToArray()),
  726. TransferValveClosingSpeed = GetFloatFromRegisters(results.Skip(96).Take(2).ToArray()),
  727. TransferValveOpenPosition = GetFloatFromRegisters(results.Skip(100).Take(2).ToArray()),
  728. TransferValveClosePosition = GetFloatFromRegisters(results.Skip(104).Take(2).ToArray()),
  729. AirValveOpeningDelay = GetInt32FromRegisters(results.Skip(108).Take(2).ToArray()),
  730. BottlingShutdownValue = results[120],
  731. BottlingShutdownTime = modbusTcpClient.ReadHoldingRegistersAsInt32(slaveId: 1, startAddress: 234, count: 1)[0],
  732. };
  733. return result;
  734. }
  735. /// <summary>
  736. /// 获取16位寄存器中的32Uint位值
  737. /// </summary>
  738. private UInt32 GetInt32FromRegisters(ushort[] registers)
  739. {
  740. ushort highRegister = registers[0];
  741. ushort lowRegister = registers[1];
  742. // 根据字节序组合成32位值
  743. byte[] bytes = new byte[4];
  744. if (BitConverter.IsLittleEndian)
  745. {
  746. bytes[3] = (byte)((highRegister >> 8) & 0xFF);
  747. bytes[2] = (byte)(highRegister & 0xFF);
  748. bytes[1] = (byte)((lowRegister >> 8) & 0xFF);
  749. bytes[0] = (byte)(lowRegister & 0xFF);
  750. }
  751. else
  752. {
  753. bytes[0] = (byte)((highRegister >> 8) & 0xFF);
  754. bytes[1] = (byte)(highRegister & 0xFF);
  755. bytes[2] = (byte)((lowRegister >> 8) & 0xFF);
  756. bytes[3] = (byte)(lowRegister & 0xFF);
  757. }
  758. // 转换为float
  759. return (UInt32)BitConverter.ToInt32(bytes, 0);
  760. }
  761. /// <summary>
  762. /// 获取16位寄存器中的32Float位值
  763. /// </summary>
  764. private float GetFloatFromRegisters(ushort[] registers)
  765. {
  766. string byteOrder = "ABCD";
  767. ushort highRegister = registers[0];
  768. ushort lowRegister = registers[1];
  769. byte[] bytes = ConvertToByteArray(lowRegister, highRegister, byteOrder);
  770. return BitConverter.ToSingle(bytes, 0); ;
  771. }
  772. /// <summary>
  773. /// 将寄存器数组换为字节数组转(根据指定字节序)
  774. /// </summary>
  775. /// <param name="lowRegister">低位寄存器</param>
  776. /// <param name="highRegister">高位寄存器</param>
  777. /// <param name="byteOrder">字节序</param>
  778. /// <returns>字节数组</returns>
  779. private byte[] ConvertToByteArray(ushort lowRegister, ushort highRegister, string byteOrder)
  780. {
  781. byte[] bytes = new byte[4];
  782. switch (byteOrder.ToUpper())
  783. {
  784. case "ABCD": // 大端序
  785. bytes[0] = (byte)(highRegister >> 8);
  786. bytes[1] = (byte)(highRegister & 0xFF);
  787. bytes[2] = (byte)(lowRegister >> 8);
  788. bytes[3] = (byte)(lowRegister & 0xFF);
  789. break;
  790. case "CDAB": // 小端序
  791. bytes[0] = (byte)(lowRegister >> 8);
  792. bytes[1] = (byte)(lowRegister & 0xFF);
  793. bytes[2] = (byte)(highRegister >> 8);
  794. bytes[3] = (byte)(highRegister & 0xFF);
  795. break;
  796. case "BADC":
  797. bytes[0] = (byte)(highRegister & 0xFF);
  798. bytes[1] = (byte)(highRegister >> 8);
  799. bytes[2] = (byte)(lowRegister & 0xFF);
  800. bytes[3] = (byte)(lowRegister >> 8);
  801. break;
  802. case "DCBA":
  803. bytes[0] = (byte)(lowRegister & 0xFF);
  804. bytes[1] = (byte)(lowRegister >> 8);
  805. bytes[2] = (byte)(highRegister & 0xFF);
  806. bytes[3] = (byte)(highRegister >> 8);
  807. break;
  808. default:
  809. // 默认使用ABCD顺序
  810. bytes[0] = (byte)(highRegister >> 8);
  811. bytes[1] = (byte)(highRegister & 0xFF);
  812. bytes[2] = (byte)(lowRegister >> 8);
  813. bytes[3] = (byte)(lowRegister & 0xFF);
  814. break;
  815. }
  816. return bytes;
  817. }
  818. /// <summary>
  819. /// 切换使能
  820. /// </summary>
  821. public void SwitchEnable()
  822. {
  823. bool Enable = ReadEnable();
  824. modbusTcpClient.WriteCoilsRegister(slaveId: 1, CoilsAddress: 24, values: !Enable);
  825. }
  826. }
  827. }