Browse Source

调整PLC主动读取模式

样式分支
applelon 2 years ago
parent
commit
256f1cb368
4 changed files with 124 additions and 29 deletions
  1. +41
    -14
      BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs
  2. +63
    -0
      BPASmartClient.MORKSM.BK.PLC/PLCReadParameter.cs
  3. +4
    -0
      BPASmartClient.MorkS/Control.cs
  4. +16
    -15
      BPASmartClient/DeviceInfo.xml

+ 41
- 14
BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs View File

@@ -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<PLCReadParameter> plcReadParameters;

public override void Init()
{
plcReadParameters = PLCReadParameter.DeSerialize(PLCReadAddress);

modbusTcp.ModbusTcpConnect(IpAddress, Port);//PLC 设备连接

//读取数据
EventBus.EventBus.GetInstance().Subscribe<ReadModel>(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<bool[]>((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<bool[]>((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<ReadModel>(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<bool[]>((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<WriteModel>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)


+ 63
- 0
BPASmartClient.MORKSM.BK.PLC/PLCReadParameter.cs View File

@@ -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
{
/// <summary>
/// PLC读取参数
/// </summary>
internal class PLCReadParameter
{
/// <summary>
/// 读取类型
/// </summary>
internal CommandType CmdType { get; set; }
/// <summary>
/// 地址
/// </summary>
internal string Address { get; set; }
/// <summary>
/// 长度
/// </summary>
internal ushort Length { get; set; }

/// <summary>
/// 配置转模型
/// </summary>
/// <param name="content">M,M0.3,3;M,M100.0,16;M,M235.0,1;M,M102,7;M,M103,6;VW,VW372,1</param>
/// <returns>读取参数模型</returns>
public static List<PLCReadParameter> DeSerialize(string content)
{
List<PLCReadParameter> result = new List<PLCReadParameter>();
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;
}
}
}

+ 4
- 0
BPASmartClient.MorkS/Control.cs View File

@@ -105,8 +105,11 @@ namespace BPASmartClient.MorkS

private void ReadPLCData()
{
/*替换为内部主动读取
ThreadManage.GetInstance().StartLong(new Action(() =>
{

ReadData("M0.3", 3, new Action<bool[]>((bools) =>
{
mORKS.RobotTakeNoodle = bools[0];
@@ -182,6 +185,7 @@ namespace BPASmartClient.MorkS

Thread.Sleep(500);
}), "ReadPLCData", true);
*/
}

/// <summary>


+ 16
- 15
BPASmartClient/DeviceInfo.xml View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<BPADevices>
<!--<Device Name="MorkT" Module="BPASmartClient.MorkT.Device_MorkT" DeviceId="1">
<!--<Device Name="MorkT" Module="BPASmartClient.MorkT.Device_MorkT" DeviceId="1">
<Peripherals>
<Peripheral Module="BPASmartClient.Lebai.LebaiRobot">
<Parameters>
@@ -19,35 +19,36 @@
</Peripherals>
</Device>-->

<Device Name="MorkS" Module="BPASmartClient.MorkS.Control" DeviceId="2">
<!--<Parameters>
<Device Name="MorkS" Module="BPASmartClient.MorkS.Control" DeviceId="2">
<!--<Parameters>
<IpAddress>127.0.10.1</IpAddress>
<Port>11</Port>
</Parameters>-->
<Peripherals>
<Peripheral Module="BPASmartClient.PLC.MorksMachine">
<Parameters>
<IpAddress>127.0.0.1</IpAddress>
<Port>502</Port>
</Parameters>
</Peripheral>
<Peripherals>
<Peripheral Module="BPASmartClient.PLC.MorksMachine">
<Parameters>
<IpAddress>127.0.0.1</IpAddress>
<Port>502</Port>
<PLCReadAddress>M,M0.3,3;M,M100.0,16;M,M235.0,1;M,M102,7;M,M103,6;VW,VW372,1</PLCReadAddress>
</Parameters>
</Peripheral>

<!--<Peripheral Module="BPASmartClient.MORKSM.BK.PLC.MorksMachine">
<!--<Peripheral Module="BPASmartClient.MORKSM.BK.PLC.MorksMachine">
<Parameters>
<IpAddress>127.0.10.1</IpAddress>
<Port>11</Port>
</Parameters>
</Peripheral>-->

<Peripheral Module="BPASmartClient.KLMCoffee.CoffeeMachine">
<!--<Peripheral Module="BPASmartClient.KLMCoffee.CoffeeMachine">
<Parameters>
<PortName>COM8</PortName>
<BaudRate>38400</BaudRate>
</Parameters>
</Peripheral>
</Peripheral>-->

</Peripherals>
</Device>
</Peripherals>
</Device>





Loading…
Cancel
Save