Browse Source

加载模块修改

样式分支
pry 2 years ago
parent
commit
c156d2cf82
26 changed files with 477 additions and 298 deletions
  1. +32
    -32
      BPASmartClient.Business/Plugin/ConfigMgr.cs
  2. +76
    -28
      BPASmartClient.Business/Plugin/DeviceMgr.cs
  3. +0
    -1
      BPASmartClient.DRCoffee/BPASmartClient.DRCoffee.csproj
  4. +2
    -1
      BPASmartClient.Device/AlarmHelper.cs
  5. +2
    -2
      BPASmartClient.Device/BaseDevice.cs
  6. +10
    -10
      BPASmartClient.Device/DeviceStatus.cs
  7. +13
    -1
      BPASmartClient.Device/IDevice.cs
  8. +0
    -1
      BPASmartClient.GSIceCream/BPASmartClient.GSIceCream.csproj
  9. +1
    -1
      BPASmartClient.Helper/Sqlite.cs
  10. +0
    -1
      BPASmartClient.KLMCoffee/BPASmartClient.KLMCoffee.csproj
  11. +0
    -1
      BPASmartClient.Lebai/BPASmartClient.Lebai.csproj
  12. +0
    -1
      BPASmartClient.MORKSM.BK.PLC/BPASmartClient.PLC.csproj
  13. +74
    -42
      BPASmartClient.MORKSM.BK.PLC/PLCMachine.cs
  14. +0
    -4
      BPASmartClient.Message/BPASmartClient.Message.csproj
  15. +21
    -21
      BPASmartClient.Message/MessageLog.cs
  16. +198
    -131
      BPASmartClient.Modbus/ModbusTcp.cs
  17. +3
    -1
      BPASmartClient.Model/DeviceConfigModelJson.cs
  18. +7
    -7
      BPASmartClient.MorkS/Control_Morks.cs
  19. +4
    -0
      BPASmartClient.Peripheral/BPASmartClient.Peripheral.csproj
  20. +7
    -2
      BPASmartClient.Peripheral/BasePeripheral.cs
  21. +19
    -1
      BPASmartClient.Peripheral/IPeripheral.cs
  22. +0
    -1
      BPASmartClient.SCChip/BPASmartClient.SCChip.csproj
  23. +1
    -1
      BPASmartClient/App.xaml.cs
  24. +2
    -2
      BPASmartClient/Control/ShopDeviceConfigView.xaml
  25. +3
    -3
      BPASmartClient/Control/ShopDeviceConfigView.xaml.cs
  26. +2
    -2
      BPASmartClient/DeviceInfo.xml

+ 32
- 32
BPASmartClient.Business/Plugin/ConfigMgr.cs View File

@@ -95,38 +95,38 @@ namespace BPASmartClient.Business



deviceConfigs = new List<DeviceConfig>();
var devicePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DeviceInfo.xml");
var xdoc = XDocument.Load(devicePath);
var devices = xdoc.XPathSelectElements("//Device");
foreach (var device in devices)
{
DeviceConfig deviceConfig = new DeviceConfig();
deviceConfig.Name = device.Attribute("Name").Value;
deviceConfig.Module = device.Attribute("Module").Value;
deviceConfig.DeviceId = int.Parse(device.Attribute("DeviceId").Value);
var Peripherals = device.XPathSelectElements("Peripherals/Peripheral");
if (Peripherals != null)
{
foreach (var Per in Peripherals)
{
BPASmartClient.Model.Peripheral peripheral = new BPASmartClient.Model.Peripheral();
peripheral.Module = Per.Attribute("Module").Value;
var Parameters = Per.Element("Parameters").Elements();
if (Parameters != null)
{
foreach (var item in Parameters)
{
peripheral.Parameters.Add(item.Name.LocalName, item.Value);
}
}
deviceConfig.Peripherals.Add(peripheral);
}
}
deviceConfigs.Add(deviceConfig);
}
//deviceConfigs = new List<DeviceConfig>();
//var devicePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DeviceInfo.xml");
//var xdoc = XDocument.Load(devicePath);
//var devices = xdoc.XPathSelectElements("//Device");
//foreach (var device in devices)
//{
// DeviceConfig deviceConfig = new DeviceConfig();
// deviceConfig.Name = device.Attribute("Name").Value;
// deviceConfig.Module = device.Attribute("Module").Value;
// deviceConfig.DeviceId = int.Parse(device.Attribute("DeviceId").Value);
// var Peripherals = device.XPathSelectElements("Peripherals/Peripheral");
// if (Peripherals != null)
// {
// foreach (var Per in Peripherals)
// {
// BPASmartClient.Model.Peripheral peripheral = new BPASmartClient.Model.Peripheral();
// peripheral.Module = Per.Attribute("Module").Value;
// var Parameters = Per.Element("Parameters").Elements();
// if (Parameters != null)
// {
// foreach (var item in Parameters)
// {
// peripheral.Parameters.Add(item.Name.LocalName, item.Value);
// }
// }
// deviceConfig.Peripherals.Add(peripheral);
// }
// }
// deviceConfigs.Add(deviceConfig);
//}
}

