|
-
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.Helper;
- using BPASmartClient.Message;
- using BPASmartClient.Modbus;
- using FryPot_DosingSystem.Model;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
-
- namespace FryPot_DosingSystem.Control
- {
- internal class DeviceOperate
- {
- private static DeviceOperate _instance;
- public static DeviceOperate GetInstance => _instance ??= new DeviceOperate();
- public bool IsConfig { get; set; }//设备plc数据是否配置
- ModbusTcp modbus = new ModbusTcp();
- private string Ip { get; set; }
- private string Port { get; set; }
- public bool Connected { get; set; }
-
- private string DeviceName { get; set; }
-
- public ConcurrentDictionary<string, object> Data { get; set; } = new ConcurrentDictionary<string, object>();
- public ObservableCollection<PlcVariableModel> Variables { get; set; } = new ObservableCollection<PlcVariableModel>();
- public DeviceOperate()
- {
- Init();
- Connect();
- ReadData();
- }
- public void Init()
- {
- if (Variables.Count > 0)
- {
- Variables.Clear();
- }
- Json<PlcVariableInfoManage>.Read();
- if (Json<PlcVariableInfoManage>.Data.VariablesInfo.Count > 0)
- {
- try
- {
- foreach (var item in Json<PlcVariableInfoManage>.Data.VariablesInfo)
- {
- Variables.Add(new PlcVariableModel { Address = item.PlcAddress, Length = (ushort)(item.Length == null ? 0 : item.Length) });
- }
- IsConfig = true;
- }
- catch (Exception)
- {
- IsConfig = false;
- //throw;
- }
- }
- else
- {
- IsConfig= false;
- }
- //Variables.Add(new PlcVariableModel() { Address = "D2001", Length = 8 });//1号线体滚筒工位号
- //Variables.Add(new PlcVariableModel() { Address = "D2011", Length = 8 });//2号线体滚筒工位号
- //Variables.Add(new PlcVariableModel() { Address = "D2021", Length = 8 });//3号线体滚筒工位号
- //Variables.Add(new PlcVariableModel() { Address = "D2031", Length = 9 });//输送线出料状态
- //Variables.Add(new PlcVariableModel() { Address = "D2040", Length = 5 });//炒锅1-5进料滚筒运行
- //Variables.Add(new PlcVariableModel() { Address = "D2045", Length = 5 });//炒锅1-5进料到位信号
- //Variables.Add(new PlcVariableModel() { Address = "D2050", Length = 5 });//炒锅1-5空桶到位信号
- //Variables.Add(new PlcVariableModel() { Address = "D2055", Length = 5 });//炒锅1-5空桶呼叫AGV
- //Variables.Add(new PlcVariableModel() { Address = "D2060", Length = 5 });//炒锅1空桶洗桶呼叫AGV
- //Variables.Add(new PlcVariableModel() { Address = "D2065", Length = 5 });//炒锅1-5空桶滚筒运行
- //Variables.Add(new PlcVariableModel() { Address = "D2070", Length = 5 });//炒锅1-5滚筒故障信号
- //Variables.Add(new PlcVariableModel() { Address = "D2075", Length = 1 });//洗桶进桶滚筒运行信号
- //Variables.Add(new PlcVariableModel() { Address = "D2076", Length = 1 });//洗桶出桶呼叫AGV
- //Variables.Add(new PlcVariableModel() { Address = "D2077", Length = 1 });// 洗桶出桶滚筒运行信号
- //Variables.Add(new PlcVariableModel() { Address = "D2078", Length = 3 });//1-3滚筒线体配方完成信号
- }
- public void Connect()
- {
- if (IsConfig)
- {
- Json<DeviceManage>.Read();
- DeviceManage devices = Json<DeviceManage>.Data;
- if (devices != null)
- {
- if (devices.Devices.Count > 0)
- {
- Ip = devices.Devices[0].Ip;
- Port = devices.Devices[0].Port;
- DeviceName = devices.Devices[0].DeviceName;
- Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); });
- // Task.Run(() => { modbus.ModbusTcpConnect(Ip, Convert.ToInt32(Port)); App.Current.Dispatcher.Invoke(new Action(() => { BPASmartClient.CustomResource.Pages.Model.MessageLog.GetInstance.RunLog("PLC连接成功"); })); });
- }
- }
- }
- }
- public void ReadData()
- {
- if (IsConfig)
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- Connected = modbus.Connected;
- while (Connected)
- {
- foreach (var item in Variables)
- {
- var res = modbus.Read(item.Address, item.Length);//读取plc数据
- if (Data.ContainsKey(item.Address))
- {
- Data[item.Address] = res;
- }
- else
- {
- Data.TryAdd(item.Address, res);
- }
- }
- Thread.Sleep(500);
-
- }
- Thread.Sleep(1000);
- }), $"设备【{DeviceName}】PLC实时数据读取线程");
- }
- }
- public void WritePlcData(string address, ushort value)
- {
- lock (this)
- {
- modbus.Write(address, value);
- }
- }
- public ConcurrentDictionary<string, object> GetAllData()
- {
- return Data;
- }
- }
- }
|