|
@@ -1,5 +1,7 @@
|
|
|
using System.Collections;
|
|
|
+using System.Net.NetworkInformation;
|
|
|
using System.Reflection;
|
|
|
+using System.Runtime.CompilerServices;
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
namespace WCS.Core
|
|
@@ -14,7 +16,8 @@ namespace WCS.Core
|
|
|
|
|
|
public ushort Start { get; private set; }
|
|
|
public ushort Length { get; private set; }
|
|
|
- private bool failed = false;
|
|
|
+ public World World { get; private set; }
|
|
|
+ public bool Failed { get; private set; }
|
|
|
private byte[] Data = new byte[0];
|
|
|
private int id = 0;
|
|
|
|
|
@@ -23,48 +26,51 @@ namespace WCS.Core
|
|
|
return id.ToString();
|
|
|
}
|
|
|
|
|
|
- public DataBlock(DBInfo ent) : base(ent)
|
|
|
+ public DataBlock(DBInfo ent,World world) : base(ent)
|
|
|
{
|
|
|
+ this.World= world;
|
|
|
DBList.Add(this);
|
|
|
id = DBList.Count;
|
|
|
}
|
|
|
|
|
|
- private DateTime faildTime = DateTime.MinValue;
|
|
|
+ DateTime faildTime = DateTime.MinValue;
|
|
|
|
|
|
public void RefreshData()
|
|
|
{
|
|
|
var isPing = false;
|
|
|
try
|
|
|
{
|
|
|
- if (failed && !Entity.PLCInfo.Ex().Ping)
|
|
|
+ var plc = Entity.PLCInfo.Ex(World);
|
|
|
+ if (Failed)
|
|
|
{
|
|
|
- isPing = true;
|
|
|
- throw new Exception($"网络异常IP为{Entity.PLCInfo.IP}的设备PLC无法访问");
|
|
|
- }
|
|
|
-
|
|
|
+ if (!plc.Ping)
|
|
|
+ {
|
|
|
+ isPing = true;
|
|
|
+ throw new Exception($"网络异常IP为{Entity.PLCInfo.IP}的设备PLC无法访问");
|
|
|
+ }
|
|
|
+ }
|
|
|
Start = (ushort)ProxyList.Min(v => v.Info.Position);
|
|
|
var last = ProxyList.OrderBy(v => v.Info.Position).Last();
|
|
|
Length = (ushort)(last.Info.Position + last.BytesCount);
|
|
|
-
|
|
|
var data = new byte[0];
|
|
|
- lock (Entity.PLCInfo.Ex())
|
|
|
+ lock (plc)
|
|
|
{
|
|
|
- data = Entity.PLCInfo.Ex().Accessor.ReadBytes(Entity.No, Start, (ushort)(Length - Start));
|
|
|
+ data = plc.Accessor.ReadBytes(Entity.No, Start, (ushort)(Length - Start));
|
|
|
}
|
|
|
if (!Data.SequenceEqual(data))
|
|
|
{
|
|
|
Data = data;
|
|
|
DbChanged?.Invoke(Data);
|
|
|
}
|
|
|
- failed = false;
|
|
|
+ Failed = false;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- failed = true;
|
|
|
+ Failed = true;
|
|
|
if (isPing) throw new Exception($"{ex.Message}");
|
|
|
throw new Exception($"{ex.Message}:\r{ex.StackTrace}");
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
public PlcItem<T> Regist<T>(ProtocolProxyBase proxy, string objid, string name, int start, byte arrLen = 1, byte strLen = 0)
|
|
|
{
|
|
@@ -104,9 +110,10 @@ namespace WCS.Core
|
|
|
|
|
|
private object Read(Type type, ref int bitStart, int strLength, int arrLength)
|
|
|
{
|
|
|
- if (failed)
|
|
|
+ if (Failed)
|
|
|
+ {
|
|
|
throw new Exception(this.Entity.No + "连接失败");
|
|
|
-
|
|
|
+ }
|
|
|
if (type.IsArray)
|
|
|
{
|
|
|
var t = type.GetElementType();
|
|
@@ -114,7 +121,7 @@ namespace WCS.Core
|
|
|
{
|
|
|
throw new Exception("只支持一维数组");
|
|
|
}
|
|
|
- var arr = Array.CreateInstance(t, arrLength);
|
|
|
+ var arr = Array.CreateInstance(t, arrLength);
|
|
|
|
|
|
for (int i = 0; i < arr.Length; i++)
|
|
|
{
|
|
@@ -444,11 +451,11 @@ namespace WCS.Core
|
|
|
|
|
|
private void WriteBytes(ref int bitStart, byte[] data)
|
|
|
{
|
|
|
- lock (Entity.PLCInfo.Ex())
|
|
|
+ lock (Entity.PLCInfo.Ex(World))
|
|
|
{
|
|
|
var start = GetByteStart(bitStart);
|
|
|
|
|
|
- Entity.PLCInfo.Ex().Accessor.WriteBytes(Entity.No, start, data);
|
|
|
+ Entity.PLCInfo.Ex(World).Accessor.WriteBytes(Entity.No, start, data);
|
|
|
data.CopyTo(Data, start - Start);
|
|
|
bitStart += data.Length * 8;
|
|
|
}
|
|
@@ -456,7 +463,7 @@ namespace WCS.Core
|
|
|
|
|
|
private void WriteBit(ref int bitStart, bool flag)
|
|
|
{
|
|
|
- lock (Entity.PLCInfo.Ex())
|
|
|
+ lock (Entity.PLCInfo.Ex(World))
|
|
|
{
|
|
|
var start = GetByteStart(bitStart);
|
|
|
var bitIndex = GetBitIndex(bitStart);
|
|
@@ -465,7 +472,7 @@ namespace WCS.Core
|
|
|
b = b.SetBit(bitIndex, flag);
|
|
|
var data = new byte[] { b };
|
|
|
|
|
|
- Entity.PLCInfo.Ex().Accessor.WriteBytes(Entity.No, (ushort)start, data);
|
|
|
+ Entity.PLCInfo.Ex(World).Accessor.WriteBytes(Entity.No, (ushort)start, data);
|
|
|
data.CopyTo(Data, start - Start);
|
|
|
|
|
|
bitStart += 1;
|