/// <summary>


+ 76
- 28
BPASmartClient.Business/Plugin/DeviceMgr.cs View File

@@ -9,6 +9,7 @@ using BPASmartClient.Peripheral;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
@@ -47,31 +48,76 @@ namespace BPASmartClient.Business
/// </summary>
private void LoadDevice()
{
var devices = Plugin.GetInstance().GetPlugin<ConfigMgr>().GetDeviceConfigs();
foreach (var device in devices)

#region 启动设备对象

var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig");
string path = $"{LocaPath.GetInstance().GetDeviceConfigPath}{text}.json";
if (File.Exists(path))
{
var deviceTemp = Assembly.Load(device.Module.Substring(0, device.Module.LastIndexOf('.'))).CreateInstance(device.Module) as IDevice;
deviceTemp.Name = device.Name;
deviceTemp.DeviceId = device.DeviceId;
foreach (var pars in device.Parameters)
string JsonString = File.ReadAllText(path);
var result = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
if (result != null)
{
deviceTemp.GetType().GetProperty(pars.Key).SetValue(deviceTemp, Convert.ChangeType(pars.Value, deviceTemp.GetType().GetProperty(pars.Key).PropertyType));
}
foreach (var shop in result)//店铺集合
{
foreach (var device in shop.deviceModels)//设备集合
{

List<IPeripheral> peripherals = new List<IPeripheral>();
foreach (var peripheral in device.Peripherals)
{
var peripheralTemp = Assembly.Load(peripheral.Module.Substring(0, peripheral.Module.LastIndexOf('.'))).CreateInstance(peripheral.Module) as IPeripheral;
string Namespace = device.DeviceNamespace.Substring(0, device.DeviceNamespace.LastIndexOf('.'));
//string startModel = device.DeviceNamespace.Substring(device.DeviceNamespace.LastIndexOf('.') + 1);
var deviceTemp = Assembly.Load(Namespace).CreateInstance(device.DeviceNamespace) as IDevice;
deviceTemp.Name = device?.DeviceName;
deviceTemp.DeviceId = int.Parse(device.DeviceId);

foreach (var pars in peripheral.Parameters)
{
peripheralTemp.GetType().GetProperty(pars.Key).SetValue(peripheralTemp, Convert.ChangeType(pars.Value, peripheralTemp.GetType().GetProperty(pars.Key).PropertyType));
//通讯模块
List<IPeripheral> peripherals = new List<IPeripheral>();
foreach (var comms in device.communicationDevcies)//通讯集合
{
string IPeripheralNamespace = comms.CommunicationNamespace.Substring(0, comms.CommunicationNamespace.LastIndexOf('.'));
//string IPeripheralStartModel = comms.CommunicationNamespace.Substring(comms.CommunicationNamespace.LastIndexOf('.'));
var peripheralTemp = Assembly.Load(IPeripheralNamespace).CreateInstance(comms.CommunicationNamespace) as IPeripheral;
peripheralTemp.variables = comms.variables;
peripheralTemp.communicationPar = comms.communicationPar;
peripherals.Add(peripheralTemp);
}

deviceTemp.Initliaze(peripherals);
this.devices.Add(deviceTemp);
}
}
peripherals.Add(peripheralTemp);
}
deviceTemp.Initliaze(peripherals);
this.devices.Add(deviceTemp);
}

#endregion



//var devices = Plugin.GetInstance().GetPlugin<ConfigMgr>().GetDeviceConfigs();
//foreach (var device in devices)
//{
// var deviceTemp = Assembly.Load(device.Module.Substring(0, device.Module.LastIndexOf('.'))).CreateInstance(device.Module) as IDevice;
// deviceTemp.Name = device.Name;
// deviceTemp.DeviceId = device.DeviceId;
// foreach (var pars in device.Parameters)
// {
// deviceTemp.GetType().GetProperty(pars.Key).SetValue(deviceTemp, Convert.ChangeType(pars.Value, deviceTemp.GetType().GetProperty(pars.Key).PropertyType));
// }

// List<IPeripheral> peripherals = new List<IPeripheral>();
// foreach (var peripheral in device.Peripherals)
// {
// var peripheralTemp = Assembly.Load(peripheral.Module.Substring(0, peripheral.Module.LastIndexOf('.'))).CreateInstance(peripheral.Module) as IPeripheral;

// foreach (var pars in peripheral.Parameters)
// {
// peripheralTemp.GetType().GetProperty(pars.Key).SetValue(peripheralTemp, Convert.ChangeType(pars.Value, peripheralTemp.GetType().GetProperty(pars.Key).PropertyType));
// }
// peripherals.Add(peripheralTemp);
// }
// deviceTemp.Initliaze(peripherals);
// this.devices.Add(deviceTemp);
//}
}

public void StartService()
@@ -95,21 +141,23 @@ namespace BPASmartClient.Business

if (PushType == 1)
{
EventBus.EventBus.GetInstance().Publish(new RecipeBomEvent()
{
DeviceId = device.DeviceId,
recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
});
device.recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result);
//EventBus.EventBus.GetInstance().Publish(new RecipeBomEvent()
//{
// DeviceId = device.DeviceId,
// recipeBoms = JsonConvert.DeserializeObject<RecipeBoms>(result)
//});
MessageLog.GetInstance.Show("接收到辅料信息");
}
else if (PushType == 0)
{
var apiData = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);
EventBus.EventBus.GetInstance().Publish(new MaterialDeliveryEvent()
{
DeviceId = device.DeviceId,
orderMaterialDelivery = apiData
});
device.orderMaterialDelivery = apiData;
//EventBus.EventBus.GetInstance().Publish(new MaterialDeliveryEvent()
//{
// DeviceId = device.DeviceId,
// orderMaterialDelivery = apiData
//});
MessageLog.GetInstance.Show("接收到物料信息");
apiData?.BatchingInfo?.ForEach(x =>
{


+ 0
- 1
BPASmartClient.DRCoffee/BPASmartClient.DRCoffee.csproj View File

@@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
<ProjectReference Include="..\BPASmartClient.SerialPort\BPASmartClient.SerialPort.csproj" />
</ItemGroup>


+ 2
- 1
BPASmartClient.Device/AlarmHelper.cs View File

@@ -59,7 +59,8 @@ namespace BPASmartClient.Device
Time = DateTime.Now.ToString("HH:mm:ss"),
};

Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
var res = Sqlite<Alarm>.GetInstance.Base.Add(tempAlarm);
Sqlite<Alarm>.GetInstance.Save();

if (Alarms.FirstOrDefault(p => p.Info == AlarmInfo) == null)
{


+ 2
- 2
BPASmartClient.Device/BaseDevice.cs View File

@@ -28,12 +28,12 @@ namespace BPASmartClient.Device
/// <summary>
/// 订单物料信息
/// </summary>
protected OrderMaterialDelivery orderMaterialDelivery { get; set; } = new OrderMaterialDelivery();
public OrderMaterialDelivery orderMaterialDelivery { get; set; } = new OrderMaterialDelivery();

/// <summary>
/// 配方数据信息
/// </summary>
protected RecipeBoms recipeBoms { get; set; } = new RecipeBoms();
public RecipeBoms recipeBoms { get; set; } = new RecipeBoms();

/// <summary>
/// 设备ID


+ 10
- 10
BPASmartClient.Device/DeviceStatus.cs View File

@@ -42,7 +42,7 @@ namespace BPASmartClient.Device


};
public ConcurrentDictionary<string, object> status = new ConcurrentDictionary<string, object>();

public void Update(string key, object value)
@@ -68,7 +68,7 @@ namespace BPASmartClient.Device
{
sta = item.Key.Split('.')[0];
string sop = item.Key.Split('.')[item.Key.Split('.').Length - 1]; name = sop;
string value = item.Value.ToString();
string value = item.Value?.ToString();
if (keyValues.ContainsKey(sta)) sta = keyValues[sta];
if (keyValues.ContainsKey(sop)) sop = keyValues[sop];
if (keyValues.ContainsKey(value)) value = keyValues[value];
@@ -89,21 +89,21 @@ namespace BPASmartClient.Device
{
string name = item.Key;
string Ms = string.Empty;
string id=string.Empty;
string sta=string.Empty;
string id = string.Empty;
string sta = string.Empty;
if (item.Key.Contains("."))
{
sta = item.Key.Split('.')[0];
string sop = item.Key.Split('.')[item.Key.Split('.').Length - 1]; name = sop;
string value = item.Value.ToString();
string value = item.Value?.ToString();
if (keyValues.ContainsKey(sta)) sta = keyValues[sta];
if (keyValues.ContainsKey(sop)) sop = keyValues[sop];
if (keyValues.ContainsKey(value)) value = keyValues[value];

Ms = $"[{sta}]-[{sop}]-[{value}]";
id= $"[{sta}]-[{sop}]";
id = $"[{sta}]-[{sop}]";
}
StatusALL.Add(new DevStatus {id= id, Name = name,type=sta, Status = item.Value.ToString(), Ms = Ms });
StatusALL.Add(new DevStatus { id = id, Name = name, type = sta, Status = item.Value.ToString(), Ms = Ms });
}
StatusALL = StatusALL?.OrderBy(x => x.type).ToList();
return StatusALL;
@@ -125,9 +125,9 @@ namespace BPASmartClient.Device
if (_status == "False" || _status == "True")
{
if (_status == "True")
StatusColor = new { r = 51, g = 232, b = 34, a = 1 };
StatusColor = new { r = 51, g = 232, b = 34, a = 1 };
else
StatusColor = new { r = 255, g = 0, b = 0, a = 1 };
StatusColor = new { r = 255, g = 0, b = 0, a = 1 };
}
}
}
@@ -135,7 +135,7 @@ namespace BPASmartClient.Device
public object StatusColor { get; set; }
public DevStatus()
{
StatusColor = new { r = 51, g = 232, b = 34, a = 1 };
StatusColor = new { r = 51, g = 232, b = 34, a = 1 };
}
}
}

