@@ -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> |
@@ -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() | |||||
{ | |||||
} | |||||
} | |||||
} |
@@ -12,9 +12,9 @@ namespace BPASmartClient.Modbus | |||||
public class ModbusTcp | 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 ModbusFactory modbusFactory; | ||||
private IModbusMaster master; | private IModbusMaster master; | ||||
@@ -74,6 +74,37 @@ namespace BPASmartClient.Modbus | |||||
MessageLog.GetInstance.Show("ModbusTcp 连接成功!"); | 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) | public int GetBoolAddress(string address) | ||||
{ | { | ||||
if (address != null && address.Length >= 4) | if (address != null && address.Length >= 4) | ||||
@@ -109,7 +140,7 @@ namespace BPASmartClient.Modbus | |||||
public void Readbool(ushort startAddress, ushort len, Action<bool[]> action) | public void Readbool(ushort startAddress, ushort len, Action<bool[]> action) | ||||
{ | { | ||||
object result; | object result; | ||||
result = Read(startAddress, ReadType.Coils, len); | |||||
result = Read(startAddress, CommandType.Coils, len); | |||||
if (result != null) | if (result != null) | ||||
{ | { | ||||
if (result is bool[] bools) | 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(); | object result = new object(); | ||||
if (tcpClient == null) return result; | if (tcpClient == null) return result; | ||||
@@ -131,16 +162,16 @@ namespace BPASmartClient.Modbus | |||||
{ | { | ||||
switch (readType) | switch (readType) | ||||
{ | { | ||||
case ReadType.Coils: | |||||
case CommandType.Coils: | |||||
result = master.ReadCoils(slaveAddress, startAddress, num); | result = master.ReadCoils(slaveAddress, startAddress, num); | ||||
break; | break; | ||||
case ReadType.Inputs: | |||||
case CommandType.Inputs: | |||||
result = master.ReadInputs(slaveAddress, startAddress, num); | result = master.ReadInputs(slaveAddress, startAddress, num); | ||||
break; | break; | ||||
case ReadType.HoldingRegisters: | |||||
case CommandType.HoldingRegisters: | |||||
result = master.ReadHoldingRegisters(slaveAddress, startAddress, num); | result = master.ReadHoldingRegisters(slaveAddress, startAddress, num); | ||||
break; | break; | ||||
case ReadType.InputRegisters: | |||||
case CommandType.InputRegisters: | |||||
result = master.ReadInputRegisters(slaveAddress, startAddress, num); | result = master.ReadInputRegisters(slaveAddress, startAddress, num); | ||||
break; | break; | ||||
default: | default: | ||||
@@ -174,7 +205,7 @@ namespace BPASmartClient.Modbus | |||||
return result; | 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; | bool result = false; | ||||
if (tcpClient == null) return result; | if (tcpClient == null) return result; | ||||
@@ -183,14 +214,14 @@ namespace BPASmartClient.Modbus | |||||
{ | { | ||||
switch (writeType) | switch (writeType) | ||||
{ | { | ||||
case WriteType.Coils: | |||||
case CommandType.Coils: | |||||
if (InputValue is bool boolValue) | if (InputValue is bool boolValue) | ||||
master.WriteSingleCoil(slaveAddress, startAddress, boolValue); | master.WriteSingleCoil(slaveAddress, startAddress, boolValue); | ||||
if (InputValue is bool[] boolsValue) | if (InputValue is bool[] boolsValue) | ||||
master.WriteMultipleCoils(slaveAddress, startAddress, boolsValue); | master.WriteMultipleCoils(slaveAddress, startAddress, boolsValue); | ||||
break; | break; | ||||
case WriteType.HoldingRegisters: | |||||
case CommandType.HoldingRegisters: | |||||
if (InputValue is ushort ushortValue) | if (InputValue is ushort ushortValue) | ||||
master.WriteSingleRegister(slaveAddress, startAddress, ushortValue); | master.WriteSingleRegister(slaveAddress, startAddress, ushortValue); | ||||
@@ -453,4 +484,24 @@ namespace BPASmartClient.Modbus | |||||
HoldingRegisters, | HoldingRegisters, | ||||
} | } | ||||
public enum CommandType | |||||
{ | |||||
/// <summary> | |||||
/// 线圈操作 | |||||
/// </summary> | |||||
Coils, | |||||
/// <summary> | |||||
/// 输入线圈操作 | |||||
/// </summary> | |||||
Inputs, | |||||
/// <summary> | |||||
/// 保持寄存器操作 | |||||
/// </summary> | |||||
HoldingRegisters, | |||||
/// <summary> | |||||
/// 输入寄存器操作 | |||||
/// </summary> | |||||
InputRegisters, | |||||
} | |||||
} | } |
@@ -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; } | |||||
} | |||||
} |
@@ -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; } | |||||
} | |||||
} |
@@ -4,4 +4,11 @@ | |||||
<TargetFramework>net6.0</TargetFramework> | <TargetFramework>net6.0</TargetFramework> | ||||
</PropertyGroup> | </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> | </Project> |
@@ -1,8 +0,0 @@ | |||||
using System; | |||||
namespace BPASmartClient.MorkS | |||||
{ | |||||
public class Class1 | |||||
{ | |||||
} | |||||
} |
@@ -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() | |||||
{ | |||||
} | |||||
} | |||||
} |
@@ -1,6 +1,6 @@ | |||||
<?xml version="1.0" encoding="utf-8" ?> | <?xml version="1.0" encoding="utf-8" ?> | ||||
<BPADevices> | <BPADevices> | ||||
<Device Name="MorkT" Module="BPASmartClient.MorkT.Device_MorkT" DeviceId="1"> | |||||
<!--<Device Name="MorkT" Module="BPASmartClient.MorkT.Device_MorkT" DeviceId="1"> | |||||
<Peripherals> | <Peripherals> | ||||
<Peripheral Module="BPASmartClient.Lebai.LebaiRobot"> | <Peripheral Module="BPASmartClient.Lebai.LebaiRobot"> | ||||
<Parameters> | <Parameters> | ||||
@@ -11,16 +11,49 @@ | |||||
</Peripheral> | </Peripheral> | ||||
<Peripheral Module="BPASmartClient.DRCoffee.CoffeeMachine"> | <Peripheral Module="BPASmartClient.DRCoffee.CoffeeMachine"> | ||||
<Parameters> | <Parameters> | ||||
<PortName>COM5</PortName> | |||||
<BaudRate>9600</BaudRate> | |||||
<PortName>COM5</PortName> | |||||
<BaudRate>9600</BaudRate> | |||||
</Parameters> | </Parameters> | ||||
</Peripheral> | </Peripheral> | ||||
<Peripheral Module="BPASmartClient.SCChip.ICChipMachine"> | <Peripheral Module="BPASmartClient.SCChip.ICChipMachine"> | ||||
<Parameters> | <Parameters> | ||||
<PortName>COM5</PortName> | |||||
<BaudRate>9600</BaudRate> | |||||
<PortName>COM5</PortName> | |||||
<BaudRate>9600</BaudRate> | |||||
</Parameters> | </Parameters> | ||||
</Peripheral> | </Peripheral> | ||||
</Peripherals> | </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> | ||||
<!--<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> | </BPADevices> |
@@ -76,6 +76,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BPASmartClient.CustomResour | |||||
EndProject | EndProject | ||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lebai.SDK", "Lebai.SDK\Lebai.SDK.csproj", "{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}" | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lebai.SDK", "Lebai.SDK\Lebai.SDK.csproj", "{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}" | ||||
EndProject | 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 | Global | ||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||||
Debug|Any CPU = Debug|Any CPU | 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}.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.ActiveCfg = Release|Any CPU | ||||
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2}.Release|Any CPU.Build.0 = 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 | EndGlobalSection | ||||
GlobalSection(SolutionProperties) = preSolution | GlobalSection(SolutionProperties) = preSolution | ||||
HideSolutionNode = FALSE | HideSolutionNode = FALSE | ||||
@@ -227,6 +233,7 @@ Global | |||||
{EF5EF8EF-4A44-4E44-9594-D878CABC4182} = {6CEA3385-6F62-452A-8275-033A6037235D} | {EF5EF8EF-4A44-4E44-9594-D878CABC4182} = {6CEA3385-6F62-452A-8275-033A6037235D} | ||||
{CB1BC55F-D267-4724-89BE-96E3A5E432A6} = {8712125E-14CD-4E1B-A1CE-4BDE03805942} | {CB1BC55F-D267-4724-89BE-96E3A5E432A6} = {8712125E-14CD-4E1B-A1CE-4BDE03805942} | ||||
{3A55F68A-D526-4CFC-A5A6-B69FB76716C2} = {666CB1A9-562E-453A-A2C7-FD9D77CFDFDD} | {3A55F68A-D526-4CFC-A5A6-B69FB76716C2} = {666CB1A9-562E-453A-A2C7-FD9D77CFDFDD} | ||||
{7F04A788-38B5-42CB-B601-70C657C953B8} = {666CB1A9-562E-453A-A2C7-FD9D77CFDFDD} | |||||
EndGlobalSection | EndGlobalSection | ||||
GlobalSection(ExtensibilityGlobals) = postSolution | GlobalSection(ExtensibilityGlobals) = postSolution | ||||
SolutionGuid = {9AEC9B81-0222-4DE9-B642-D915C29222AC} | SolutionGuid = {9AEC9B81-0222-4DE9-B642-D915C29222AC} | ||||