Selaa lähdekoodia

mokrs

样式分支
pry 2 vuotta sitten
vanhempi
commit
9c6b4eb99a
10 muutettua tiedostoa jossa 273 lisäystä ja 25 poistoa
  1. +18
    -0
      BPASmartClient.MORKSM.BK.PLC/BPASmartClient.MORKSM.BK.PLC.csproj
  2. +72
    -0
      BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs
  3. +63
    -12
      BPASmartClient.Modbus/ModbusTcp.cs
  4. +14
    -0
      BPASmartClient.Model/PLC/ReadModel.cs
  5. +14
    -0
      BPASmartClient.Model/PLC/WriteModel.cs
  6. +7
    -0
      BPASmartClient.MorkS/BPASmartClient.MorkS.csproj
  7. +0
    -8
      BPASmartClient.MorkS/Class1.cs
  8. +40
    -0
      BPASmartClient.MorkS/Control.cs
  9. +38
    -5
      BPASmartClient/DeviceInfo.xml
  10. +7
    -0
      SmartClient.sln

+ 18
- 0
BPASmartClient.MORKSM.BK.PLC/BPASmartClient.MORKSM.BK.PLC.csproj Näytä tiedosto

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.EventBus\BPASmartClient.EventBus.csproj" />
<ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" />
<ProjectReference Include="..\BPASmartClient.Message\BPASmartClient.Message.csproj" />
<ProjectReference Include="..\BPASmartClient.Modbus\BPASmartClient.Modbus.csproj" />
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
</ItemGroup>

</Project>

+ 72
- 0
BPASmartClient.MORKSM.BK.PLC/MorksMachine.cs Näytä tiedosto

@@ -0,0 +1,72 @@
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 => modbusTcp ?? new ModbusTcp();
public string IpAddress { get; set; }
public int Port { get; set; }


public override void Init()
{
//读取布尔数据
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);
if (par.Address.ToUpper().Contains("M"))
{
modbusTcp.Read(address, CommandType.Coils, par.Length);
}
else if (par.Address.ToUpper().Contains("VW"))
{
modbusTcp.Read(address, CommandType.HoldingRegisters, par.Length);
}

});

//写入布尔数据
EventBus.EventBus.GetInstance().Subscribe<WriteModel>(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);
}
});

modbusTcp.ModbusTcpConnect(IpAddress, Port);
}

public override void Start()
{

}

public override void Stop()
{

}

protected override void InitStatus()
{

}
}
}

+ 63
- 12
BPASmartClient.Modbus/ModbusTcp.cs Näytä tiedosto

@@ -12,9 +12,9 @@ namespace BPASmartClient.Modbus
public class ModbusTcp
{

private volatile static ModbusTcp _Instance;
public static ModbusTcp GetInstance => _Instance ?? (_Instance = new ModbusTcp());
private ModbusTcp() { }
//private volatile static ModbusTcp _Instance;
//public static ModbusTcp GetInstance => _Instance ?? (_Instance = new ModbusTcp());
//private ModbusTcp() { }

private ModbusFactory modbusFactory;
private IModbusMaster master;
@@ -74,6 +74,37 @@ namespace BPASmartClient.Modbus
MessageLog.GetInstance.Show("ModbusTcp 连接成功!");
}

public int GetAddress(string address)
{
if (address == null) return -1;
if (address.Length > 0)
{
if (address.ToUpper().Contains("M") && address.Length >= 4)
{
var res = address.Substring(1).Split('.');
if (res != null && res.Length == 2)
{
if (int.TryParse(res[0], out int firstAddress) && int.TryParse(res[1], out int ExitAddress))
{
if (ExitAddress >= 0 && ExitAddress <= 7)
{
return (firstAddress * 8) + 320 + ExitAddress;
}
}
}
}
else if (address.ToUpper().Contains("VW") && address.Length > 3)
{
var res = address.Substring(2);
if (res != null && int.TryParse(res, out int tempAddress))
{
return (tempAddress / 2) + 100;
}
}
}
return -1;
}