+ 13
- 1
BPASmartClient.Device/IDevice.cs View File

@@ -1,4 +1,5 @@
using BPA.Message.Enum;
using BPA.Message;
using BPA.Message.Enum;
using BPASmartClient.Model;
using BPASmartClient.Peripheral;
using System;
@@ -27,6 +28,17 @@ namespace BPASmartClient.Device
/// 设备类型
/// </summary>
DeviceClientType DeviceType { get; }

/// <summary>
/// 订单物料信息
/// </summary>
OrderMaterialDelivery orderMaterialDelivery { get; set; }

/// <summary>
/// 配方数据信息
/// </summary>
RecipeBoms recipeBoms { get; set; }

/// <summary>
/// 设备所有状态
/// </summary>


+ 0
- 1
BPASmartClient.GSIceCream/BPASmartClient.GSIceCream.csproj View File

@@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
<ProjectReference Include="..\BPASmartClient.SerialPort\BPASmartClient.SerialPort.csproj" />
</ItemGroup>


+ 1
- 1
BPASmartClient.Helper/Sqlite.cs View File

@@ -36,7 +36,7 @@ namespace BPASmartClient.Helper
public void Save()
{
Database.EnsureCreated();
SaveChanges();
var a = SaveChanges();
}

public List<T> GetData()


