Skip to content

Commit

Permalink
1、SiemensClient 批量数据写
Browse files Browse the repository at this point in the history
2、MQTT操作demo
3、手动更新
  • Loading branch information
zhaopeiym committed Sep 11, 2020
1 parent a997a90 commit dc25c41
Show file tree
Hide file tree
Showing 31 changed files with 2,013 additions and 221 deletions.
53 changes: 48 additions & 5 deletions IoTClient.Tests/PLCTests/SiemensClient_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public SiemensClient_Tests()
{
var ip = IPAddress.Parse("SiemensClientIp".GetConfig());
var port = int.Parse("SiemensClientPort".GetConfig());
client = new SiemensClient(SiemensVersion.S7_200Smart, new IPEndPoint(ip, port));
client = new SiemensClient(SiemensVersion.S7_1500, new IPEndPoint(ip, port));
}

[Fact]
Expand Down Expand Up @@ -109,9 +109,9 @@ public void test()
//TODO 最多只能批量读取 19个?
Dictionary<string, DataTypeEnum> addresses = new Dictionary<string, DataTypeEnum>();

addresses.Add("V1000", DataTypeEnum.Float);
addresses.Add("I0.0", DataTypeEnum.Bool);
addresses.Add("V4109", DataTypeEnum.Byte);
//addresses.Add("V1000", DataTypeEnum.Float);
//addresses.Add("I0.0", DataTypeEnum.Bool);
//addresses.Add("V4109", DataTypeEnum.Byte);
//addresses.Add("V1004", DataTypeEnum.Float);

//addresses.Add("V1000", DataTypeEnum.Float);
Expand All @@ -137,7 +137,50 @@ public void test()
//addresses.Add("V1254", DataTypeEnum.Float);
//addresses.Add("V1258", DataTypeEnum.Float);

var obj = client.Read(addresses);

//addresses.Add("V1012", DataTypeEnum.Float);
//addresses.Add("V1076 ", DataTypeEnum.UInt32);
//addresses.Add("V5056 ", DataTypeEnum.Float);
//addresses.Add("V5232 ", DataTypeEnum.Float);

//addresses.Add("I0.0 ", DataTypeEnum.Bool);
//addresses.Add("I0.1 ", DataTypeEnum.Bool);
//addresses.Add("I0.2 ", DataTypeEnum.Bool);
//addresses.Add("I0.3 ", DataTypeEnum.Bool);
//addresses.Add("I0.4 ", DataTypeEnum.Bool);
//addresses.Add("I0.5 ", DataTypeEnum.Bool);
//addresses.Add("I0.6 ", DataTypeEnum.Bool);
//addresses.Add("I0.7 ", DataTypeEnum.Bool);

//addresses.Add("I1.0 ", DataTypeEnum.Bool);
//addresses.Add("I1.1 ", DataTypeEnum.Bool);
//addresses.Add("I1.2 ", DataTypeEnum.Bool);
//addresses.Add("I1.3 ", DataTypeEnum.Bool);
//addresses.Add("I1.4 ", DataTypeEnum.Bool);
//addresses.Add("I1.5 ", DataTypeEnum.Bool);
//addresses.Add("I1.6 ", DataTypeEnum.Bool);
//addresses.Add("I1.7 ", DataTypeEnum.Bool);


//client.Write("DB4.0", (float)6);
//client.Write("DB4.12", (float)9);
//client.Write("DB1.410.0", false);
//client.Write("DB1.410.0", true);

var result = client.BatchRead(addresses);

Dictionary<string, object> newAddresses = new Dictionary<string, object>();
newAddresses.Add("DB4.24", (float)1);
newAddresses.Add("DB4.0", (float)2);
newAddresses.Add("DB1.434.0", true);
newAddresses.Add("DB1.482.0", true);
newAddresses.Add("DB4.12", (float)3);
newAddresses.Add("DB1.410.0", true);
var result1 = client.BatchWrite(newAddresses);

var r3 = client.Write("DB1.482.0", false);
var result2 = client.Write("DB1.434.0", false);
client.Write("DB1.434.0", true);
}

