|
|
@@ -14,7 +14,6 @@ namespace BPASmart.Server |
|
|
|
|
|
|
|
public void Init() |
|
|
|
{ |
|
|
|
var tt = sizeof(bool); |
|
|
|
BPASmartClient.Message.MessageLog.GetInstance.ShowDebugLog("通讯模块初始化"); |
|
|
|
RedisHelper.GetInstance.ConnectAsync(); |
|
|
|
MqttInit(); |
|
|
@@ -37,45 +36,80 @@ namespace BPASmart.Server |
|
|
|
{ |
|
|
|
GetReadDataModels(item).ToList()?.ForEach(temp => |
|
|
|
{ |
|
|
|
switch (temp.Key) |
|
|
|
//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 => |
|
|
|
{ |
|
|
|
case EDataType.Bool: |
|
|
|
temp.Value?.ForEach(value => |
|
|
|
{ |
|
|
|
var res = modbusTcpMaster.ReadBool(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); |
|
|
|
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); |
|
|
|
SetValue(res.Content, item.DeviceName, value, 2); |
|
|
|
}); |
|
|
|
break; |
|
|
|
case EDataType.Float: |
|
|
|
temp.Value?.ForEach(value => |
|
|
|
{ |
|
|
|
var res = modbusTcpMaster.ReadFloat(value.StartAddress.ToString(), value.Length); |
|
|
|
SetValue(res.Content, item.DeviceName, 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, item.DeviceName, value, temp.Key); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
Thread.Sleep(100); |
|
|
@@ -128,32 +162,51 @@ namespace BPASmart.Server |
|
|
|
{ |
|
|
|
if (CommunicationDevices.ContainsKey(item.DeviceName)) |
|
|
|
{ |
|
|
|
switch (item.DataType) |
|
|
|
string address = string.Empty; |
|
|
|
if (item.RealAddress != null && item.RealAddress.Length > 0) address = item.DeviceName; |
|
|
|
else |
|
|
|
{ |
|
|
|
var res = Json<CommunicationPar>.Data.CommunicationDevices.FirstOrDefault(p => p.DeviceName == item.DeviceName); |
|
|
|
var res2 = res?.VarTableModels.FirstOrDefault(p => p.VarName?.Length > 0 && p.VarName == item.VarName); |
|
|
|
address = res2?.RealAddress; |
|
|
|
} |
|
|
|
if (item.Value != null && item.Value.Length > 0) |
|
|
|
{ |
|
|
|
case EDataType.Bool: |
|
|
|
CommunicationDevices[item.DeviceName].Write(item.RealAddress, Convert.ToBoolean(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Byte: |
|
|
|
CommunicationDevices[item.DeviceName].Write(item.RealAddress, Convert.ToByte(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Int: |
|
|
|
CommunicationDevices[item.DeviceName].Write(item.RealAddress, Convert.ToInt16(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Word: |
|
|
|
CommunicationDevices[item.DeviceName].Write(item.RealAddress, Convert.ToUInt16(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Dint: |
|
|
|
CommunicationDevices[item.DeviceName].Write(item.RealAddress, Convert.ToInt32(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Dword: |
|
|
|
CommunicationDevices[item.DeviceName].Write(item.RealAddress, Convert.ToUInt32(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Float: |
|
|
|
CommunicationDevices[item.DeviceName].Write(item.RealAddress, Convert.ToSingle(item.Value)); |
|
|
|
break; |
|
|
|
default: |
|
|
|
break; |
|
|
|
switch (item.DataType) |
|
|
|
{ |
|
|
|
case EDataType.Bool: |
|
|
|
//CommunicationDevices[item.DeviceName].Write(address, Convert.ToBoolean(item.Value)); |
|
|
|
CommunicationDevices[item.DeviceName].Write(address, Convert.ToBoolean(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Byte: |
|
|
|
//CommunicationDevices[item.DeviceName].Write(address, Convert.ToByte(item.Value)); |
|
|
|
CommunicationDevices[item.DeviceName].Write(address, Convert.ToByte(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Int: |
|
|
|
//CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt16(item.Value)); |
|
|
|
CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt16(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Word: |
|
|
|
//CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt16(item.Value)); |
|
|
|
CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt16(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Dint: |
|
|
|
//CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt32(item.Value)); |
|
|
|
CommunicationDevices[item.DeviceName].Write(address, Convert.ToInt32(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Dword: |
|
|
|
//CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt32(item.Value)); |
|
|
|
CommunicationDevices[item.DeviceName].Write(address, Convert.ToUInt32(item.Value)); |
|
|
|
break; |
|
|
|
case EDataType.Float: |
|
|
|
//CommunicationDevices[item.DeviceName].Write(address, Convert.ToSingle(item.Value)); |
|
|
|
CommunicationDevices[item.DeviceName].Write(address, Convert.ToSingle(item.Value)); |
|
|
|
break; |
|
|
|
default: |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
@@ -195,6 +248,67 @@ namespace BPASmart.Server |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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); |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels(CommunicationModel communicationModel) |
|
|
|
{ |
|
|
|
Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>(); |
|
|
|