+ 0
- 1
BPASmartClient.KLMCoffee/BPASmartClient.KLMCoffee.csproj View File

@@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
<ProjectReference Include="..\BPASmartClient.SerialPort\BPASmartClient.SerialPort.csproj" />
</ItemGroup>


+ 0
- 1
BPASmartClient.Lebai/BPASmartClient.Lebai.csproj View File

@@ -10,7 +10,6 @@
<ProjectReference Include="..\BPASmartClient.EventBus\BPASmartClient.EventBus.csproj" />
<ProjectReference Include="..\BPASmartClient.Helper\BPASmartClient.Helper.csproj" />
<ProjectReference Include="..\BPASmartClient.Message\BPASmartClient.Message.csproj" />
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
<ProjectReference Include="..\Lebai.SDK\Lebai.SDK.csproj" />
</ItemGroup>


+ 0
- 1
BPASmartClient.MORKSM.BK.PLC/BPASmartClient.PLC.csproj View File

@@ -11,7 +11,6 @@
<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>



+ 74
- 42
BPASmartClient.MORKSM.BK.PLC/PLCMachine.cs View File

@@ -12,17 +12,17 @@ namespace BPASmartClient.PLC
public class PLCMachine : BasePeripheral
{
ModbusTcp modbusTcp = new ModbusTcp();
public string IpAddress { get; set; }
public int Port { get; set; }
public string PLCReadAddress { get; set; }
//public string IpAddress { get; set; }
//public int Port { get; set; }
//public string PLCReadAddress { get; set; }

private List<PLCReadParameter> plcReadParameters;
//private List<PLCReadParameter> plcReadParameters;

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

Task.Run(new Action(() => { modbusTcp.ModbusTcpConnect(IpAddress, Port); })); //PLC 设备连接
Task.Run(new Action(() => { modbusTcp.ModbusTcpConnect(communicationPar.IPAddress, communicationPar.IPPort); })); //PLC 设备连接

ThreadManage.GetInstance().StartLong(new Action(() =>
{
@@ -31,59 +31,91 @@ namespace BPASmartClient.PLC
while (IsConnected)
{
IsWork = true;
foreach (var par in plcReadParameters)
foreach (var par in variables)
{
ushort address = (ushort)modbusTcp.GetAddress(par?.Address);
object readData = new object();
switch (par.CmdType)
if (par?.Address.Length > 0)
{
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;
var res = modbusTcp.READD(par.Address, (ushort)par.ReadLeng);
if (status.ContainsKey(par.Address))
{
status[par.Address] = res;
}
else
{
status.TryAdd(par.Address, res);
}
}
status[par.Address] = readData;

//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;
}


//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);
}
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<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)
{
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.WRITEE(par?.Address, par?.Value);
//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);
//}
});
}



+ 0
- 4
BPASmartClient.Message/BPASmartClient.Message.csproj View File

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

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
</ItemGroup>

</Project>

+ 21
- 21
BPASmartClient.Message/MessageLog.cs View File

@@ -1,4 +1,4 @@
using BPASmartClient.Peripheral;
//using BPASmartClient.Peripheral;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -135,26 +135,26 @@ namespace BPASmartClient.Message
public object ReturnDeviceID()
{
object DeviceId = null;
StackTrace trace = new StackTrace();
for (int i = 1; i < 10; i++)
{
Type type = trace.GetFrame(i).GetMethod().ReflectedType;
try
{
object obj = Activator.CreateInstance(type);
if (obj is IPeripheral)
{
IPeripheral peripheral = obj as IPeripheral;
DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null);
}
if (DeviceId != null) break;
}
catch (Exception ex)
{
}
}
//StackTrace trace = new StackTrace();
//for (int i = 1; i < 10; i++)
//{
// Type type = trace.GetFrame(i).GetMethod().ReflectedType;
// try
// {
// object obj = Activator.CreateInstance(type);
// if (obj is IPeripheral)
// {
// IPeripheral peripheral = obj as IPeripheral;
// DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null);
// }
// if (DeviceId != null) break;
// }
// catch (Exception ex)
// {
// }
//}

return DeviceId;
}