private void test2(string address, ushort readNumber)
Expand Down
3 changes: 3 additions & 0 deletions IoTClient.Tests/TempTest/temp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class temp
[Fact]
public void test()
{

var oo = BitConverter.ToSingle(DataConvert.StringToByteArray("20 FF C0 00 00").Reverse().ToArray(),0);

var str = "0300 00 1D";
//var b = str.Split(" ").Select(t => Convert.ToByte(t, 16)).ToArray();

Expand Down
47 changes: 43 additions & 4 deletions IoTClient.Tool/Controls/BACnetControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 62 additions & 64 deletions IoTClient.Tool/Controls/BACnetControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ private void Scan()
{
//获取子节点个数
var deviceCount = GetDeviceArrayIndexCount(device) + 1;
//TODO 50 可设置 配置
ScanPointsBatch(device, 50, deviceCount);
//TODO 20 可设置 配置
ScanPointsBatch(device, 20, deviceCount);
}
Log("开始扫描属性");
foreach (var device in devicesList)
{
Log($"开始扫描属性,Address:{device.Address.ToString()} DeviceId:{device.DeviceId}");
ScanSubProperties(device);
}
Log("扫描完成");
Expand Down Expand Up @@ -144,7 +144,7 @@ public void ScanPointsBatch(BacNode device, uint deviceCount, uint count)
}
catch (Exception exp)
{
Log("Err:" + exp.Message);
Log("Err:" + exp.Message);
}
}

Expand All @@ -163,7 +163,7 @@ public uint GetDeviceArrayIndexCount(BacNode device)
}
catch (Exception ex)
{
Log("Err:" + ex.Message);
Log("Err:" + ex.Message);
}
return 0;
}
Expand All @@ -178,7 +178,7 @@ private BacnetValue ReadScalarValue(BacnetAddress adr, BacnetObjectId oid,
}
catch (Exception ex)
{
Log("Err:" + ex.Message);
Log("Err:" + ex.Message);
}
return new BacnetValue();
}
Expand All @@ -189,73 +189,71 @@ private BacnetValue ReadScalarValue(BacnetAddress adr, BacnetObjectId oid,
/// <param name="device"></param>
private void ScanSubProperties(BacNode device)
{
var adr = device.Address;
if (adr == null) return;
if (device.Properties == null) return;
try
{
var adr = device.Address;
if (adr == null) return;
if (device.Properties == null) return;

List<BacnetPropertyReference> rList = new List<BacnetPropertyReference>();
rList.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_DESCRIPTION, uint.MaxValue));
rList.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_REQUIRED, uint.MaxValue));
rList.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_OBJECT_NAME, uint.MaxValue));
rList.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_PRESENT_VALUE, uint.MaxValue));
List<BacnetReadAccessSpecification> properties = new List<BacnetReadAccessSpecification>();
List<BacnetPropertyReference> rList = new List<BacnetPropertyReference>();
rList.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_DESCRIPTION, uint.MaxValue));
rList.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_REQUIRED, uint.MaxValue));
rList.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_OBJECT_NAME, uint.MaxValue));
rList.Add(new BacnetPropertyReference((uint)BacnetPropertyIds.PROP_PRESENT_VALUE, uint.MaxValue));

