From 256f1cb36890a96a6c24161e7ea099b1e8da6b93 Mon Sep 17 00:00:00 2001 From: applelon <380149513@qq.com> Date: Fri, 29 Apr 2022 10:19:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4PLC=E4=B8=BB=E5=8A=A8?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs | 55 +++++++++++----- .../PLCReadParameter.cs | 63 +++++++++++++++++++ BPASmartClient.MorkS/Control.cs | 4 ++ BPASmartClient/DeviceInfo.xml | 31 ++++----- 4 files changed, 124 insertions(+), 29 deletions(-) create mode 100644 BPASmartClient.MORKSM.BK.PLC/PLCReadParameter.cs diff --git a/BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs b/BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs index 8c803dca..4d3fbdb8 100644 --- a/BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs +++ b/BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs @@ -14,29 +14,56 @@ namespace BPASmartClient.PLC ModbusTcp modbusTcp = new ModbusTcp(); public string IpAddress { get; set; } public int Port { get; set; } + public string PLCReadAddress { get; set; } + private List plcReadParameters; public override void Init() { + plcReadParameters = PLCReadParameter.DeSerialize(PLCReadAddress); + modbusTcp.ModbusTcpConnect(IpAddress, Port);//PLC 设备连接 - //读取数据 - EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) + ThreadManage.GetInstance().StartLong(new Action(() => { - 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.Readbool(address, par.Length, new Action((s) => { readData = s; })); - } - else if (par.Address.ToUpper().Contains("VW")) + while (modbusTcp.Connected) { - readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length); + foreach (var par in plcReadParameters) + { + ushort address = (ushort)modbusTcp.GetAddress(par?.Address); + object readData = new object(); + switch (par.CmdType) { + case CommandType.Coils: + modbusTcp.Readbool(address, par.Length, new Action((s) => { readData = s; })); + break; + case CommandType.HoldingRegisters: + readData = modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length); + break; + } + status[par.Address] = readData; + } + Thread.Sleep(500); } - callBack?.Invoke(readData); - }); + Thread.Sleep(1000); + }), $"设备[{DeviceId}]PLC读取线程", true); + + //读取数据 + 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.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); + }); //写入数据 EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) diff --git a/BPASmartClient.MORKSM.BK.PLC/PLCReadParameter.cs b/BPASmartClient.MORKSM.BK.PLC/PLCReadParameter.cs new file mode 100644 index 00000000..d95ebdd1 --- /dev/null +++ b/BPASmartClient.MORKSM.BK.PLC/PLCReadParameter.cs @@ -0,0 +1,63 @@ +using BPASmartClient.Modbus; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.PLC +{ + /// + /// PLC读取参数 + /// + internal class PLCReadParameter + { + /// + /// 读取类型 + /// + internal CommandType CmdType { get; set; } + /// + /// 地址 + /// + internal string Address { get; set; } + /// + /// 长度 + /// + internal ushort Length { get; set; } + + /// + /// 配置转模型 + /// + /// M,M0.3,3;M,M100.0,16;M,M235.0,1;M,M102,7;M,M103,6;VW,VW372,1 + /// 读取参数模型 + public static List DeSerialize(string content) + { + List result = new List(); + var wholeTemp = content.Split(';'); + foreach (var temp in wholeTemp) + { + if (temp.Contains(",")) + { + var subTemp = temp.Split(','); + if (null != subTemp && subTemp.Length == 3) + { + PLCReadParameter parameter = new PLCReadParameter(); + switch (subTemp[0]) + { + case "M": + parameter.CmdType = CommandType.Coils; + break; + case "VW": + parameter.CmdType = CommandType.HoldingRegisters; + break; + } + parameter.Address = subTemp[1]; + parameter.Length = ushort.Parse(subTemp[2]); + result.Add(parameter); + } + } + } + return result; + } + } +} diff --git a/BPASmartClient.MorkS/Control.cs b/BPASmartClient.MorkS/Control.cs index 3603e4fc..a5be4764 100644 --- a/BPASmartClient.MorkS/Control.cs +++ b/BPASmartClient.MorkS/Control.cs @@ -105,8 +105,11 @@ namespace BPASmartClient.MorkS private void ReadPLCData() { + /*替换为内部主动读取 ThreadManage.GetInstance().StartLong(new Action(() => { + + ReadData("M0.3", 3, new Action((bools) => { mORKS.RobotTakeNoodle = bools[0]; @@ -182,6 +185,7 @@ namespace BPASmartClient.MorkS Thread.Sleep(500); }), "ReadPLCData", true); + */ } /// diff --git a/BPASmartClient/DeviceInfo.xml b/BPASmartClient/DeviceInfo.xml index 55b7235e..00204240 100644 --- a/BPASmartClient/DeviceInfo.xml +++ b/BPASmartClient/DeviceInfo.xml @@ -1,6 +1,6 @@  - - - - - - - 127.0.0.1 - 502 - - + + + + 127.0.0.1 + 502 + M,M0.3,3;M,M100.0,16;M,M235.0,1;M,M102,7;M,M103,6;VW,VW372,1 + + - - + - - + +