+ 198
- 131
BPASmartClient.Modbus/ModbusTcp.cs View File

@@ -107,179 +107,246 @@ namespace BPASmartClient.Modbus
return -1;
}

public int GetBoolAddress(string address)
//public void Readbool(ushort startAddress, ushort len, Action<bool[]> action)
//{
// object result;
// result = Read(startAddress, CommandType.Coils, len);
// if (result != null)
// {
// if (result is bool[] bools)
// {
// if (bools.Length == len)
// {
// action(bools);
// }
// }
// else if (result is bool bl)
// {
// List<bool> boolLists = new List<bool>();
// boolLists.Add(bl);
// action(boolLists.ToArray());
// }
// }
//}

public object READD(string address, ushort len = 1, byte slaveAddress = 1)
{
if (address != null && 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;
}
}
}
}
return -1;
}

public int GetWordAddress(string address)
{
if (address != null && address.Length > 3)
{
var res = address.Substring(2);
if (res != null && int.TryParse(res, out int tempAddress))
{
return (tempAddress / 2) + 100;
}
}
return -1;
}

public void Readbool(ushort startAddress, ushort len, Action<bool[]> action)
{
object result;
result = Read(startAddress, CommandType.Coils, len);
if (result != null)
{
if (result is bool[] bools)
{
if (bools.Length == len)
{
action(bools);
}
}
else if (result is bool bl)
{
List<bool> boolLists = new List<bool>();
boolLists.Add(bl);
action(boolLists.ToArray());
}
}
}

public object Read(ushort startAddress, CommandType readType, ushort num = 1, byte slaveAddress = 1)
{
object result = new object();
if (tcpClient == null) return result;
if (num <= 0) return result;
if (address == null || tcpClient == null) return default(object);
ushort startAddress = (ushort)GetAddress(address);
CommandType commandType = CommandType.Coils;
try
{
switch (readType)
if (address.ToUpper().Contains("M"))
{
case CommandType.Coils:
result = master.ReadCoils(slaveAddress, startAddress, num);
break;
case CommandType.Inputs:
result = master.ReadInputs(slaveAddress, startAddress, num);
break;
case CommandType.HoldingRegisters:
result = master.ReadHoldingRegisters(slaveAddress, startAddress, num);
break;
case CommandType.InputRegisters:
result = master.ReadInputRegisters(slaveAddress, startAddress, num);
break;
default:
break;
commandType = CommandType.Coils;
return master.ReadCoils(slaveAddress, startAddress, len);
}
if (result == null) return new object();
if (result is bool[] bools)
else if (address.ToUpper().Contains("VW"))
{
if (bools.Length == 1)
return bools[0];
else
return bools;
commandType = CommandType.HoldingRegisters;
return master.ReadHoldingRegisters(slaveAddress, startAddress, len);
}
if (result is ushort[] ushorts)
else if (address.ToUpper().Contains("I"))
{
if (ushorts.Length == 1)
return ushorts[0];
else
return ushorts;
commandType = CommandType.Inputs;
return master.ReadInputs(slaveAddress, startAddress, len);
}
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx($"读取地址:【{startAddress}】,读取类型:【{readType}】出错,{ex.ToString()}");
MessageLog.GetInstance.ShowEx($"读取地址:【{address}:= {startAddress}】,读取类型:【{commandType.ToString()}】出错,{ex.ToString()}");
if (ex.InnerException is SocketException)
{
tcpClient = null;
Connect();
}
}
return result;
return default(object);
}