for (int i = 0; i < device.Properties.Count; i++)
{
var subNode = device.Properties[i];
try
List<BacnetReadAccessResult> lstAccessRst = new List<BacnetReadAccessResult>();
var batchNumber = (int)numericUpDown1.Value;
var batchCount = Math.Ceiling((float)device.Properties.Count / batchNumber);
for (int i = 0; i < batchCount; i++)
{
properties.Add(new BacnetReadAccessSpecification(subNode.ObjectId, rList));
IList<BacnetReadAccessSpecification> properties = device.Properties.Skip(i * batchNumber).Take(batchNumber)
.Select(t => new BacnetReadAccessSpecification(t.ObjectId, rList)).ToList();
//批量读取
lstAccessRst.AddRange(Bacnet_client.ReadPropertyMultipleRequest(adr, properties));
}

//批量读取,9条一组
if ((i != 0 && i % 9 == 0) || i == device.Properties.Count - 1)
if (lstAccessRst?.Any() ?? false)
{
foreach (var aRst in lstAccessRst)
{
IList<BacnetReadAccessResult> lstAccessRst = Bacnet_client.ReadPropertyMultipleRequest(adr, properties);
if (lstAccessRst?.Any() ?? false)
if (aRst.values == null) continue;
var subNode = device.Properties
.Where(t => t.ObjectId.Instance == aRst.objectIdentifier.Instance && t.ObjectId.Type == aRst.objectIdentifier.Type)
.FirstOrDefault();
foreach (var bPValue in aRst.values)
{
foreach (var aRst in lstAccessRst)
if (bPValue.value == null || bPValue.value.Count == 0) continue;
var pid = (BacnetPropertyIds)(bPValue.property.propertyIdentifier);
var bValue = bPValue.value.First();
var strBValue = "" + bValue.Value;
//Log(pid + " , " + strBValue + " , " + bValue.Tag);
switch (pid)
{
if (aRst.values == null) continue;
subNode = device.Properties
.Where(t => t.ObjectId.Instance == aRst.objectIdentifier.Instance && t.ObjectId.Type == aRst.objectIdentifier.Type)
.FirstOrDefault();
foreach (var bPValue in aRst.values)
{
if (bPValue.value == null || bPValue.value.Count == 0) continue;
var pid = (BacnetPropertyIds)(bPValue.property.propertyIdentifier);
var bValue = bPValue.value.First();
var strBValue = "" + bValue.Value;
//Log(pid + " , " + strBValue + " , " + bValue.Tag);
switch (pid)
case BacnetPropertyIds.PROP_DESCRIPTION://描述
{
case BacnetPropertyIds.PROP_DESCRIPTION://描述
{
subNode.PROP_DESCRIPTION = bValue.ToString()?.Trim();
}
break;
case BacnetPropertyIds.PROP_OBJECT_NAME://点名
{
subNode.PROP_OBJECT_NAME = bValue.ToString()?.Trim();
}
break;
case BacnetPropertyIds.PROP_PRESENT_VALUE://值
{
subNode.PROP_PRESENT_VALUE = bValue.Value;
subNode.DataType = bValue.Value.GetType();
}
break;
subNode.PROP_DESCRIPTION = bValue.ToString()?.Trim();
}
}
ShwoText(string.Format("地址:{0}\t 点名:{1}\t 值:{2}\t 类型:{3}\t 描述:{4} ", $"{subNode.ObjectId.Instance}_{(int)subNode.ObjectId.Type}", subNode.PROP_OBJECT_NAME, subNode.PROP_PRESENT_VALUE, subNode.PROP_PRESENT_VALUE.GetType().ToString().Split('.')[1], subNode.PROP_DESCRIPTION));
break;
case BacnetPropertyIds.PROP_OBJECT_NAME://点名
{
subNode.PROP_OBJECT_NAME = bValue.ToString()?.Trim();
}
break;
case BacnetPropertyIds.PROP_PRESENT_VALUE://值
{
subNode.PROP_PRESENT_VALUE = bValue.Value;
subNode.DataType = bValue.Value.GetType();
}
break;
}
}
properties.Clear();
ShwoText(string.Format("地址:{0}\t 点名:{1}\t 值:{2}\t 类型:{3}\t 描述:{4} ", $"{subNode.ObjectId.Instance}_{(int)subNode.ObjectId.Type}", subNode.PROP_OBJECT_NAME, subNode.PROP_PRESENT_VALUE, subNode.PROP_PRESENT_VALUE.GetType().ToString().Split('.')[1], subNode.PROP_DESCRIPTION));
}
}
catch (Exception exp)
{
Log("Error: " + exp.Message);
}
}
catch (Exception ex)
{
Log("【Err】:" + ex.Message);
}
}

Expand All @@ -277,8 +275,8 @@ private void ShwoText(string str)

private async void Read_ClickAsync(object sender, EventArgs e)
{
var address = txt_address.Text?.Trim();
var addressPart = address.Split('_');
var address = txt_address.Text?.Trim();
var addressPart = address.Split('_');
BacProperty rpop = null;
BacNode bacnet = null;

Expand Down Expand Up @@ -320,14 +318,14 @@ private async void Read_ClickAsync(object sender, EventArgs e)
}
catch
{
Log("Err:读取失败.");
Log("Err:读取失败.");
}
}
else
{
retry++;
if (retry < 4) goto tag_retry;
Log($"Err:读取失败[{retry - 1}]");
Log($"Err:读取失败[{retry - 1}]");
}
}

Expand Down
Loading

0 comments on commit dc25c41

Please sign in to comment.