using BPASmartClient.Peripheral; using BPASmartClient.Helper; using BPASmartClient.Message; using BPASmartClient.EventBus; using BPASmartClient.Modbus; using static BPASmartClient.EventBus.EventBus; using BPASmartClient.Model; using BPASmartClient.Model.PLC; namespace BPASmartClient.PLC { public class PLCMachine : BasePeripheral { ModbusTcp modbusTcp = new ModbusTcp(); public override void Init() { Task.Run(new Action(() => { modbusTcp.ModbusTcpConnect(communicationPar.IPAddress, communicationPar.IPPort); })); //PLC 设备连接 ThreadManage.GetInstance().StartLong(new Action(() => { IsConnected = modbusTcp.Connected; if (!IsConnected) IsWork = false; while (IsConnected) { IsWork = true; foreach (var par in variables) { if (par?.Address.Length > 0) { var res = modbusTcp.Read(par.Address, (ushort)par.ReadLeng); if (status.ContainsKey(par.Address)) { status[par.Address] = res; } else { status.TryAdd(par.Address, res); } } } Thread.Sleep(500); } Thread.Sleep(1000); }), $"设备[{DeviceId}]PLC读取线程", true); //写入数据 EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { if (@event == null) return; var par = @event as WriteModel; modbusTcp.Write(par?.Address, par?.Value); }); } //public override void ReadData(string address) //{ //} public override void Start() { } public override void Stop() { } public override void WriteData(string address, object value) { if (address != null && value != null) modbusTcp.Write(address, value); } protected override void InitStatus() { } } }