public bool Write(ushort startAddress, CommandType writeType, object InputValue, byte slaveAddress = 1)
public void WRITEE<T>(string address, T value, byte slaveAddress = 1)
{
bool result = false;
if (tcpClient == null) return result;
if (!(InputValue is bool || InputValue is bool[] || InputValue is ushort || InputValue is ushort[])) return result;
if (address == null || tcpClient == null) return;
ushort startAddress = (ushort)GetAddress(address);
CommandType commandType = CommandType.Coils;
try
{
switch (writeType)
if (address.ToUpper().Contains("M"))
{
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 CommandType.HoldingRegisters:
if (InputValue is ushort ushortValue)
master.WriteSingleRegister(slaveAddress, startAddress, ushortValue);

if (InputValue is ushort[] ushortsValue)
commandType = CommandType.Coils;
if (value is bool boolValue)
master.WriteSingleCoil(slaveAddress, startAddress, boolValue);
else if (value is bool[] boolsValue)
master.WriteMultipleCoils(slaveAddress, startAddress, boolsValue);
}
else if (address.ToUpper().Contains("VW"))
{
commandType = CommandType.HoldingRegisters;
if (value is ushort ushortValue)
master.WriteSingleRegister(slaveAddress, startAddress, ushortValue);
else if (value is ushort[] ushortsValue)
{
int len = 100;
if (ushortsValue.Length > len)
{
int len = 100;
if (ushortsValue.Length > len)
List<ushort[]> ushortLists = new List<ushort[]>();
for (int i = 0; i < ushortsValue.Length / len; i++)
{
List<ushort[]> ushortLists = new List<ushort[]>();
for (int i = 0; i < ushortsValue.Length / len; i++)
{
ushortLists.Add(ushortsValue.Skip(0).Take(len).ToArray());
}
int y = ushortsValue.Length % len;

if (y > 0)
{
ushortLists.Add(ushortsValue.Skip(ushortsValue.Length - y).Take(y).ToArray());
}
foreach (var item in ushortLists)
{
master.WriteMultipleRegisters(slaveAddress, startAddress, item);
startAddress += (ushort)item.Length;
}
ushortLists.Add(ushortsValue.Skip(0).Take(len).ToArray());
}
else
int y = ushortsValue.Length % len;

if (y > 0)
{
master.WriteMultipleRegisters(slaveAddress, startAddress, ushortsValue);
ushortLists.Add(ushortsValue.Skip(ushortsValue.Length - y).Take(y).ToArray());
}
foreach (var item in ushortLists)
{
master.WriteMultipleRegisters(slaveAddress, startAddress, item);
startAddress += (ushort)item.Length;
}

}

break;
default:
break;
else
{
master.WriteMultipleRegisters(slaveAddress, startAddress, ushortsValue);
}
}
}
else if (address.ToUpper().Contains("I"))
{
commandType = CommandType.Inputs;
}
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx(ex.ToString());
MessageLog.GetInstance.ShowEx($"读写入地址:【{address}:= {startAddress}】,写入类型:【{commandType.ToString()}】出错,{ex.ToString()}");
if (ex.InnerException is SocketException)
{
tcpClient = null;
Connect();
}
return false;
}
return true;
}

//public object Read(ushort startAddress, CommandType readType, ushort num = 1, byte slaveAddress = 1)
//{
// object result = new object();
// if (tcpClient == null) return result;
// if (num <= 0) return result;
// try
// {
// switch (readType)
// {
// case CommandType.Coils:
// result = master.ReadCoils(slaveAddress, startAddress, num);
// break;
// case CommandType.Inputs:
// result = master.ReadInputs(slaveAddress, startAddress, num);
// break;
// case CommandType.HoldingRegisters:
// result = master.ReadHoldingRegisters(slaveAddress, startAddress, num);
// break;
// case CommandType.InputRegisters:
// result = master.ReadInputRegisters(slaveAddress, startAddress, num);
// break;
// default:
// break;
// }
// if (result == null) return new object();
// if (result is bool[] bools)
// {
// if (bools.Length == 1)
// return bools[0];
// else
// return bools;
// }
// if (result is ushort[] ushorts)
// {
// if (ushorts.Length == 1)
// return ushorts[0];
// else
// return ushorts;
// }
// }
// catch (Exception ex)
// {
// MessageLog.GetInstance.ShowEx($"读取地址:【{startAddress}】,读取类型:【{readType}】出错,{ex.ToString()}");
// if (ex.InnerException is SocketException)
// {
// tcpClient = null;
// Connect();
// }
// }
// return result;
//}

