|
- //#define Debug
- using Aop.Api.Domain;
- using BPA.Communication;
- using BPA.Helper;
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.CustomResource.UserControls;
- using BPASmartClient.CustomResource.UserControls.MessageShow;
- using BPASmartClient.DosingSystem.Model;
- using BPASmartClient.DosingSystem.ViewModel;
- using BPASmartClient.Model;
- using Google.Protobuf.WellKnownTypes;
- using Org.BouncyCastle.Asn1;
-
-
- //using BPASmartClient.Modbus;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Reflection;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace BPASmartClient.DosingSystem
- {
- public class DeviceInquire
- {
- int stockCount;//料仓数
- private volatile static DeviceInquire _Instance;
- public static DeviceInquire GetInstance => _Instance ?? (_Instance = new DeviceInquire());
-
- private DeviceInquire() { }
-
- string IPSegment = "192.168.2.";
- /// <summary>
- /// 设备列表集合
- /// </summary>
- public ConcurrentDictionary<string, DeviceStatus> DeviceLists = new ConcurrentDictionary<string, DeviceStatus>();
- List<string> InvalidIP = new List<string>();//无效 IP 集合
- List<string> IPLists = new List<string>();//启动 Ping 任务IP集合
- ConcurrentQueue<string> IPQueues = new ConcurrentQueue<string>();//pincomplete 完成队列
-
- /// <summary>
- /// 记录每个IP对应的设备是否已经执行数据清零,避免重连后再次清零。
- /// </summary>
- private ConcurrentDictionary<string, bool> IsClearData = new();
-
- public ObservableCollection<DeviceCurrentStatus> TopDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
- public ObservableCollection<DeviceCurrentStatus> BottomDeviceCurrentStatuses { get; set; } = new ObservableCollection<DeviceCurrentStatus>();
- public ObservableCollection<Devices> devices { get; set; } = new ObservableCollection<Devices>();
- private void DeviceDataInit()
- {
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- for (int i = 0; i < DeviceLists.Count; i++)
- {
- string deviceName = DeviceLists.ElementAt(i).Value.DeviceName;
- int TopIndex = Array.FindIndex(TopDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
- int BottomIndex = Array.FindIndex(BottomDeviceCurrentStatuses.ToArray(), p => p.DeviceName == deviceName);
- if (TopIndex >= 0 && TopIndex < TopDeviceCurrentStatuses.Count)
- {
- TopDeviceCurrentStatuses.ElementAt(TopIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
- TopDeviceCurrentStatuses.ElementAt(TopIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
- }
-
- if (BottomIndex >= 0 && BottomIndex < BottomDeviceCurrentStatuses.Count)
- {
- BottomDeviceCurrentStatuses.ElementAt(BottomIndex).Weight = DeviceLists.ElementAt(i).Value.deviceStatus.WeightFeedback;
- BottomDeviceCurrentStatuses.ElementAt(BottomIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
- }
-
- 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*/);
- if (deviceIndex >= 0 && deviceIndex < devices.Count)
- {
- devices.ElementAt(deviceIndex).DeviceName = DeviceLists.ElementAt(i).Value.DeviceName;
- devices.ElementAt(deviceIndex).DeviceNum = DeviceLists.ElementAt(i).Value.deviceStatus.DeviceNum;
- }
- }
- Thread.Sleep(200);
- }), "设备状态监听", true);
- }
-
-
- public void Init()
- {
- if (!string.IsNullOrEmpty(Json<DevicePar>.Data.BaseParModel.NetworkSegAddress)) IPSegment = Json<DevicePar>.Data.BaseParModel.NetworkSegAddress;
- else Json<DevicePar>.Data.BaseParModel.NetworkSegAddress = IPSegment;
- stockCount = Json<DevicePar>.Data.BaseParModel.StockCount;
- //TestData();
- /*IpAddressLines();*/
- Task.Factory.StartNew(() =>
- {
- SimensSend.GetInstance.Connect();
- /*while(!clearValue)
- {
- clearValue = ClearData();
- }*/
- //SiemensDevice.GetInstance.Connect(Json<DevicePar>.Data.BaseParModel.DeviceAddress);
- });
- /* DeviceDataInit();*/
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- if (IPQueues.Count >= IPLists.Count)
- IpAddressLines();
- Thread.Sleep(5000);
- }), "配料机设备上线监听", true);
- }
- /// <summary>
- /// 清除所有数据
- /// </summary>
- /// <returns></returns>
- public bool ClearData()
- {
- if (SimensSend.GetInstance.SendSimens.IsReconnect)
- {
- var res = SimensSend.GetInstance.SendSimens.Write<ushort>("DB1.DBW840", (ushort)1).IsSuccess;
- Thread.Sleep(2000);
- bool result = false;
- if (res)
- {
- result = SimensSend.GetInstance.SendSimens.Write<ushort>("DB1.DBW840", (ushort)0).IsSuccess;
- }
- if (result)
- {
- App.Current.Dispatcher.Invoke(() => { NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, App.MainWindow, "成功", $"数据清除完成"); });
- }
-
- /* #region 2023-11-20-17新增:在软件重启时,自动清零设备上的配方执行数量等数据。
- var result1= modbusTcp.Write<ushort>(DeviceAddress.ZeroRecipeCount.ToAdd(), 1).IsSuccess;
- Thread.Sleep(500);
- var result2= modbusTcp.Write<ushort>(DeviceAddress.ZeroRecipeCount.ToAdd(), 0).IsSuccess;
-
- return result1 && result2;
- #endregion*/
- return result;
- }
- else
- {
- return false;
- }
- }
-
- public void Rescan()
- {
- InvalidIP.Clear();
- }
-
- public DeviceStatus GetDevice(string ip)
- {
- if (ip != null)
- {
- if (DeviceLists.ContainsKey(ip)) return DeviceLists[ip];
- else return new DeviceStatus();
- //var res = DeviceLists.Values.FirstOrDefault(p => p.IpAddress == ip);
- //if (res != null) return res;
- }
- return new DeviceStatus();
- }
-
- public List<DeviceStatus> GetDevice()
- {
- List<DeviceStatus> deviceStatuses = new List<DeviceStatus>();
- foreach (var device in DeviceLists)
- {
- deviceStatuses.Add(device.Value);
- }
- return deviceStatuses;
- }
-
- private void IpAddressLines()
- {
- IPLists.Clear();
- IPQueues.Clear();
- //TestData();
- /*for (int i = 1; i <= 255; i++)
- {
- if (!InvalidIP.Contains($"{IPSegment}{i}") && !DeviceLists.ContainsKey($"{IPSegment}{i}"))
- {
- string pattern = @"^(([1-9]\d?)|(1\d{2})|(2[01]\d)|(22[0-3]))(\.((1?\d\d?)|(2[04]/d)|(25[0-5]))){3}$";
- if (Regex.IsMatch($"{IPSegment}{i}", pattern))
- {
- IPLists.Add($"{IPSegment}{i}");
- }
- }
- }*/
-
- for (int i = 0; i < stockCount; i++)
- {
- IPLists.Add($"{IPSegment}{(i + 1) * 10}");
- //IPLists.Add($"{IPSegment}{this.devices[i].DeviceNum}");
-
- //2023-11-23:根据IP添加到记录字典。
- var ip = $"{IPSegment}{(i + 1) * 10}";
- if (!IsClearData.ContainsKey(ip))
- {
- IsClearData.TryAdd($"{IPSegment}{(i + 1) * 10}", false);
- }
- }
-
- IPLists.ForEach((item) =>
- {
- Ping myPing = new Ping();
- myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
- myPing.SendAsync(item, 1000, null);
-
- });
- }
- bool clearValue = false;
-
- private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
- {
- if (e.Reply != null && e.Reply.Status == IPStatus.Success)
- {
- string ip = e.Reply.Address.ToString();
-
- if (!DeviceLists.ContainsKey(ip))
- {
- DeviceStatus DS = new DeviceStatus();
- DS.modbusTcp.IsReconnect = false;
- #region 2023-11-23修改:在该IP第一次连接时清除设备的数据,后续重连时不清除。
- /*if (IsClearData.ContainsKey(ip) && IsClearData[ip] == false)
- {
- if (DS.ClearData())
- {
- //清除成功就置位。
- IsClearData[ip] = true;
- }
- } */
- //备注:2024-1-22修改,连接后整体数据清理
-
- #endregion
- DS.modbusTcp.Connected = new Action(() =>
- {
- var tt = DS.modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20);
-
- string DeviceName = DS.modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20).Content?.Trim()?.Replace(" ", "");//读取设备名称
-
- if (DeviceName.Length > 0)
- {
- DeviceLists.TryAdd(ip, DS);
- DeviceLists[ip].Init(DeviceName);
- DeviceLists[ip].modbusTcp.IsReconnect = false;
-
-
-
- App.Current.Dispatcher.Invoke(new Action(() =>
- {
- devices.Add(new Devices() { DeviceName = DeviceName, IpAddress = ip });
- //2023/7/3调整
- if (stockCount <= 8 || (stockCount > 8 && TopDeviceCurrentStatuses.Count <= stockCount / 2 + 1 && int.TryParse(ip.Split('.')[3], out int res) && res <= (stockCount / 2 + 1) * 10))
- TopDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
- else
- BottomDeviceCurrentStatuses.Add(new DeviceCurrentStatus() { DeviceName = DeviceName });
-
-
- for (int i = 0; i < Json<LocalRecipe>.Data.Recipes.Count; i++)
- {
- for (int m = 0; m < Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.Count; m++)
- {
- if (Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).DeviceIp == ip)
- {
- Json<LocalRecipe>.Data.Recipes.ElementAt(i).RawMaterials.ElementAt(m).RawMaterialName = DeviceName;
- }
- }
- }
- if (Global.DeviceRawMaterials.Count > 0)
- {
- if (Global.DeviceRawMaterials.FirstOrDefault(p => p.RawMaterialName == DeviceName) == null)
- {
- Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
- }
- }
- else
- {
- Global.DeviceRawMaterials.Add(new RawMaterialModel() { RawMaterialName = DeviceName, DeviceIp = ip, RawMaterialSource = 1 });
- }
-
- }));
- }
- else
- {
- //if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
- }
- });
-
- DS.modbusTcp.ConnectFail = new Action(() =>
- {
- //if (!InvalidIP.Contains(ip)) InvalidIP.Add(ip);
- });
-
- DS.modbusTcp.Disconnect = new Action(() =>
- {
- if (InvalidIP.Contains(ip)) InvalidIP.Remove(ip);
- var res = devices.FirstOrDefault(P => P.IpAddress == ip);
- if (res != null && devices.Contains(res))
- {
- App.Current.Dispatcher.Invoke(new Action(() =>
- {
- devices.Remove(res);
- var item = Global.DeviceRawMaterials.FirstOrDefault(P => P.RawMaterialName == res.DeviceName);
- if (item != null) Global.DeviceRawMaterials.Remove(item);
-
- var topRes = TopDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
- var bottomRes = BottomDeviceCurrentStatuses.FirstOrDefault(p => p.DeviceName == res.DeviceName);
- if (topRes != null) TopDeviceCurrentStatuses.Remove(topRes);
- if (bottomRes != null) BottomDeviceCurrentStatuses.Remove(bottomRes);
- }));
- }
-
- if (DeviceLists.ContainsKey(ip))
- {
- DeviceLists[ip].Dispose();
- var res11 = DeviceLists[ip];
- DeviceLists.TryRemove(ip, out res11);
- }
- });
-
- Task.Run(new Action(() =>
- {
- DS.modbusTcp.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = ip, Port = 502 });//PLC连接
- IPQueues.Enqueue(e.Reply.Address.ToString());
- }));
- }
- else IPQueues.Enqueue(e.Reply.Address.ToString());
- }
- else
- {
- if (e.Reply != null)
- IPQueues.Enqueue(e.Reply.Address.ToString());
- }
- }
- }
- /// <summary>
- /// 料仓状态(报警)获取及展示
- /// </summary>
- public class DeviceStatus
- {
- public int recipeC { get; set; }
- public int mrCount { get; set; }
-
- private bool start_down = false;
- public Action<int,int> startAction { get; set; }
- #region 对象属性声明
- public string DeviceName = String.Empty;
- public string IpAddress => modbusTcp.GetConfigPar().IpAddress;
-
- /// <summary>
- /// 设备状态
- /// </summary>
- public RawMaterialDeviceStatus deviceStatus { get; set; } = new RawMaterialDeviceStatus();
-
- public ModbusTcp modbusTcp = new ModbusTcp();
-
- public bool IsConnected => modbusTcp.IsConnected();
-
- public Task ResetAll() {
-
- return Task.Factory.StartNew(() => {
- if (deviceStatus.DeviceNum > 0) {
- int dnum = deviceStatus.DeviceNum;
- modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 1);
- Thread.Sleep(100);
- modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 0);
- modbusTcp.Write<ushort>(DeviceAddress.Start.ToAdd(), 0);
- SimensSend.GetInstance.SendSimens.Write<ushort>($"DB1.DBW{810 + 2 * (dnum - 1)}", 0);
- SimensSend.GetInstance.SendSimens.Write<int>($"DB1.DBW{710 + 4 * (dnum - 1)}", 0);
- }
- });
- }
-
- public Task Reset()
- {
- return Task.Factory.StartNew(() =>
- {
- ushort reset = 0;
- reset = reset.SetBitValue(1, true);
- modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), reset);
- Thread.Sleep(200);
- modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 0);
-
- int dnum = deviceStatus.DeviceNum;
- if (dnum > 0)
- {
- SimensSend.GetInstance.SendSimens.Write<bool>($"DB1.DBX70{(int)((dnum - 1) / 8)}.{(dnum - 1) % 8}", true);
- Thread.Sleep(100);
- SimensSend.GetInstance.SendSimens.Write<bool>($"DB1.DBX70{(int)((dnum - 1) / 8)}.{(dnum - 1) % 8}", false);
- }
- MessageNotify.GetInstance.ShowRunLog($"设备{DeviceName}配料状态:配料完成复位");
- });
- }
-
-
- public void Reset1()
- {
- modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 1);
- Thread.Sleep(200);
- modbusTcp.Write<ushort>(DeviceAddress.Finish_Point.ToAdd(), 0);
- modbusTcp.Write<ushort>(DeviceAddress.Start.ToAdd(), 0);
-
- }
-
- public Task UpdateState(float weight)
- {
- return Task.Factory.StartNew(() =>
- {
- MessageNotify.GetInstance.ShowRunLog($"设备{DeviceName}配料状态:下发开始配料信号");
- modbusTcp.Write<ushort>(DeviceAddress.Start.ToAdd(), 1);
- modbusTcp.Write<float>(DeviceAddress.WeightSet.ToAdd(), weight);//下发重量
- Thread.Sleep(100);
- modbusTcp.Write<ushort>(DeviceAddress.Start.ToAdd(), 0);
- });
-
-
- }
- #endregion
-
- public void Init(string DeviceName)
- {
-
- //modbusTcp.Show = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
- //modbusTcp.ShowEx = new Action<string>((s) => { MessageNotify.GetInstance.ShowRunLog(s); });
- this.DeviceName = DeviceName;
- AlarmHelper<AlarmInfo>.Init();
- if (IsConnected)
- {
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- var dn = modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20);
- if (dn.IsSuccess) { this.DeviceName = dn.Content.Trim().Replace(" ", ""); deviceStatus.DeviceName = this.DeviceName; }
-
- //获取设备运行状态
- var rs = this.modbusTcp.Read<ushort>(DeviceAddress.RunStatus.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (rs.IsSuccess) deviceStatus.RunStatus = rs.Content;
-
- var rw = this.modbusTcp.Read<float>(DeviceAddress.RealtimeWeight.ToAdd(), 2, DataFormat.CDAB);
- if (rw.IsSuccess)
- {
- deviceStatus.RealWeightFeedback = rw.Content;
- }
-
- ////获取设备料仓剩余重量
- var wf = this.modbusTcp.Read<uint>(DeviceAddress.WeightFeedback.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (wf.IsSuccess) deviceStatus.WeightFeedback = (uint)(wf.Content);
-
- ////获取下料重量
- var nwf = this.modbusTcp.Read<float>(DeviceAddress.CutWeightFeedback.ToAdd(), 2, BPA.Helper.DataFormat.CDAB);
- if (nwf.IsSuccess) deviceStatus.CutWeightFeedback = nwf.Content;
-
- ////获取设备编号
- var dnum = this.modbusTcp.Read<ushort>(DeviceAddress.DeviceNum.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (dnum.IsSuccess)
- {
- deviceStatus.DeviceNum = dnum.Content;
- if (dnum.Content > 0&& SimensSend.GetInstance.IsConnect)
- {
- deviceStatus.PailArrive = SimensSend.GetInstance.SendSimens.Read<bool>($"DB1.DBX80{(int)((dnum.Content-1)/8)}.{(dnum.Content-1)%8}").Content;
- deviceStatus.PassPail = SimensSend.GetInstance.SendSimens.Read<ushort>($"DB1.DBW{810 + 2 * (dnum.Content - 1)}").Content;
- deviceStatus.RecipePail = SimensSend.GetInstance.SendSimens.Read<int>($"DB1.DBW{710 + 4 * (dnum.Content - 1)}").Content;
- deviceStatus.RecipeFinish = SimensSend.GetInstance.SendSimens.Read<bool>($"DB1.DBX70{(int)((dnum.Content - 1) / 8)}.{(dnum.Content - 1) % 8}").Content;
- }
- }
- ////获取设备故障编码
- var dac = this.modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (dac.IsSuccess) deviceStatus.DeviceAlarmCode = dac.Content;
- deviceStatus.StartSend = SimensSend.GetInstance.SendSimens.Read<bool>($"M11.5").Content;
- //获取原料类型
- var mt = this.modbusTcp.Read<ushort>(DeviceAddress.MaterialDeviceType.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (mt.IsSuccess) deviceStatus.RawMaterialType = mt.Content;
-
- //获取工作模式
- var wm = this.modbusTcp.Read<ushort>(DeviceAddress.WorkModel.ToAdd(),1, BPA.Helper.DataFormat.CDAB);
- if (wm.IsSuccess) {
- deviceStatus.Finish_mt = wm.Content.GetBitValue(6);
- }
- /* //慢加重量
- var ssaw = this.modbusTcp.Read<float>(DeviceAddress.SlowlyAddWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ssaw.IsSuccess) deviceStatus.SlowAddWeight = ssaw.Content;
- //提前关阀重量
- var pcvw = this.modbusTcp.Read<float>(DeviceAddress.PreCloseValveWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (pcvw.IsSuccess) deviceStatus.PreCloseValueWeight = pcvw.Content;
- //快加速度
- var ra = this.modbusTcp.Read<uint>(DeviceAddress.RapidAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ra.IsSuccess) deviceStatus.RapidAcceleration = ra.Content;
- //慢加速度
- var sa = this.modbusTcp.Read<uint>(DeviceAddress.SlowAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (sa.IsSuccess) deviceStatus.SlowAcceleration = sa.Content;
- //伺服手动速度
- var sms = this.modbusTcp.Read<uint>(DeviceAddress.ServoManualSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (sms.IsSuccess) deviceStatus.ServoManualSpeed = sms.Content;
- //料仓上限重量
- var ulw = this.modbusTcp.Read<uint>(DeviceAddress.SiloUpperLimitWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ulw.IsSuccess) deviceStatus.SiloUpperLimitWeight = ulw.Content;
- //料仓下限重量
- var llw = this.modbusTcp.Read<uint>(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (llw.IsSuccess) deviceStatus.SiloLowerLimitWeight = llw.Content;
- //料仓搅拌速度
- var ss = this.modbusTcp.Read<uint>(DeviceAddress.StirringSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ss.IsSuccess) deviceStatus.StirringSpeed = ss.Content;*/
- /* //获取桶是否到位
- var pailArrive = this.modbusTcp.Read<ushort>(DeviceAddress.PailArrive.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (pailArrive.IsSuccess) deviceStatus.PailArrive = pailArrive.Content;
- //已通过桶数
- var pass = this.modbusTcp.Read<ushort>(DeviceAddress.PassPail.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (pass.IsSuccess) deviceStatus.PassPail = pass.Content;
- //配料最大桶数
- var max = this.modbusTcp.Read<ushort>(DeviceAddress.MaxPail.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (max.IsSuccess) deviceStatus.MaxPassPail = max.Content;
- //执行配方数
- var doCount = this.modbusTcp.Read<ushort>(DeviceAddress.DoRecipeCount.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (doCount.IsSuccess) deviceStatus.DoRecipeCount = doCount.Content;*/
-
- /*var ts = this.modbusTcp.Read<ushort>(DeviceAddress.TranspportSwitch.ToAdd(), 1, BPA.Helper.DataFormat.CDAB);
- if (ts.IsSuccess) deviceStatus.TranspportSwitch = ts.Content;*/
- //this.DeviceName = modbusTcp.Read<string>(DeviceAddress.DeviceName.ToAdd(), 20).Content.Trim().Replace(" ", "");
- //deviceStatus.RunStatus = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.RunStatus.ToAdd()).Content; //获取设备运行状态
- //deviceStatus.WeightFeedback = (float)this.modbusTcp.Read<uint>(DeviceAddress.WeightFeedback.ToAdd()).Content;//获取设备料仓剩余重量
- //deviceStatus.NowWeightFeedback = this.modbusTcp.Read<float>(DeviceAddress.CutWeightFeedback.ToAdd()).Content;//获取下料重量
- //deviceStatus.DeviceNum = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.DeviceNum.ToAdd()).Content;//获取设备编号
- //deviceStatus.DeviceAlarmCode = (ushort)this.modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd()).Content;//获取设备故障编码
-
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop1 = deviceStatus.DeviceAlarmCode.GetBitValue(1);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Servo = deviceStatus.DeviceAlarmCode.GetBitValue(2);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).Inverter = deviceStatus.DeviceAlarmCode.GetBitValue(3);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).EStop2 = deviceStatus.DeviceAlarmCode.GetBitValue(7);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloUpperLimit = deviceStatus.DeviceAlarmCode.GetBitValue(8);
- AlarmHelper<AlarmInfo>.GetInstance(DeviceName).SiloLowerLimit = deviceStatus.DeviceAlarmCode.GetBitValue(9);
-
-
- Thread.Sleep(10);
- }), $"{DeviceName} 开始监听", true);
- }
- }
-
- public void SetDeviceName(string name,int index)
- {
- this.modbusTcp.Write(DeviceAddress.DeviceName.ToAdd(), new ushort[20]);
- this.modbusTcp.Write(DeviceAddress.DeviceName.ToAdd(), name);
- }
-
- public void StatusReset()
- {
- // prop1:
- this.modbusTcp.Write(DeviceAddress.FinfishStatus.ToAdd(), (ushort)1);
- //MessageNotify.GetInstance.ShowRunLog($"料仓复位,值:{1}");
- //var value = this.modbusTcp.Read<ushort>(DeviceAddress.FinfishStatus.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- //if (value != null && value.ToString() != 1.ToString()) goto prop1;
- }
-
- public void Dispose()
- {
- TaskManage.GetInstance.StopTask($"{DeviceName} 开始监听");
- }
-
-
-
-
-
- public void Start(float Value)
- {
- if (modbusTcp.IsConnected())
- {
- var res = Json<DevicePar>.Data.deviceParModels.FirstOrDefault(p => p.MaterialName == DeviceName);
- if (res != null)
- {
- //modbusTcp.Write(DeviceAddress.SlowlyAddWeight.ToAdd(), res.SlowlyAddWeight);
-
- //modbusTcp.Write(DeviceAddress.PreCloseValveWeight.ToAdd(), res.PreCloseValveWeight);
- //modbusTcp.Write(DeviceAddress.RapidAcceleration.ToAdd(), (uint)res.RapidAcceleration);
- //modbusTcp.Write(DeviceAddress.SlowAcceleration.ToAdd(), (uint)res.SlowAcceleration);
- //modbusTcp.Write(DeviceAddress.ServoManualSpeed.ToAdd(), (uint)res.ServoManualSpeed);
- //modbusTcp.Write(DeviceAddress.SiloUpperLimitWeight.ToAdd(), (uint)res.SiloUpperLimitWeight);
- //modbusTcp.Write(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), (uint)res.LowerLimitWeightOfSilo);
- //modbusTcp.Write(DeviceAddress.StirringSpeed.ToAdd(), (uint)res.StirringSpeed);
-
- //MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},配料参数下发完成");
-
- prop1:
- modbusTcp.Write(DeviceAddress.SlowlyAddWeight.ToAdd(), res.SlowlyAddWeight);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置慢加重量:{res.SlowlyAddWeight}");
- var value1 = modbusTcp.Read<float>(DeviceAddress.SlowlyAddWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value1 != null && value1.ToString() != res.SlowlyAddWeight.ToString()) goto prop1;
- prop2:
- modbusTcp.Write(DeviceAddress.PreCloseValveWeight.ToAdd(), res.PreCloseValveWeight);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置提前关阀重量:{res.PreCloseValveWeight}");
- var value2 = modbusTcp.Read<float>(DeviceAddress.PreCloseValveWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value2 != null && value2.ToString() != res.PreCloseValveWeight.ToString()) goto prop2;
- prop3:
- modbusTcp.Write(DeviceAddress.RapidAcceleration.ToAdd(), (uint)res.RapidAcceleration);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置快加速度:{res.RapidAcceleration}");
- var value3 = modbusTcp.Read<uint>(DeviceAddress.RapidAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value3 != null && value3.ToString() != res.RapidAcceleration.ToString()) goto prop3;
- prop4:
- modbusTcp.Write(DeviceAddress.SlowAcceleration.ToAdd(), (uint)res.SlowAcceleration);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置慢加速度:{res.SlowAcceleration}");
- var value4 = modbusTcp.Read<uint>(DeviceAddress.SlowAcceleration.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value4 != null && value4.ToString() != res.SlowAcceleration.ToString()) goto prop4;
- prop5:
- modbusTcp.Write(DeviceAddress.ServoManualSpeed.ToAdd(), (uint)res.ServoManualSpeed);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置伺服手动速度:{res.ServoManualSpeed}");
- var value5 = modbusTcp.Read<uint>(DeviceAddress.ServoManualSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value5 != null && value5.ToString() != res.ServoManualSpeed.ToString()) goto prop5;
-
- prop6:
- modbusTcp.Write(DeviceAddress.SiloUpperLimitWeight.ToAdd(), (uint)res.SiloUpperLimitWeight);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置料仓上限重量:{res.SiloUpperLimitWeight}");
- var value6 = modbusTcp.Read<uint>(DeviceAddress.SiloUpperLimitWeight.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value6 != null && value6.ToString() != res.SiloUpperLimitWeight.ToString()) goto prop6;
- prop7:
- modbusTcp.Write(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), (uint)res.LowerLimitWeightOfSilo);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置料仓下限重量:{res.LowerLimitWeightOfSilo}");
- var value7 = modbusTcp.Read<uint>(DeviceAddress.LowerLimitWeightOfSilo.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if ((value7 != null && value7.ToString() != res.LowerLimitWeightOfSilo.ToString())) goto prop7;
- prop8:
- modbusTcp.Write(DeviceAddress.StirringSpeed.ToAdd(), (uint)res.StirringSpeed);
- MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置搅拌速度:{res.StirringSpeed}");
- var value8 = modbusTcp.Read<uint>(DeviceAddress.StirringSpeed.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- if (value8 != null && value8.ToString() != res.StirringSpeed.ToString()) goto prop8;
- MessageNotify.GetInstance.ShowRunLog($"{res.MaterialName},配料参数下发完成");
- }
- //prop9:
- // modbusTcp.Write(DeviceAddress.WeightSet.ToAdd(), Value);//写入配方重量
- // MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value}");
-
- // var value9 = modbusTcp.Read<float>(DeviceAddress.WeightSet.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- // if ((value9 != null && value9.ToString() != Value.ToString()) || value9 == 0) goto prop9;
- // //prop10:
- // //MessageNotify.GetInstance.ShowRunLog($"写入配方重量,值{Value}:{value9}");
- // //modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
- // //var value10 = modbusTcp.Read<ushort>(DeviceAddress.Start.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- // //if ((value10 != null && value10.ToString() != 1.ToString())||value10==0) goto prop10;
- // //modbusTcp.Write(DeviceAddress.WeightSet.ToAdd(), Value);//写入配方重量
- // modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
- // Task.Run(() =>
- // {
- // while (true)
- // {
- // ushort c = modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content;
- // MessageNotify.GetInstance.ShowRunLog($"{IpAddress}:LW51,值:{modbusTcp.Read<ushort>(DeviceAddress.DeviceAlarmCode.ToAdd(), 1, BPA.Helper.DataFormat.CDAB).Content}");
- // string str = DecToBinary(c);
- // if (str[8].ToString() == 0.ToString())
- // {
- // modbusTcp.Write(DeviceAddress.Start.ToAdd(), (ushort)1);//设备启动写入
- // }
- // else
- // return;
- // }
-
-
- // });
-
- // MessageNotify.GetInstance.ShowRunLog($"{DeviceName},设置重量:{Value},味魔方启动配料");
- }
- }
-
- static string DecToBinary(int a)
- {
- int r;
- string s = "";
- do
- {
- r = a % 2;
- a /= 2;
- s = r + s;
- }
- while (a != 0);
- int length = s.Length;
- for (int i = 0; i < 16 - length; i++)
- {
- s = 0 + s;
- }
- return s;
- }
- }
- }
|