@@ -0,0 +1,110 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Collections.ObjectModel; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace BPASmart.Model | |||
{ | |||
public static class ExpandMethod | |||
{ | |||
/// <summary> | |||
/// 获取数据类型的大小 | |||
/// </summary> | |||
/// <param name="eDataType"></param> | |||
/// <returns></returns> | |||
public static ushort GetEDataSize(this EDataType eDataType) | |||
{ | |||
switch (eDataType) | |||
{ | |||
case EDataType.Bool: | |||
case EDataType.Byte: | |||
case EDataType.Int: | |||
case EDataType.Word: | |||
return 1; | |||
case EDataType.Dint: | |||
case EDataType.Dword: | |||
case EDataType.Float: | |||
return 2; | |||
case EDataType.Double: | |||
break; | |||
case EDataType.String: | |||
break; | |||
default: | |||
break; | |||
} | |||
return 1; | |||
} | |||
/// <summary> | |||
/// 获取Modbus Tcp 连续变量数据的组对象 | |||
/// </summary> | |||
/// <param name="variableInfos"></param> | |||
/// <param name="by"></param> | |||
/// <returns></returns> | |||
public static List<ReadDataModel> GetDataGroup(this IGrouping<string, VariableInfo> variableInfos, int by = 1) | |||
{ | |||
List<ReadDataModel> ReturnValue = new List<ReadDataModel>(); | |||
var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); | |||
List<int> RealAddresss = new List<int>(); | |||
variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); | |||
int count = 0; | |||
if (res != null) | |||
{ | |||
int address = RealAddresss.Min(); | |||
int startAddress = address; | |||
for (int i = 0; i < res.Count; i++) | |||
{ | |||
if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) | |||
{ | |||
if (TempAddress == address) | |||
{ | |||
count++; | |||
address += by; | |||
} | |||
else | |||
{ | |||
ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
count = 1; | |||
address = TempAddress + by; | |||
startAddress = TempAddress; | |||
} | |||
} | |||
} | |||
ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
} | |||
return ReturnValue; | |||
} | |||
public static Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels(this ObservableCollection<VariableInfo> varialeInfos) | |||
{ | |||
Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>(); | |||
varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => | |||
{ | |||
if (tempVar.Key != null && tempVar.Key.Length > 0) | |||
{ | |||
EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); | |||
switch (dataType) | |||
{ | |||
case EDataType.Bool: | |||
case EDataType.Byte: | |||
case EDataType.Int: | |||
case EDataType.Word: | |||
if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup()); | |||
break; | |||
case EDataType.Dint: | |||
case EDataType.Dword: | |||
case EDataType.Float: | |||
if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup(2)); | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
}); | |||
return readDataModels; | |||
} | |||
} | |||
} |
@@ -34,83 +34,83 @@ namespace BPASmart.Server | |||
CommunicationDevices.TryAdd(item.DeviceName, modbusTcpMaster); | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
{ | |||
GetReadDataModels(item).ToList()?.ForEach(temp => | |||
{ | |||
//switch (temp.Key) | |||
//{ | |||
// case EDataType.Bool: | |||
// temp.Value?.ForEach(value => | |||
// { | |||
// //var res = modbusTcpMaster.ReadBool(value.StartAddress.ToString(), value.Length); | |||
// var res = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length); | |||
// SetValue(res.Content, item.DeviceName, value, 1); | |||
// }); | |||
// break; | |||
// case EDataType.Byte: | |||
// break; | |||
// case EDataType.Int: | |||
// break; | |||
// case EDataType.Word: | |||
// temp.Value?.ForEach(value => | |||
// { | |||
// //var res = modbusTcpMaster.ReadUshort(value.StartAddress.ToString(), value.Length); | |||
// var res = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length); | |||
// SetValue(res.Content, item.DeviceName, value, 1); | |||
// }); | |||
// break; | |||
// case EDataType.Dint: | |||
// break; | |||
// case EDataType.Dword: | |||
// temp.Value?.ForEach(value => | |||
// { | |||
// //var res = modbusTcpMaster.ReadUint(value.StartAddress.ToString(), value.Length); | |||
// var res = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length); | |||
// SetValue(res.Content, item.DeviceName, value, 2); | |||
// }); | |||
// break; | |||
// case EDataType.Float: | |||
// temp.Value?.ForEach(value => | |||
// { | |||
// //var res = modbusTcpMaster.ReadFloat(value.StartAddress.ToString(), value.Length); | |||
// var res = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length); | |||
// SetValue(res.Content, item.DeviceName, value, 2); | |||
// }); | |||
// break; | |||
// default: | |||
// break; | |||
//} | |||
item.VarTableModels.GetReadDataModels().ToList()?.ForEach(temp => | |||
{ | |||
//switch (temp.Key) | |||
//{ | |||
// case EDataType.Bool: | |||
// temp.Value?.ForEach(value => | |||
// { | |||
// //var res = modbusTcpMaster.ReadBool(value.StartAddress.ToString(), value.Length); | |||
// var res = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length); | |||
// SetValue(res.Content, item.DeviceName, value, 1); | |||
// }); | |||
// break; | |||
// case EDataType.Byte: | |||
// break; | |||
// case EDataType.Int: | |||
// break; | |||
// case EDataType.Word: | |||
// temp.Value?.ForEach(value => | |||
// { | |||
// //var res = modbusTcpMaster.ReadUshort(value.StartAddress.ToString(), value.Length); | |||
// var res = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length); | |||
// SetValue(res.Content, item.DeviceName, value, 1); | |||
// }); | |||
// break; | |||
// case EDataType.Dint: | |||
// break; | |||
// case EDataType.Dword: | |||
// temp.Value?.ForEach(value => | |||
// { | |||
// //var res = modbusTcpMaster.ReadUint(value.StartAddress.ToString(), value.Length); | |||
// var res = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length); | |||
// SetValue(res.Content, item.DeviceName, value, 2); | |||
// }); | |||
// break; | |||
// case EDataType.Float: | |||
// temp.Value?.ForEach(value => | |||
// { | |||
// //var res = modbusTcpMaster.ReadFloat(value.StartAddress.ToString(), value.Length); | |||
// var res = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length); | |||
// SetValue(res.Content, item.DeviceName, value, 2); | |||
// }); | |||
// break; | |||
// default: | |||
// break; | |||
//} | |||
Array ResultArray = null; | |||
temp.Value?.ForEach(value => | |||
{ | |||
switch (temp.Key) | |||
{ | |||
case EDataType.Bool: | |||
ResultArray = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Byte: | |||
break; | |||
case EDataType.Int: | |||
ResultArray = modbusTcpMaster.Read<short[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Word: | |||
ResultArray = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Dint: | |||
ResultArray = modbusTcpMaster.Read<int[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Dword: | |||
ResultArray = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Float: | |||
ResultArray = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
default: | |||
break; | |||
} | |||
SetValue(ResultArray, item.DeviceName, value, temp.Key); | |||
}); | |||
}); | |||
Array ResultArray = null; | |||
temp.Value?.ForEach(value => | |||
{ | |||
switch (temp.Key) | |||
{ | |||
case EDataType.Bool: | |||
ResultArray = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Byte: | |||
break; | |||
case EDataType.Int: | |||
ResultArray = modbusTcpMaster.Read<short[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Word: | |||
ResultArray = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Dint: | |||
ResultArray = modbusTcpMaster.Read<int[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Dword: | |||
ResultArray = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Float: | |||
ResultArray = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
default: | |||
break; | |||
} | |||
SetValue(ResultArray, item.DeviceName, value, temp.Key); | |||
}); | |||
}); | |||
Thread.Sleep(100); | |||
}), $"{item.DeviceName} 设备数据采集"); | |||
@@ -213,69 +213,69 @@ namespace BPASmart.Server | |||
} | |||
} | |||
private void SetValue<TArray>(TArray[] arrays, string DeviceName, ReadDataModel readDataModel, ushort by) | |||
{ | |||
if (arrays != null) | |||
{ | |||
int index = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置 | |||
if (index >= 0 && index < Json<CommunicationPar>.Data.CommunicationDevices.Count) | |||
{ | |||
var tempArray = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray(); | |||
for (int i = 0; i < arrays.Length; i++) | |||
{ | |||
int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); | |||
if (varIndex >= 0 && varIndex < tempArray.Length) | |||
{ | |||
Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays[i].ToString(); | |||
} | |||
} | |||
var Devicename = Json<CommunicationPar>.Data.CommunicationDevices[index].DeviceName; | |||
List<ReeisDataModel> reeisDataModels = new List<ReeisDataModel>(); | |||
Json<CommunicationPar>.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar => | |||
{ | |||
if (tempVar.VarName.Length > 0) | |||
{ | |||
reeisDataModels.Add(new ReeisDataModel() | |||
{ | |||
VarName = tempVar.VarName, | |||
VarVaule = tempVar.CurrentValue, | |||
DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType) | |||
}); | |||
} | |||
}); | |||
RedisHelper.GetInstance.SetValue($"{Devicename}", reeisDataModels); | |||
} | |||
} | |||
} | |||
//private void SetValue<TArray>(TArray[] arrays, string DeviceName, ReadDataModel readDataModel, ushort by) | |||
//{ | |||
// if (arrays != null) | |||
// { | |||
// int index = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置 | |||
// if (index >= 0 && index < Json<CommunicationPar>.Data.CommunicationDevices.Count) | |||
// { | |||
// var tempArray = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray(); | |||
// for (int i = 0; i < arrays.Length; i++) | |||
// { | |||
// int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); | |||
// if (varIndex >= 0 && varIndex < tempArray.Length) | |||
// { | |||
// Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays[i].ToString(); | |||
// } | |||
// } | |||
// var Devicename = Json<CommunicationPar>.Data.CommunicationDevices[index].DeviceName; | |||
// List<ReeisDataModel> reeisDataModels = new List<ReeisDataModel>(); | |||
// Json<CommunicationPar>.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar => | |||
// { | |||
// if (tempVar.VarName.Length > 0) | |||
// { | |||
// reeisDataModels.Add(new ReeisDataModel() | |||
// { | |||
// VarName = tempVar.VarName, | |||
// VarVaule = tempVar.CurrentValue, | |||
// DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType) | |||
// }); | |||
// } | |||
// }); | |||
// RedisHelper.GetInstance.SetValue($"{Devicename}", reeisDataModels); | |||
// } | |||
// } | |||
//} | |||
private ushort GetBySize(EDataType eDataType) | |||
{ | |||
switch (eDataType) | |||
{ | |||
case EDataType.Bool: | |||
case EDataType.Byte: | |||
case EDataType.Int: | |||
case EDataType.Word: | |||
return 1; | |||
case EDataType.Dint: | |||
case EDataType.Dword: | |||
case EDataType.Float: | |||
return 2; | |||
case EDataType.Double: | |||
break; | |||
case EDataType.String: | |||
break; | |||
default: | |||
break; | |||
} | |||
return 1; | |||
} | |||
//private ushort GetBySize(EDataType eDataType) | |||
//{ | |||
// switch (eDataType) | |||
// { | |||
// case EDataType.Bool: | |||
// case EDataType.Byte: | |||
// case EDataType.Int: | |||
// case EDataType.Word: | |||
// return 1; | |||
// case EDataType.Dint: | |||
// case EDataType.Dword: | |||
// case EDataType.Float: | |||
// return 2; | |||
// case EDataType.Double: | |||
// break; | |||
// case EDataType.String: | |||
// break; | |||
// default: | |||
// break; | |||
// } | |||
// return 1; | |||
//} | |||
private void SetValue(Array arrays, string DeviceName, ReadDataModel readDataModel, EDataType eDataType) | |||
{ | |||
if (arrays != null) | |||
{ | |||
ushort by = GetBySize(eDataType); | |||
ushort by = eDataType.GetEDataSize(); | |||
int index = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置 | |||
if (index >= 0 && index < Json<CommunicationPar>.Data.CommunicationDevices.Count) | |||
{ | |||
@@ -309,70 +309,70 @@ namespace BPASmart.Server | |||
private Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels(CommunicationModel communicationModel) | |||
{ | |||
Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>(); | |||
communicationModel.VarTableModels.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => | |||
{ | |||
if (tempVar.Key != null && tempVar.Key.Length > 0) | |||
{ | |||
//int address = tempVar.Min(p => p.RealAddress); | |||
EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); | |||
switch (dataType) | |||
{ | |||
case EDataType.Bool: | |||
case EDataType.Byte: | |||
case EDataType.Int: | |||
case EDataType.Word: | |||
if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar)); | |||
break; | |||
case EDataType.Dint: | |||
case EDataType.Dword: | |||
case EDataType.Float: | |||
if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2)); | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
}); | |||
return readDataModels; | |||
} | |||
//private Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels(CommunicationModel communicationModel) | |||
//{ | |||
// Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>(); | |||
// communicationModel.VarTableModels.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => | |||
// { | |||
// if (tempVar.Key != null && tempVar.Key.Length > 0) | |||
// { | |||
// //int address = tempVar.Min(p => p.RealAddress); | |||
// EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); | |||
// switch (dataType) | |||
// { | |||
// case EDataType.Bool: | |||
// case EDataType.Byte: | |||
// case EDataType.Int: | |||
// case EDataType.Word: | |||
// if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar)); | |||
// break; | |||
// case EDataType.Dint: | |||
// case EDataType.Dword: | |||
// case EDataType.Float: | |||
// if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2)); | |||
// break; | |||
// default: | |||
// break; | |||
// } | |||
// } | |||
// }); | |||
// return readDataModels; | |||
//} | |||
private List<ReadDataModel> GetDataGroup(IGrouping<string, VariableInfo> variableInfos, int by = 1) | |||
{ | |||
List<ReadDataModel> ReturnValue = new List<ReadDataModel>(); | |||
var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); | |||
List<int> RealAddresss = new List<int>(); | |||
variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); | |||
int count = 0; | |||
if (res != null) | |||
{ | |||
//int address = variableInfos.Min(p => p.RealAddress); | |||
int address = RealAddresss.Min(); | |||
int startAddress = address; | |||
for (int i = 0; i < res.Count; i++) | |||
{ | |||
if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) | |||
{ | |||
if (TempAddress == address) | |||
{ | |||
count++; | |||
address += by; | |||
} | |||
else | |||
{ | |||
ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
count = 1; | |||
address = TempAddress + by; | |||
startAddress = TempAddress; | |||
} | |||
} | |||
//private List<ReadDataModel> GetDataGroup(IGrouping<string, VariableInfo> variableInfos, int by = 1) | |||
//{ | |||
// List<ReadDataModel> ReturnValue = new List<ReadDataModel>(); | |||
// var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); | |||
// List<int> RealAddresss = new List<int>(); | |||
// variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); | |||
// int count = 0; | |||
// if (res != null) | |||
// { | |||
// //int address = variableInfos.Min(p => p.RealAddress); | |||
// int address = RealAddresss.Min(); | |||
// int startAddress = address; | |||
// for (int i = 0; i < res.Count; i++) | |||
// { | |||
// if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) | |||
// { | |||
// if (TempAddress == address) | |||
// { | |||
// count++; | |||
// address += by; | |||
// } | |||
// else | |||
// { | |||
// ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
// count = 1; | |||
// address = TempAddress + by; | |||
// startAddress = TempAddress; | |||
// } | |||
// } | |||
} | |||
ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
} | |||
return ReturnValue; | |||
} | |||
// } | |||
// ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
// } | |||
// return ReturnValue; | |||
//} | |||
} | |||
} |
@@ -27,32 +27,33 @@ namespace BPASmart.VariableManager.ViewModels | |||
{ | |||
DataInit(o); | |||
VarNameChanged(); | |||
DelegationNotifi.GetInstance.VariableSave = new Action(() => { Json<CommunicationPar>.Save(); }); | |||
StartMotionCommand = new RelayCommand(() => | |||
{ | |||
switch (ButtonContext) | |||
{ | |||
case "开始监控": | |||
TabName = "当前值"; | |||
CurrentVisibility = Visibility.Visible; | |||
RemoveButVisiblity = Visibility.Collapsed; | |||
ButtonContext = "停止监控"; | |||
IsEnable = false; | |||
Motion(); | |||
break; | |||
case "停止监控": | |||
TabName = "操作"; | |||
CurrentVisibility = Visibility.Collapsed; | |||
RemoveButVisiblity = Visibility.Visible; | |||
ButtonContext = "开始监控"; | |||
IsEnable = true; | |||
ThreadManage.GetInstance().StopTask($"{DeviceType} 初始化连接"); | |||
ThreadManage.GetInstance().StopTask($"{DeviceType} 设备数据采集"); | |||
break; | |||
default: | |||
break; | |||
} | |||
}); | |||
{ | |||
switch (ButtonContext) | |||
{ | |||
case "开始监控": | |||
TabName = "当前值"; | |||
CurrentVisibility = Visibility.Visible; | |||
RemoveButVisiblity = Visibility.Collapsed; | |||
ButtonContext = "停止监控"; | |||
IsEnable = false; | |||
Motion(); | |||
break; | |||
case "停止监控": | |||
TabName = "操作"; | |||
CurrentVisibility = Visibility.Collapsed; | |||
RemoveButVisiblity = Visibility.Visible; | |||
ButtonContext = "开始监控"; | |||
IsEnable = true; | |||
ThreadManage.GetInstance().StopTask($"{DeviceType} 初始化连接"); | |||
ThreadManage.GetInstance().StopTask($"{DeviceType} 设备数据采集"); | |||
break; | |||
default: | |||
break; | |||
} | |||
}); | |||
RemoveCommand = new RelayCommand<object>((o) => | |||
{ | |||
@@ -157,52 +158,42 @@ namespace BPASmart.VariableManager.ViewModels | |||
{ | |||
ThreadManage.GetInstance().StartLong(new Action(() => | |||
{ | |||
GetReadDataModels().ToList()?.ForEach(temp => | |||
varialeInfos.GetReadDataModels().ToList()?.ForEach(temp => | |||
{ | |||
switch (temp.Key) | |||
Array ResultArray = null; | |||
temp.Value?.ForEach(value => | |||
{ | |||
case EDataType.Bool: | |||
temp.Value?.ForEach(value => | |||
{ | |||
var res = modbusTcpMaster.ReadCoils(value.StartAddress, value.Length); | |||
SetValue(res, value, 1); | |||
}); | |||
break; | |||
case EDataType.Byte: | |||
break; | |||
case EDataType.Int: | |||
break; | |||
case EDataType.Word: | |||
temp.Value?.ForEach(value => | |||
{ | |||
var res = modbusTcpMaster.ReadHoldingRegisters(value.StartAddress, value.Length); | |||
SetValue(res, value, 1); | |||
}); | |||
break; | |||
case EDataType.Dint: | |||
break; | |||
case EDataType.Dword: | |||
temp.Value?.ForEach(value => | |||
{ | |||
var res = modbusTcpMaster.ReadHoldingRegisters(value.StartAddress, value.Length); | |||
SetValue(res, value, 2); | |||
}); | |||
break; | |||
case EDataType.Float: | |||
temp.Value?.ForEach(value => | |||
{ | |||
var res = modbusTcpMaster.ReadHoldingRegisters(value.StartAddress, value.Length); | |||
SetValue(res, value, 2); | |||
}); | |||
break; | |||
default: | |||
break; | |||
} | |||
switch (temp.Key) | |||
{ | |||
case EDataType.Bool: | |||
ResultArray = modbusTcpMaster.Read<bool[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Byte: | |||
break; | |||
case EDataType.Int: | |||
ResultArray = modbusTcpMaster.Read<short[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Word: | |||
ResultArray = modbusTcpMaster.Read<ushort[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Dint: | |||
ResultArray = modbusTcpMaster.Read<int[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Dword: | |||
ResultArray = modbusTcpMaster.Read<uint[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
case EDataType.Float: | |||
ResultArray = modbusTcpMaster.Read<float[]>(value.StartAddress.ToString(), value.Length)?.Content; | |||
break; | |||
default: | |||
break; | |||
} | |||
SetValue(ResultArray, value, temp.Key); | |||
}); | |||
}); | |||
Thread.Sleep(100); | |||
}), $"{DeviceType} 设备数据采集"); | |||
//var DeviceModel = item; | |||
}); | |||
modbusTcpMaster.IsReconnect = true; | |||
modbusTcpMaster.ModbusTcpConnect(_modbusTcp.IP, _modbusTcp.PortNum); | |||
@@ -284,109 +275,131 @@ namespace BPASmart.VariableManager.ViewModels | |||
public RelayCommand<object> RemoveCommand { get; set; } | |||
#endregion | |||
private void SetValue<TArray>(TArray[] arrays, ReadDataModel readDataModel, ushort by) | |||
{ | |||
for (int i = 0; i < arrays.Length; i++) | |||
{ | |||
int varIndex = Array.FindIndex(varialeInfos.ToArray(), p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); | |||
if (varIndex >= 0 && varIndex < varialeInfos.Count) | |||
{ | |||
varialeInfos.ElementAt(varIndex).CurrentValue = arrays[i].ToString(); | |||
} | |||
} | |||
} | |||
//private void SetValue<TArray>(TArray[] arrays, ReadDataModel readDataModel, ushort by) | |||
//{ | |||
// for (int i = 0; i < arrays.Length; i++) | |||
// { | |||
// int varIndex = Array.FindIndex(varialeInfos.ToArray(), p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); | |||
// if (varIndex >= 0 && varIndex < varialeInfos.Count) | |||
// { | |||
// varialeInfos.ElementAt(varIndex).CurrentValue = arrays[i].ToString(); | |||
// } | |||
// } | |||
private Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels() | |||
//} | |||
private void SetValue(Array arrays, ReadDataModel readDataModel, EDataType eDataType) | |||
{ | |||
Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>(); | |||
varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => | |||
if (arrays != null) | |||
{ | |||
if (tempVar.Key != null && tempVar.Key.Length > 0) | |||
ushort by = eDataType.GetEDataSize(); | |||
for (int i = 0; i < arrays.Length; i++) | |||
{ | |||
//int address = tempVar.Min(p => p.RealAddress); | |||
EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); | |||
switch (dataType) | |||
int varIndex = Array.FindIndex(varialeInfos.ToArray(), p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); | |||
if (varIndex >= 0 && varIndex < varialeInfos.Count) | |||
{ | |||
case EDataType.Bool: | |||
case EDataType.Byte: | |||
case EDataType.Int: | |||
case EDataType.Word: | |||
if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar)); | |||
break; | |||
case EDataType.Dint: | |||
case EDataType.Dword: | |||
case EDataType.Float: | |||
if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2)); | |||
break; | |||
default: | |||
break; | |||
varialeInfos.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString(); | |||
} | |||
} | |||
}); | |||
return readDataModels; | |||
} | |||
private List<ReadDataModel> GetDataGroup(IGrouping<string, VariableInfo> variableInfos, int by = 1) | |||
{ | |||
//List<ReadDataModel> ReturnValue = new List<ReadDataModel>(); | |||
//var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); | |||
//int count = 0; | |||
//if (res != null) | |||
//{ | |||
// int address = variableInfos.Min(p => p.RealAddress); | |||
// int startAddress = address; | |||
// for (int i = 0; i < res.Count; i++) | |||
// { | |||
// if (res.ElementAt(i).RealAddress == address) | |||
// { | |||
// count++; | |||
// address += by; | |||
// } | |||
// else | |||
// { | |||
// ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
// count = 1; | |||
// address = res.ElementAt(i).RealAddress + by; | |||
// startAddress = res.ElementAt(i).RealAddress; | |||
// } | |||
// } | |||
// ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
//} | |||
//return ReturnValue; | |||
List<ReadDataModel> ReturnValue = new List<ReadDataModel>(); | |||
var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); | |||
List<int> RealAddresss = new List<int>(); | |||
variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); | |||
int count = 0; | |||
if (res != null) | |||
{ | |||
int address = RealAddresss.Min(); | |||
int startAddress = address; | |||
for (int i = 0; i < res.Count; i++) | |||
{ | |||
if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) | |||
{ | |||
if (TempAddress == address) | |||
{ | |||
count++; | |||
address += by; | |||
} | |||
else | |||
{ | |||
ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
count = 1; | |||
address = TempAddress + by; | |||
startAddress = TempAddress; | |||
} | |||
} | |||
} | |||
ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
//int index = Array.FindIndex(Json<CommunicationPar>.Data.CommunicationDevices.ToArray(), p => p.DeviceName == DeviceName);//获取设备所在集合位置 | |||
//if (index >= 0 && index < Json<CommunicationPar>.Data.CommunicationDevices.Count) | |||
//{ | |||
// var tempArray = Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ToArray(); | |||
// for (int i = 0; i < arrays.Length; i++) | |||
// { | |||
// int varIndex = Array.FindIndex(tempArray, p => p.RealAddress == (readDataModel.StartAddress + (i * by)).ToString()); | |||
// if (varIndex >= 0 && varIndex < tempArray.Length) | |||
// { | |||
// Json<CommunicationPar>.Data.CommunicationDevices.ElementAt(index).VarTableModels.ElementAt(varIndex).CurrentValue = arrays.GetValue(i)?.ToString(); | |||
// } | |||
// } | |||
// var Devicename = Json<CommunicationPar>.Data.CommunicationDevices[index].DeviceName; | |||
// List<ReeisDataModel> reeisDataModels = new List<ReeisDataModel>(); | |||
// Json<CommunicationPar>.Data.CommunicationDevices[index].VarTableModels.ToList().ForEach(tempVar => | |||
// { | |||
// if (tempVar.VarName.Length > 0) | |||
// { | |||
// reeisDataModels.Add(new ReeisDataModel() | |||
// { | |||
// VarName = tempVar.VarName, | |||
// VarVaule = tempVar.CurrentValue, | |||
// DataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.DataType) | |||
// }); | |||
// } | |||
// }); | |||
// RedisHelper.GetInstance.SetValue($"{Devicename}", reeisDataModels); | |||
//} | |||
} | |||
return ReturnValue; | |||
} | |||
//private Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels() | |||
//{ | |||
// Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>(); | |||
// varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar => | |||
// { | |||
// if (tempVar.Key != null && tempVar.Key.Length > 0) | |||
// { | |||
// EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key); | |||
// switch (dataType) | |||
// { | |||
// case EDataType.Bool: | |||
// case EDataType.Byte: | |||
// case EDataType.Int: | |||
// case EDataType.Word: | |||
// if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar)); | |||
// break; | |||
// case EDataType.Dint: | |||
// case EDataType.Dword: | |||
// case EDataType.Float: | |||
// if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, GetDataGroup(tempVar, 2)); | |||
// break; | |||
// default: | |||
// break; | |||
// } | |||
// } | |||
// }); | |||
// return readDataModels; | |||
//} | |||
//private List<ReadDataModel> GetDataGroup(IGrouping<string, VariableInfo> variableInfos, int by = 1) | |||
//{ | |||
// List<ReadDataModel> ReturnValue = new List<ReadDataModel>(); | |||
// var res = variableInfos?.OrderBy(p => p.RealAddress).ToList(); | |||
// List<int> RealAddresss = new List<int>(); | |||
// variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); }); | |||
// int count = 0; | |||
// if (res != null) | |||
// { | |||
// int address = RealAddresss.Min(); | |||
// int startAddress = address; | |||
// for (int i = 0; i < res.Count; i++) | |||
// { | |||
// if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress)) | |||
// { | |||
// if (TempAddress == address) | |||
// { | |||
// count++; | |||
// address += by; | |||
// } | |||
// else | |||
// { | |||
// ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
// count = 1; | |||
// address = TempAddress + by; | |||
// startAddress = TempAddress; | |||
// } | |||
// } | |||
// } | |||
// ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count }); | |||
// } | |||
// return ReturnValue; | |||
//} | |||
/// <summary> | |||
@@ -248,7 +248,7 @@ | |||
<!--#endregion--> | |||
<Grid Grid.Row="2"> | |||
<ScrollViewer IsEnabled="{Binding IsEnable}" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> | |||
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"> | |||
<ItemsControl ItemsSource="{Binding varialeInfos}"> | |||
<ItemsControl.ItemTemplate> | |||
<DataTemplate> | |||
@@ -274,6 +274,7 @@ | |||
<Grid Grid.Column="1" KeyDown="TextBox_KeyDown"> | |||
<TextBox | |||
IsEnabled="{Binding DataContext.IsEnable, RelativeSource={RelativeSource AncestorType=ItemsControl, Mode=FindAncestor}}" | |||
Width="{Binding DataContext.NameWidth, RelativeSource={RelativeSource AncestorType=ItemsControl, Mode=FindAncestor}}" | |||
Foreground="{Binding IsRedundant, Converter={StaticResource tabConvert}}" | |||
Style="{StaticResource InputTextboxStyle}" | |||
@@ -287,6 +288,7 @@ | |||
<TextBox | |||
Grid.Column="2" | |||
IsEnabled="{Binding DataContext.IsEnable, RelativeSource={RelativeSource AncestorType=ItemsControl, Mode=FindAncestor}}" | |||
KeyDown="TextBox_KeyDown" | |||
Width="{Binding DataContext.AddressWidth, RelativeSource={RelativeSource AncestorType=ItemsControl, Mode=FindAncestor}}" | |||
Foreground="{Binding IsRedundant, Converter={StaticResource tabConvert}}" | |||