//public bool Write(ushort startAddress, CommandType writeType, object InputValue, byte slaveAddress = 1)
//{
// bool result = false;
// if (tcpClient == null) return result;
// if (!(InputValue is bool || InputValue is bool[] || InputValue is ushort || InputValue is ushort[])) return result;
// try
// {
// switch (writeType)
// {
// 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 CommandType.HoldingRegisters:
// if (InputValue is ushort ushortValue)
// master.WriteSingleRegister(slaveAddress, startAddress, ushortValue);

// if (InputValue is ushort[] ushortsValue)
// {
// int len = 100;
// if (ushortsValue.Length > len)
// {
// List<ushort[]> ushortLists = new List<ushort[]>();
// for (int i = 0; i < ushortsValue.Length / len; i++)
// {
// ushortLists.Add(ushortsValue.Skip(0).Take(len).ToArray());
// }
// int y = ushortsValue.Length % len;

// if (y > 0)
// {
// ushortLists.Add(ushortsValue.Skip(ushortsValue.Length - y).Take(y).ToArray());
// }
// foreach (var item in ushortLists)
// {
// master.WriteMultipleRegisters(slaveAddress, startAddress, item);
// startAddress += (ushort)item.Length;
// }
// }
// else
// {
// master.WriteMultipleRegisters(slaveAddress, startAddress, ushortsValue);
// }

// }

// break;
// default:
// break;
// }
// }
// catch (Exception ex)
// {
// MessageLog.GetInstance.ShowEx(ex.ToString());
// if (ex.InnerException is SocketException)
// {
// tcpClient = null;
// Connect();
// }
// return false;
// }
// return true;
//}


#region 批量数据读取
/// <summary>


+ 3
- 1
BPASmartClient.Model/DeviceConfigModelJson.cs View File

@@ -91,6 +91,8 @@ namespace BPASmartClient.Model
public CommunicationPar communicationPar { get { return _mcommunicationPar; } set { _mcommunicationPar = value; OnPropertyChanged(); } }
private CommunicationPar _mcommunicationPar = new CommunicationPar();

public ObservableCollection<Variable> variables { get; set; } = new ObservableCollection<Variable>();

}

/// <summary>
@@ -159,7 +161,7 @@ namespace BPASmartClient.Model
public string Parity { get { return _mParity; } set { _mParity = value; OnPropertyChanged(); } }
private string _mParity;

public ObservableCollection<Variable> variables { get; set; } = new ObservableCollection<Variable>();
//public ObservableCollection<Variable> variables { get; set; } = new ObservableCollection<Variable>();
}

public class Variable : ObservableObject


+ 7
- 7
BPASmartClient.MorkS/Control_Morks.cs View File

@@ -176,13 +176,13 @@ namespace BPASmartClient.MorkS
}
}));

//EventBus.EventBus.GetInstance().Publish(new ReadModel() { DeviceId = DeviceId, Address = "VW372", Length = 1 }, (o) =>
//{
// if (o != null && o.Length > 0 && o[0] is ushort value)
// {
// mORKS.TurntableFeedbackloc = value;
// }
//});
EventBus.EventBus.GetInstance().Publish(new ReadModel() { DeviceId = DeviceId, Address = "VW372", Length = 1 }, (o) =>
{
if (o != null && o.Length > 0 && o[0] is ushort value)
{
mORKS.TurntableFeedbackloc = value;
}
});
}

/// <summary>


+ 4
- 0
BPASmartClient.Peripheral/BPASmartClient.Peripheral.csproj View File

@@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
</ItemGroup>

</Project>

+ 7
- 2
BPASmartClient.Peripheral/BasePeripheral.cs View File

@@ -1,6 +1,8 @@
using System;
using BPASmartClient.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -24,11 +26,14 @@ namespace BPASmartClient.Peripheral
/// 归属设备Id
/// </summary>
public int DeviceId { get; set; }
public CommunicationPar communicationPar { get; set; } = new CommunicationPar();
public ConcurrentDictionary<string, object> status { get; set; } = new ConcurrentDictionary<string, object>();
public ObservableCollection<Variable> variables { get; set; } = new ObservableCollection<Variable>();