public int GetBoolAddress(string address)
{
if (address != null && address.Length >= 4)
@@ -109,7 +140,7 @@ namespace BPASmartClient.Modbus
public void Readbool(ushort startAddress, ushort len, Action<bool[]> action)
{
object result;
result = Read(startAddress, ReadType.Coils, len);
result = Read(startAddress, CommandType.Coils, len);
if (result != null)
{
if (result is bool[] bools)
@@ -122,7 +153,7 @@ namespace BPASmartClient.Modbus
}
}

public object Read(ushort startAddress, ReadType readType, ushort num = 1, byte slaveAddress = 1)
public object Read(ushort startAddress, CommandType readType, ushort num = 1, byte slaveAddress = 1)
{
object result = new object();
if (tcpClient == null) return result;
@@ -131,16 +162,16 @@ namespace BPASmartClient.Modbus
{
switch (readType)
{
case ReadType.Coils:
case CommandType.Coils:
result = master.ReadCoils(slaveAddress, startAddress, num);
break;
case ReadType.Inputs:
case CommandType.Inputs:
result = master.ReadInputs(slaveAddress, startAddress, num);
break;
case ReadType.HoldingRegisters:
case CommandType.HoldingRegisters:
result = master.ReadHoldingRegisters(slaveAddress, startAddress, num);
break;
case ReadType.InputRegisters:
case CommandType.InputRegisters:
result = master.ReadInputRegisters(slaveAddress, startAddress, num);
break;
default:
@@ -174,7 +205,7 @@ namespace BPASmartClient.Modbus
return result;
}

public bool Write(ushort startAddress, WriteType writeType, object InputValue, byte slaveAddress = 1)
public bool Write(ushort startAddress, CommandType writeType, object InputValue, byte slaveAddress = 1)
{
bool result = false;
if (tcpClient == null) return result;
@@ -183,14 +214,14 @@ namespace BPASmartClient.Modbus
{
switch (writeType)
{
case WriteType.Coils:
case CommandType.Coils:
if (InputValue is bool boolValue)
master.WriteSingleCoil(slaveAddress, startAddress, boolValue);

if (InputValue is bool[] boolsValue)
master.WriteMultipleCoils(slaveAddress, startAddress, boolsValue);
break;
case WriteType.HoldingRegisters:
case CommandType.HoldingRegisters:
if (InputValue is ushort ushortValue)
master.WriteSingleRegister(slaveAddress, startAddress, ushortValue);

@@ -453,4 +484,24 @@ namespace BPASmartClient.Modbus
HoldingRegisters,
}

public enum CommandType
{
/// <summary>
/// 线圈操作
/// </summary>
Coils,
/// <summary>
/// 输入线圈操作
/// </summary>
Inputs,
/// <summary>
/// 保持寄存器操作
/// </summary>
HoldingRegisters,
/// <summary>
/// 输入寄存器操作
/// </summary>
InputRegisters,
}

}

+ 14
- 0
BPASmartClient.Model/PLC/ReadModel.cs Näytä tiedosto

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.Model.PLC
{
public class ReadModel : BaseEvent
{
public string Address { get; set; }
public ushort Length { get; set; }
}
}

+ 14
- 0
BPASmartClient.Model/PLC/WriteModel.cs Näytä tiedosto

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.Model.PLC
{
public class WriteModel : BaseEvent
{
public string Address { get; set; }
public object Value { get; set; }
}
}

+ 7
- 0
BPASmartClient.MorkS/BPASmartClient.MorkS.csproj Näytä tiedosto

@@ -4,4 +4,11 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Device\BPASmartClient.Device.csproj" />
<ProjectReference Include="..\BPASmartClient.EventBus\BPASmartClient.EventBus.csproj" />
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.MORKSM.BK.PLC\BPASmartClient.MORKSM.BK.PLC.csproj" />
</ItemGroup>

</Project>

+ 0
- 8
BPASmartClient.MorkS/Class1.cs Näytä tiedosto

@@ -1,8 +0,0 @@
using System;

namespace BPASmartClient.MorkS
{
public class Class1
{
}
}

+ 40
- 0
BPASmartClient.MorkS/Control.cs Näytä tiedosto

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using BPA.Message.Enum;
using BPASmartClient.Device;
using BPASmartClient.EventBus;
using BPASmartClient.Model;
using BPASmartClient.Peripheral;
using static BPASmartClient.EventBus.EventBus;

namespace BPASmartClient.MorkS
{
public class Control : IDevice
{
public string DeviceId { get; set; }
public string Name { get; set; }

public DeviceClientType DeviceType { get; }

public DeviceStatus Status { get; set; }

public void Initliaze(List<IPeripheral> peripherals)
{
peripherals.ForEach(item => { item.Init(); });
//EventBus.EventBus.GetInstance().Subscribe<Demo_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
//{

//});
}

public void StartMain()
{

}

public void Stop()
{

}
}
}

+ 38
- 5
BPASmartClient/DeviceInfo.xml Näytä tiedosto

@@ -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>
@@ -11,16 +11,49 @@
</Peripheral>
<Peripheral Module="BPASmartClient.DRCoffee.CoffeeMachine">
<Parameters>
<PortName>COM5</PortName>
<BaudRate>9600</BaudRate>
<PortName>COM5</PortName>
<BaudRate>9600</BaudRate>
</Parameters>
</Peripheral>
<Peripheral Module="BPASmartClient.SCChip.ICChipMachine">
<Parameters>
<PortName>COM5</PortName>
<BaudRate>9600</BaudRate>
<PortName>COM5</PortName>
<BaudRate>9600</BaudRate>
</Parameters>
</Peripheral>
</Peripherals>
</Device>-->

<Device Name="MorkS" Module="BPASmartClient.MorkS.Control" DeviceId="2">
<Peripherals>
<Peripheral Module="BPASmartClient.MORKSM.BK.PLC.MorksMachine">
<Parameters>
<IpAddress>127.0.0.1</IpAddress>
<Port>1</Port>
</Parameters>
</Peripheral>

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

</Peripherals>
</Device>

<!--<Device Name="MorkM" Module="BPASmartClient.MorkS.Control" DeviceId="2">
<Peripherals>
<Peripheral Module="BPASmartClient.MORKSM.BK.PLC.MorksMachine">
<Parameters>
<IpAddress>127.0.0.1</IpAddress>
<Port>1</Port>
</Parameters>
</Peripheral>
</Peripherals>
</Device>-->



</BPADevices>

+ 7
- 0
SmartClient.sln Näytä tiedosto

@@ -76,6 +76,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.CustomResour
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lebai.SDK", "Lebai.SDK\Lebai.SDK.csproj", "{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BPASmartClient.MORKSM.BK.PLC", "BPASmartClient.MORKSM.BK.PLC\BPASmartClient.MORKSM.BK.PLC.csproj", "{7F04A788-38B5-42CB-B601-70C657C953B8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -194,6 +196,10 @@ Global
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}.Release|Any CPU.Build.0 = Release|Any CPU
{7F04A788-38B5-42CB-B601-70C657C953B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F04A788-38B5-42CB-B601-70C657C953B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F04A788-38B5-42CB-B601-70C657C953B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F04A788-38B5-42CB-B601-70C657C953B8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -227,6 +233,7 @@ Global
{EF5EF8EF-4A44-4E44-9594-D878CABC4182} = {6CEA3385-6F62-452A-8275-033A6037235D}
{CB1BC55F-D267-4724-89BE-96E3A5E432A6} = {8712125E-14CD-4E1B-A1CE-4BDE03805942}
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2} = {666CB1A9-562E-453A-A2C7-FD9D77CFDFDD}
{7F04A788-38B5-42CB-B601-70C657C953B8} = {666CB1A9-562E-453A-A2C7-FD9D77CFDFDD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9AEC9B81-0222-4DE9-B642-D915C29222AC}


Ladataan…
Peruuta
Tallenna