终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

679 lines
35 KiB

  1. //#define Debug
  2. using Aop.Api.Domain;
  3. using BPA.Communication;
  4. using BPA.Helper;
  5. using BPASmartClient.CustomResource.Pages.Model;
  6. using BPASmartClient.CustomResource.UserControls;
  7. using BPASmartClient.CustomResource.UserControls.MessageShow;
  8. using BPASmartClient.DosingSystem.Model;
  9. using BPASmartClient.DosingSystem.ViewModel;
  10. using BPASmartClient.Model;
  11. using Google.Protobuf.WellKnownTypes;
  12. using Org.BouncyCastle.Asn1;
  13. //using BPASmartClient.Modbus;
  14. using System;
  15. using System.Collections.Concurrent;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.Diagnostics;
  19. using System.Linq;
  20. using System.Net.NetworkInformation;
  21. using System.Reflection;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. using System.Windows.Forms;
  25. namespace BPASmartClient.DosingSystem
  26. {
  27. public class DeviceInquire
  28. {
  29. int stockCount;//料仓数
  30. private volatile static DeviceInquire _Instance;
  31. public static DeviceInquire GetInstance => _Instance ?? (_Instance = new DeviceInquire());
  32. private DeviceInquire() { }
  33. string IPSegment = "192.168.2.";
  34. /// <summary>
  35. /// 设备列表集合
  36. /// </summary>
  37. public ConcurrentDictionary<string, DeviceStatus> DeviceLists = new ConcurrentDictionary<string, DeviceStatus>();
  38. List<string> InvalidIP = new List<string>();//无效 IP 集合
  39. List<string> IPLists = new List<string>();//启动 Ping 任务IP集合
  40. ConcurrentQueue<string> IPQueues = new ConcurrentQueue<string>();//pincomplete 完成队列
  41. /// <summary>
  42. /// 记录每个IP对应的设备是否已经执行数据清零,避免重连后再次清零。
  43. /// </summary>
  44. private ConcurrentDictionary<string, bool> IsClearData = new();
  45. public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  46. public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
  47. public ObservableCollection<Devices> devices { get; set; } = new ObservableCollection<Devices>();
  48. private void DeviceDataInit()
  49. {
  50. TaskManage.GetInstance.StartLong(new Action(() =>
  51. {
  52. for (int i = 0; i < DeviceLists.Count; i++)
  53. {
  54. string deviceName = DeviceLists.ElementAt(i).Value.DeviceName;
  55. int TopIndex = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
  56. int BottomIndex = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
  57. if (TopIndex >= 0 && TopIndex < TopDeviceCurrentStatuses.Count)
  58. {
  59. TopDeviceCurrentStatuses.ElementAt(TopIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
  60. TopDeviceCurrentStatuses.ElementAt(TopIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  61. }
  62. if (BottomIndex >= 0 && BottomIndex < BottomDeviceCurrentStatuses.Count)
  63. {
  64. BottomDeviceCurrentStatuses.ElementAt(BottomIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
  65. BottomDeviceCurrentStatuses.ElementAt(BottomIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  66. }
  67. int deviceIndex = Array.FindIndex(devices.ToArray(), p => p.IpAddress == DeviceLists.ElementAt(i).Key/* && p.DeviceName != DeviceLists.ElementAt(i).Value.DeviceName && p.DeviceNum != DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum*/);
  68. if (deviceIndex >= 0 && deviceIndex < devices.Count)
  69. {
  70. devices.ElementAt(deviceIndex).DeviceName = DeviceLists.ElementAt(i).Value.DeviceName;
  71. devices.ElementAt(deviceIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
  72. }
  73. }
  74. Thread.Sleep(200);
  75. }), "设备状态监听", true);
  76. }
  77. public void Init()
  78. {
  79. if (!string.IsNullOrEmpty(Json<DevicePar>.Data.BaseParModel.NetworkSegAddress)) IPSegment = Json<DevicePar>.Data.BaseParModel.NetworkSegAddress;
  80. else Json<DevicePar>.Data.BaseParModel.NetworkSegAddress = IPSegment;
  81. stockCount = Json<DevicePar>.Data.BaseParModel.StockCount;
  82. //TestData();
  83. /*IpAddressLines();*/
  84. Task.Factory.StartNew(() =>
  85. {
  86. SimensSend.GetInstance.Connect();
  87. /*while(!clearValue)
  88. {
  89. clearValue = ClearData();
  90. }*/
  91. //SiemensDevice.GetInstance.Connect(Json<DevicePar>.Data.BaseParModel.DeviceAddress);
  92. });
  93. /* DeviceDataInit();*/
  94. TaskManage.GetInstance.StartLong(new Action(() =>
  95. {
  96. if (IPQueues.Count >= IPLists.Count)
  97. IpAddressLines();
  98. Thread.Sleep(5000);
  99. }), "配料机设备上线监听", true);
  100. }
  101. /// <summary>
  102. /// 清除所有数据
  103. /// </summary>
  104. /// <returns></returns>
  105. public bool ClearData()
  106. {
  107. if (SimensSend.GetInstance.SendSimens.IsReconnect)
  108. {
  109. var res = SimensSend.GetInstance.SendSimens.Write<ushort>("DB1.DBW840", (ushort)1).IsSuccess;
  110. Thread.Sleep(2000);
  111. bool result = false;
  112. if (res)
  113. {
  114. result = SimensSend.GetInstance.SendSimens.Write<ushort>("DB1.DBW840", (ushort)0).IsSuccess;
  115. }
  116. if (result)
  117. {
  118. App.Current.Dispatcher.Invoke(() => { NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"数据清除完成"); });
  119. }
  120. /* #region 2023-11-20-17新增:在软件重启时,自动清零设备上的配方执行数量等数据。
  121. var result1= modbusTcp.Write<ushort>(DeviceAddress.ZeroRecipeCount.ToAdd(), 1).IsSuccess;
  122. Thread.Sleep(500);
  123. var result2= modbusTcp.Write<ushort>(DeviceAddress.ZeroRecipeCount.ToAdd(), 0).IsSuccess;
  124. return result1 && result2;
  125. #endregion*/
  126. return result;
  127. }
  128. else
  129. {
  130. return false;
  131. }
  132. }
  133. public void Rescan()
  134. {
  135. InvalidIP.Clear();
  136. }
  137. public DeviceStatus GetDevice(string ip)
  138. {
  139. if (ip != null)
  140. {
  141. if (DeviceLists.ContainsKey(ip)) return DeviceLists[ip];
  142. else return new DeviceStatus();
  143. //var res = DeviceLists.Values.FirstOrDefault(p => p.IpAddress == ip);
  144. //if (res != null) return res;
  145. }
  146. return new DeviceStatus();
  147. }
  148. public List<DeviceStatus> GetDevice()
  149. {
  150. List<DeviceStatus> deviceStatuses = new List<DeviceStatus>();
  151. foreach (var device in DeviceLists)
  152. {
  153. deviceStatuses.Add(device.Value);
  154. }
  155. return deviceStatuses;
  156. }
  157. private void IpAddressLines()
  158. {
  159. IPLists.Clear();
  160. IPQueues.Clear();
  161. //TestData();
  162. /*for (int i = 1; i <= 255; i++)
  163. {
  164. if (!InvalidIP.Contains($"{IPSegment}{i}") && !DeviceLists.ContainsKey($"{IPSegment}{i}"))
  165. {
  166. string pattern = @"^(([1-9]\d?)|(1\d{2})|(2[01]\d)|(22[0-3]))(\.((1?\d\d?)|(2[04]/d)|(25[0-5]))){3}$";
  167. if (Regex.IsMatch($"{IPSegment}{i}", pattern))
  168. {
  169. IPLists.Add($"{IPSegment}{i}");
  170. }
  171. }
  172. }*/
  173. for (int i = 0; i < stockCount; i++)
  174. {
  175. IPLists.Add($"{IPSegment}{(i + 1) * 10}");
  176. //IPLists.Add($"{IPSegment}{this.devices[i].DeviceNum}");
  177. //2023-11-23:根据IP添加到记录字典。
  178. var ip = $"{IPSegment}{(i + 1) * 10}";
  179. if (!IsClearData.ContainsKey(ip))
  180. {
  181. IsClearData.TryAdd($"{IPSegment}{(i + 1) * 10}", false);
  182. }
  183. }
  184. IPLists.ForEach((item) =>
  185. {
  186. Ping myPing = new Ping();
  187. myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
  188. myPing.SendAsync(item, 1000, null);
  189. });
  190. }
  191. bool clearValue = false;
  192. private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
  193. {
  194. if (e.Reply != null && e.Reply.Status == IPStatus.Success)
  195. {
  196. string ip = e.Reply.Address.ToString();
  197. if (!DeviceLists.ContainsKey(ip))
  198. {
  199. DeviceStatus DS = new DeviceStatus();
  200. DS.modbusTcp.IsReconnect = false;
  201. #region 2023-11-23修改:在该IP第一次连接时清除设备的数据,后续重连时不清除。
  202. /*if (IsClearData.ContainsKey(ip) && IsClearData[ip] == false)
  203. {
  204. if (DS.ClearData())
  205. {
  206. //清除成功就置位。
  207. IsClearData[ip] = true;
  208. }
  209. } */
  210. //备注:2024-1-22修改,连接后整体数据清理
  211. #endregion
  212. DS.modbusTcp.Connected = new Action(() =>
  213. {
  214. var tt = DS.modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20);
  215. string DeviceName = DS.modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20).Content?.Trim()?.Replace(" ", "");//读取设备名称
  216. if (DeviceName.Length > 0)
  217. {
  218. DeviceLists.TryAdd(ip, DS);
  219. DeviceLists[ip].Init(DeviceName);
  220. DeviceLists[ip].modbusTcp.IsReconnect = false;
  221. App.Current.Dispatcher.Invoke(new Action(() =>
  222. {
  223. devices.Add(new Devices() { DeviceName = DeviceName, IpAddress = ip });
  224. //2023/7/3调整
  225. if (stockCount <= 8 || (stockCount > 8 && TopDeviceCurrentStatuses.Count <= stockCount / 2 + 1 && int.TryParse(ip.Split('.')[3], out int res) && res <= (stockCount / 2 + 1) * 10))
  226. TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
  227. else
  228. BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
  229. for (int i = 0; i < Json<LocalRecipe>.Data.Recipes.Count; i++)
  230. {
  231. for (int m = 0; m < Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.Count; m++)
  232. {
  233. if (Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp == ip)
  234. {
  235. Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).RawMaterialName = DeviceName;
  236. }
  237. }
  238. }
  239. if (Global.DeviceRawMaterials.Count > 0)
  240. {
  241. if (Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == DeviceName) == null)
  242. {
  243. Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
  244. }
  245. }
  246. else
  247. {
  248. Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
  249. }
  250. }));
  251. }
  252. else
  253. {
  254. //if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
  255. }
  256. });
  257. DS.modbusTcp.ConnectFail = new Action(() =>
  258. {
  259. //if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
  260. });
  261. DS.modbusTcp.Disconnect = new Action(() =>
  262. {
  263. if (InvalidIP.Contains(ip)) InvalidIP.Remove(ip);
  264. var res = devices.FirstOrDefault(P => P.IpAddress == ip);
  265. if (res != null && devices.Contains(res))
  266. {
  267. App.Current.Dispatcher.Invoke(new Action(() =>
  268. {
  269. devices.Remove(res);
  270. var item = Global.DeviceRawMaterials.FirstOrDefault(P => P.RawMaterialName == res.DeviceName);
  271. if (item != null) Global.DeviceRawMaterials.Remove(item);
  272. var topRes = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
  273. var bottomRes = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
  274. if (topRes != null) TopDeviceCurrentStatuses.Remove(topRes);
  275. if (bottomRes != null) BottomDeviceCurrentStatuses.Remove(bottomRes);
  276. }));
  277. }
  278. if (DeviceLists.ContainsKey(ip))
  279. {
  280. DeviceLists[ip].Dispose();
  281. var res11 = DeviceLists[ip];
  282. DeviceLists.TryRemove(ip, out res11);
  283. }
  284. });
  285. Task.Run(new Action(() =>
  286. {
  287. DS.modbusTcp.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = ip, Port = 502 });//PLC连接
  288. IPQueues.Enqueue(e.Reply.Address.ToString());
  289. }));
  290. }
  291. else IPQueues.Enqueue(e.Reply.Address.ToString());
  292. }
  293. else
  294. {
  295. if (e.Reply != null)
  296. IPQueues.Enqueue(e.Reply.Address.ToString());
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// 料仓状态(报警)获取及展示
  302. /// </summary>
  303. public class DeviceStatus
  304. {
  305. public int recipeC { get; set; }
  306. public int mrCount { get; set; }
  307. private bool start_down = false;
  308. public Action<int,int> startAction { get; set; }
  309. #region 对象属性声明
  310. public string DeviceName = String.Empty;
  311. public string IpAddress => modbusTcp.GetConfigPar().IpAddress;
  312. /// <summary>
  313. /// 设备状态
  314. /// </summary>
  315. public RawMaterialDeviceStatus deviceStatus { get; set; } = new RawMaterialDeviceStatus();
  316. public ModbusTcp modbusTcp = new ModbusTcp();
  317. public bool IsConnected => modbusTcp.IsConnected();
  318. public Task ResetAll() {
  319. return Task.Factory.StartNew(() => {
  320. if (deviceStatus.DeviceNum > 0) {
  321. int dnum = deviceStatus.DeviceNum;
  322. modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 1);
  323. Thread.Sleep(100);
  324. modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 0);
  325. modbusTcp.Write<ushort>(DeviceAddress.Start.ToAdd(), 0);
  326. SimensSend.GetInstance.SendSimens.Write<ushort>($"DB1.DBW{810 + 2 * (dnum - 1)}", 0);
  327. SimensSend.GetInstance.SendSimens.Write<int>($"DB1.DBW{710 + 4 * (dnum - 1)}", 0);
  328. }
  329. });
  330. }
  331. public Task Reset()
  332. {
  333. return Task.Factory.StartNew(() =>
  334. {
  335. ushort reset = 0;
  336. reset = reset.SetBitValue(1, true);
  337. modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), reset);
  338. Thread.Sleep(200);
  339. modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 0);
  340. int dnum = deviceStatus.DeviceNum;
  341. if (dnum > 0)
  342. {
  343. SimensSend.GetInstance.SendSimens.Write<bool>($"DB1.DBX70{(int)((dnum - 1) / 8)}.{(dnum - 1) % 8}", true);
  344. Thread.Sleep(100);
  345. SimensSend.GetInstance.SendSimens.Write<bool>($"DB1.DBX70{(int)((dnum - 1) / 8)}.{(dnum - 1) % 8}", false);
  346. }
  347. MessageNotify.GetInstance.ShowRunLog($"设备{DeviceName}配料状态:配料完成复位");
  348. });
  349. }
  350. public void Reset1()
  351. {
  352. modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 1);
  353. Thread.Sleep(200);
  354. modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 0);
  355. modbusTcp.Write<ushort>(DeviceAddress.Start.ToAdd(), 0);
  356. }
  357. public Task UpdateState(float weight)
  358. {
  359. return Task.Factory.StartNew(() =>
  360. {
  361. MessageNotify.GetInstance.ShowRunLog($"设备{DeviceName}配料状态:下发开始配料信号");
  362. modbusTcp.Write<ushort>(DeviceAddress.Start.ToAdd(), 1);
  363. modbusTcp.Write<float>(DeviceAddress.WeightSet.ToAdd(), weight);//下发重量
  364. Thread.Sleep(100);
  365. modbusTcp.Write<ushort>(DeviceAddress.Start.ToAdd(), 0);
  366. });
  367. }
  368. #endregion
  369. public void Init(string DeviceName)
  370. {
  371. //modbusTcp.Show = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
  372. //modbusTcp.ShowEx = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
  373. this.DeviceName = DeviceName;
  374. AlarmHelper<AlarmInfo>.Init();
  375. if (IsConnected)
  376. {
  377. TaskManage.GetInstance.StartLong(new Action(() =>
  378. {
  379. var dn = modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20);
  380. if (dn.IsSuccess) { this.DeviceName = dn.Content.Trim().Replace(" ", ""); deviceStatus.DeviceName = this.DeviceName; }
  381. //获取设备运行状态
  382. var rs = this.modbusTcp.Read<ushort>(DeviceAddress.RunStatus.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  383. if (rs.IsSuccess) deviceStatus.RunStatus = rs.Content;
  384. var rw = this.modbusTcp.Read<float>(DeviceAddress.RealtimeWeight.ToAdd(), 2, DataFormat.CDAB);
  385. if (rw.IsSuccess)
  386. {
  387. deviceStatus.RealWeightFeedback = rw.Content;
  388. }
  389. ////获取设备料仓剩余重量
  390. var wf = this.modbusTcp.Read<uint>(DeviceAddress.WeightFeedback.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  391. if (wf.IsSuccess) deviceStatus.WeightFeedback = (uint)(wf.Content);
  392. ////获取下料重量
  393. var nwf = this.modbusTcp.Read<float>(DeviceAddress.CutWeightFeedback.ToAdd(), 2, BPA.Helper.DataFormat.CDAB);
  394. if (nwf.IsSuccess) deviceStatus.CutWeightFeedback = nwf.Content;
  395. ////获取设备编号
  396. var dnum = this.modbusTcp.Read<ushort>(DeviceAddress.DeviceNum.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  397. if (dnum.IsSuccess)
  398. {
  399. deviceStatus.DeviceNum = dnum.Content;
  400. if (dnum.Content > 0&& SimensSend.GetInstance.IsConnect)
  401. {
  402. deviceStatus.PailArrive = SimensSend.GetInstance.SendSimens.Read<bool>($"DB1.DBX80{(int)((dnum.Content-1)/8)}.{(dnum.Content-1)%8}").Content;
  403. deviceStatus.PassPail = SimensSend.GetInstance.SendSimens.Read<ushort>($"DB1.DBW{810 + 2 * (dnum.Content - 1)}").Content;
  404. deviceStatus.RecipePail = SimensSend.GetInstance.SendSimens.Read<int>($"DB1.DBW{710 + 4 * (dnum.Content - 1)}").Content;
  405. deviceStatus.RecipeFinish = SimensSend.GetInstance.SendSimens.Read<bool>($"DB1.DBX70{(int)((dnum.Content - 1) / 8)}.{(dnum.Content - 1) % 8}").Content;
  406. }
  407. }
  408. ////获取设备故障编码
  409. var dac = this.modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  410. if (dac.IsSuccess) deviceStatus.DeviceAlarmCode = dac.Content;
  411. deviceStatus.StartSend = SimensSend.GetInstance.SendSimens.Read<bool>($"M11.5").Content;
  412. //获取原料类型
  413. var mt = this.modbusTcp.Read<ushort>(DeviceAddress.MaterialDeviceType.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  414. if (mt.IsSuccess) deviceStatus.RawMaterialType = mt.Content;
  415. //获取工作模式
  416. var wm = this.modbusTcp.Read<ushort>(DeviceAddress.WorkModel.ToAdd(),1, BPA.Helper.DataFormat.CDAB);
  417. if (wm.IsSuccess) {
  418. deviceStatus.Finish_mt = wm.Content.GetBitValue(6);
  419. }
  420. /* //慢加重量
  421. var ssaw = this.modbusTcp.Read<float>(DeviceAddress.SlowlyAddWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  422. if (ssaw.IsSuccess) deviceStatus.SlowAddWeight = ssaw.Content;
  423. //提前关阀重量
  424. var pcvw = this.modbusTcp.Read<float>(DeviceAddress.PreCloseValveWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  425. if (pcvw.IsSuccess) deviceStatus.PreCloseValueWeight = pcvw.Content;
  426. //快加速度
  427. var ra = this.modbusTcp.Read<uint>(DeviceAddress.RapidAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  428. if (ra.IsSuccess) deviceStatus.RapidAcceleration = ra.Content;
  429. //慢加速度
  430. var sa = this.modbusTcp.Read<uint>(DeviceAddress.SlowAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  431. if (sa.IsSuccess) deviceStatus.SlowAcceleration = sa.Content;
  432. //伺服手动速度
  433. var sms = this.modbusTcp.Read<uint>(DeviceAddress.ServoManualSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  434. if (sms.IsSuccess) deviceStatus.ServoManualSpeed = sms.Content;
  435. //料仓上限重量
  436. var ulw = this.modbusTcp.Read<uint>(DeviceAddress.SiloUpperLimitWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  437. if (ulw.IsSuccess) deviceStatus.SiloUpperLimitWeight = ulw.Content;
  438. //料仓下限重量
  439. var llw = this.modbusTcp.Read<uint>(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  440. if (llw.IsSuccess) deviceStatus.SiloLowerLimitWeight = llw.Content;
  441. //料仓搅拌速度
  442. var ss = this.modbusTcp.Read<uint>(DeviceAddress.StirringSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  443. if (ss.IsSuccess) deviceStatus.StirringSpeed = ss.Content;*/
  444. /* //获取桶是否到位
  445. var pailArrive = this.modbusTcp.Read<ushort>(DeviceAddress.PailArrive.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  446. if (pailArrive.IsSuccess) deviceStatus.PailArrive = pailArrive.Content;
  447. //已通过桶数
  448. var pass = this.modbusTcp.Read<ushort>(DeviceAddress.PassPail.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  449. if (pass.IsSuccess) deviceStatus.PassPail = pass.Content;
  450. //配料最大桶数
  451. var max = this.modbusTcp.Read<ushort>(DeviceAddress.MaxPail.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  452. if (max.IsSuccess) deviceStatus.MaxPassPail = max.Content;
  453. //执行配方数
  454. var doCount = this.modbusTcp.Read<ushort>(DeviceAddress.DoRecipeCount.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  455. if (doCount.IsSuccess) deviceStatus.DoRecipeCount = doCount.Content;*/
  456. /*var ts = this.modbusTcp.Read<ushort>(DeviceAddress.TranspportSwitch.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
  457. if (ts.IsSuccess) deviceStatus.TranspportSwitch = ts.Content;*/
  458. //this.DeviceName = modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20).Content.Trim().Replace(" ", "");
  459. //deviceStatus.RunStatus = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.RunStatus.ToAdd()).Content; //获取设备运行状态
  460. //deviceStatus.WeightFeedback = (float)this.modbusTcp.Read<uint>(DeviceAddress.WeightFeedback.ToAdd()).Content;//获取设备料仓剩余重量
  461. //deviceStatus.NowWeightFeedback = this.modbusTcp.Read<float>(DeviceAddress.CutWeightFeedback.ToAdd()).Content;//获取下料重量
  462. //deviceStatus.DeviceNum = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.DeviceNum.ToAdd()).Content;//获取设备编号
  463. //deviceStatus.DeviceAlarmCode = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd()).Content;//获取设备故障编码
  464. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop1 = deviceStatus.DeviceAlarmCode.GetBitValue(1);
  465. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Servo = deviceStatus.DeviceAlarmCode.GetBitValue(2);
  466. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Inverter = deviceStatus.DeviceAlarmCode.GetBitValue(3);
  467. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop2 = deviceStatus.DeviceAlarmCode.GetBitValue(7);
  468. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloUpperLimit = deviceStatus.DeviceAlarmCode.GetBitValue(8);
  469. AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloLowerLimit = deviceStatus.DeviceAlarmCode.GetBitValue(9);
  470. Thread.Sleep(10);
  471. }), $"{DeviceName} 开始监听", true);
  472. }
  473. }
  474. public void SetDeviceName(string name,int index)
  475. {
  476. this.modbusTcp.Write(DeviceAddress.DeviceName.ToAdd(), new ushort[20]);
  477. this.modbusTcp.Write(DeviceAddress.DeviceName.ToAdd(), name);
  478. }
  479. public void StatusReset()
  480. {
  481. // prop1:
  482. this.modbusTcp.Write(DeviceAddress.FinfishStatus.ToAdd(), (ushort)1);
  483. //MessageNotify.GetInstance.ShowRunLog($"料仓复位,值:{1}");
  484. //var value = this.modbusTcp.Read<ushort>(DeviceAddress.FinfishStatus.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  485. //if (value != null && value.ToString() != 1.ToString()) goto prop1;
  486. }
  487. public void Dispose()
  488. {
  489. TaskManage.GetInstance.StopTask($"{DeviceName} 开始监听");
  490. }
  491. public void Start(float Value)
  492. {
  493. if (modbusTcp.IsConnected())
  494. {
  495. var res = Json<DevicePar>.Data.deviceParModels.FirstOrDefault(p => p.MaterialName == DeviceName);
  496. if (res != null)
  497. {
  498. //modbusTcp.Write(DeviceAddress.SlowlyAddWeight.ToAdd(), res.SlowlyAddWeight);
  499. //modbusTcp.Write(DeviceAddress.PreCloseValveWeight.ToAdd(), res.PreCloseValveWeight);
  500. //modbusTcp.Write(DeviceAddress.RapidAcceleration.ToAdd(), (uint)res.RapidAcceleration);
  501. //modbusTcp.Write(DeviceAddress.SlowAcceleration.ToAdd(), (uint)res.SlowAcceleration);
  502. //modbusTcp.Write(DeviceAddress.ServoManualSpeed.ToAdd(), (uint)res.ServoManualSpeed);
  503. //modbusTcp.Write(DeviceAddress.SiloUpperLimitWeight.ToAdd(), (uint)res.SiloUpperLimitWeight);
  504. //modbusTcp.Write(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), (uint)res.LowerLimitWeightOfSilo);
  505. //modbusTcp.Write(DeviceAddress.StirringSpeed.ToAdd(), (uint)res.StirringSpeed);
  506. //MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},配料参数下发完成");
  507. prop1:
  508. modbusTcp.Write(DeviceAddress.SlowlyAddWeight.ToAdd(), res.SlowlyAddWeight);
  509. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置慢加重量:{res.SlowlyAddWeight}");
  510. var value1 = modbusTcp.Read<float>(DeviceAddress.SlowlyAddWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  511. if (value1 != null && value1.ToString() != res.SlowlyAddWeight.ToString()) goto prop1;
  512. prop2:
  513. modbusTcp.Write(DeviceAddress.PreCloseValveWeight.ToAdd(), res.PreCloseValveWeight);
  514. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置提前关阀重量:{res.PreCloseValveWeight}");
  515. var value2 = modbusTcp.Read<float>(DeviceAddress.PreCloseValveWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  516. if (value2 != null && value2.ToString() != res.PreCloseValveWeight.ToString()) goto prop2;
  517. prop3:
  518. modbusTcp.Write(DeviceAddress.RapidAcceleration.ToAdd(), (uint)res.RapidAcceleration);
  519. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置快加速度:{res.RapidAcceleration}");
  520. var value3 = modbusTcp.Read<uint>(DeviceAddress.RapidAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  521. if (value3 != null && value3.ToString() != res.RapidAcceleration.ToString()) goto prop3;
  522. prop4:
  523. modbusTcp.Write(DeviceAddress.SlowAcceleration.ToAdd(), (uint)res.SlowAcceleration);
  524. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置慢加速度:{res.SlowAcceleration}");
  525. var value4 = modbusTcp.Read<uint>(DeviceAddress.SlowAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  526. if (value4 != null && value4.ToString() != res.SlowAcceleration.ToString()) goto prop4;
  527. prop5:
  528. modbusTcp.Write(DeviceAddress.ServoManualSpeed.ToAdd(), (uint)res.ServoManualSpeed);
  529. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置伺服手动速度:{res.ServoManualSpeed}");
  530. var value5 = modbusTcp.Read<uint>(DeviceAddress.ServoManualSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  531. if (value5 != null && value5.ToString() != res.ServoManualSpeed.ToString()) goto prop5;
  532. prop6:
  533. modbusTcp.Write(DeviceAddress.SiloUpperLimitWeight.ToAdd(), (uint)res.SiloUpperLimitWeight);
  534. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置料仓上限重量:{res.SiloUpperLimitWeight}");
  535. var value6 = modbusTcp.Read<uint>(DeviceAddress.SiloUpperLimitWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  536. if (value6 != null && value6.ToString() != res.SiloUpperLimitWeight.ToString()) goto prop6;
  537. prop7:
  538. modbusTcp.Write(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), (uint)res.LowerLimitWeightOfSilo);
  539. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置料仓下限重量:{res.LowerLimitWeightOfSilo}");
  540. var value7 = modbusTcp.Read<uint>(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  541. if ((value7 != null && value7.ToString() != res.LowerLimitWeightOfSilo.ToString())) goto prop7;
  542. prop8:
  543. modbusTcp.Write(DeviceAddress.StirringSpeed.ToAdd(), (uint)res.StirringSpeed);
  544. MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置搅拌速度:{res.StirringSpeed}");
  545. var value8 = modbusTcp.Read<uint>(DeviceAddress.StirringSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  546. if (value8 != null && value8.ToString() != res.StirringSpeed.ToString()) goto prop8;
  547. MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},配料参数下发完成");
  548. }
  549. //prop9:
  550. // modbusTcp.Write(DeviceAddress.WeightSet.ToAdd(), Value);//写入配方重量
  551. // MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value}");
  552. // var value9 = modbusTcp.Read<float>(DeviceAddress.WeightSet.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  553. // if ((value9 != null && value9.ToString() != Value.ToString()) || value9 == 0) goto prop9;
  554. // //prop10:
  555. // //MessageNotify.GetInstance.ShowRunLog($"写入配方重量,值{Value}:{value9}");
  556. // //modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
  557. // //var value10 = modbusTcp.Read<ushort>(DeviceAddress.Start.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  558. // //if ((value10 != null && value10.ToString() != 1.ToString())||value10==0) goto prop10;
  559. // //modbusTcp.Write(DeviceAddress.WeightSet.ToAdd(), Value);//写入配方重量
  560. // modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
  561. // Task.Run(() =>
  562. // {
  563. // while (true)
  564. // {
  565. // ushort c = modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
  566. // MessageNotify.GetInstance.ShowRunLog($"{IpAddress}:LW51,值:{modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content}");
  567. // string str = DecToBinary(c);
  568. // if (str[8].ToString() == 0.ToString())
  569. // {
  570. // modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
  571. // }
  572. // else
  573. // return;
  574. // }
  575. // });
  576. // MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value},味魔方启动配料");
  577. }
  578. }
  579. static string DecToBinary(int a)
  580. {
  581. int r;
  582. string s = "";
  583. do
  584. {
  585. r = a % 2;
  586. a /= 2;
  587. s = r + s;
  588. }
  589. while (a != 0);
  590. int length = s.Length;
  591. for (int i = 0; i < 16 - length; i++)
  592. {
  593. s = 0 + s;
  594. }
  595. return s;
  596. }
  597. }
  598. }