/// <summary>
/// 外设状态集合
/// </summary>
protected ConcurrentDictionary<string, object> status = new ConcurrentDictionary<string, object>();
//protected ConcurrentDictionary<string, object> status = new ConcurrentDictionary<string, object>();

/// <summary>
/// 初始化外设状态


+ 19
- 1
BPASmartClient.Peripheral/IPeripheral.cs View File

@@ -1,6 +1,8 @@
using System;
using BPASmartClient.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -35,6 +37,22 @@ namespace BPASmartClient.Peripheral
/// </summary>
/// <returns>状态值</returns>
ConcurrentDictionary<string, object> GetAllStatus();

/// <summary>
/// 设备变量数据
/// </summary>
ObservableCollection<Variable> variables { get; set; }

/// <summary>
/// 通讯参数
/// </summary>
CommunicationPar communicationPar { get; set; }

/// <summary>
/// 设备数据,key:地址,value:数据
/// </summary>
ConcurrentDictionary<string, object> status { get; set; }

/// <summary>
/// 初始化
/// </summary>


+ 0
- 1
BPASmartClient.SCChip/BPASmartClient.SCChip.csproj View File

@@ -5,7 +5,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BPASmartClient.Model\BPASmartClient.Model.csproj" />
<ProjectReference Include="..\BPASmartClient.Peripheral\BPASmartClient.Peripheral.csproj" />
<ProjectReference Include="..\BPASmartClient.SerialPort\BPASmartClient.SerialPort.csproj" />
</ItemGroup>


+ 1
- 1
BPASmartClient/App.xaml.cs View File

@@ -114,7 +114,7 @@ namespace BPASmartClient
private void DataSave()
{
Json<KeepDataBase>.Save();
Sqlite<Alarm>.GetInstance.Save();
//Sqlite<Alarm>.GetInstance.Save();
}

/// <summary>


+ 2
- 2
BPASmartClient/Control/ShopDeviceConfigView.xaml View File

@@ -425,7 +425,7 @@
<TreeView.Resources>

<!-- 店铺信息 -->
<HierarchicalDataTemplate DataType="{x:Type vmm:DeviceConfigModel}" ItemsSource="{Binding deviceModels}">
<HierarchicalDataTemplate DataType="{x:Type vmm:DeviceConfigModelJson}" ItemsSource="{Binding deviceModels}">
<StackPanel>
<TextBlock Style="{StaticResource TextBlockStyle}" Text="{Binding ShopName}">
<TextBlock.ContextMenu>
@@ -924,7 +924,7 @@
Grid.Row="1"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<ItemsControl ItemsSource="{Binding SelectedValue.communicationPar.variables, ElementName=tv, Mode=TwoWay}">
<ItemsControl ItemsSource="{Binding SelectedValue.variables, ElementName=tv, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Height="30">


+ 3
- 3
BPASmartClient/Control/ShopDeviceConfigView.xaml.cs View File

@@ -81,13 +81,13 @@ namespace BPASmartClient.Control
int commIndex = Array.FindIndex(ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ToArray(), p => p.DeviceModelId == communicationModel.DeviceModelId);
if (commIndex >= 0 && commIndex < ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.Count)
{
if (ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).communicationPar.variables.Count <= 0)
if (ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).variables.Count <= 0)
{
for (int i = 0; i < 20; i++)
{
ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).communicationPar.variables.Add(new Variable()
ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).variables.Add(new Variable()
{
Id = ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).communicationPar.variables.Count,
Id = ShopDeviceConfigViewModel.deviceConfig.ElementAt(0).deviceModels.ElementAt(index).communicationDevcies.ElementAt(commIndex).variables.Count,
Address = string.Empty,
ReadLeng = 0
});


+ 2
- 2
BPASmartClient/DeviceInfo.xml View File

@@ -61,8 +61,8 @@
<Parameters>
<IpAddress>127.0.0.1</IpAddress>
<Port>502</Port>
<!--<PLCReadAddress>M,M230.0,24;M,M0.3,3;M,M100.0,16;M,M235.0,1;M,M102.0,7;M,M103.0,6;VW,VW372,1</PLCReadAddress>-->
<PLCReadAddress>M,M230.0,24</PLCReadAddress>
<PLCReadAddress>M,M230.0,24;M,M0.3,3;M,M100.0,16;M,M235.0,1;M,M102.0,7;M,M103.0,6;VW,VW372,1</PLCReadAddress>
<!--<PLCReadAddress>M,M230.0,24</PLCReadAddress>-->
</Parameters>
</Peripheral>
</Peripherals>


Loading…
Cancel
Save