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.MORKSM.BK.PLC { public class MorksMachine : BasePeripheral { ModbusTcp modbusTcp = new ModbusTcp(); public string IpAddress { get; set; } public int Port { get; set; } public override void Init() { //读取数据 EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { if (@event == null) return; var par = @event as ReadModel; ushort address = (ushort)modbusTcp.GetAddress(par?.Address); object readData = new object(); if (par.Address.ToUpper().Contains("M")) { //modbusTcp.Read(address, CommandType.Coils, par.Length); modbusTcp.Readbool(address, par.Length, new Action((s) => { readData = s; })); } else if (par.Address.ToUpper().Contains("VW")) { readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length); } callBack?.Invoke(readData); //callBack = new EventCallBackHandle(readData); }); //写入数据 EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { if (@event == null) return; var par = @event as WriteModel; ushort address = (ushort)modbusTcp.GetAddress(par?.Address); if (par.Address.ToUpper().Contains("M")) { modbusTcp.Write(address, CommandType.Coils, par.Value); } else if (par.Address.ToUpper().Contains("VW")) { modbusTcp.Write(address, CommandType.HoldingRegisters, par.Value); } }); ////批量读取数据 //EventBus.EventBus.GetInstance().Subscribe<>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) //{ //}); } public override void Start() { modbusTcp.ModbusTcpConnect(IpAddress, Port); } public override void Stop() { } protected override void InitStatus() { } } }