Browse Source

fix bug

fix a bug in clientDriver
master
Gavin 6 years ago
parent
commit
12b0aba63f
  1. BIN
      SCADA/Example/CoreTest.exe
  2. BIN
      SCADA/Program/.vs/DataExchange/v16/.suo
  3. 319
      SCADA/Program/ClientDriver/ClientDriver.cs

BIN
SCADA/Example/CoreTest.exe

Binary file not shown.

BIN
SCADA/Program/.vs/DataExchange/v16/.suo

Binary file not shown.

319
SCADA/Program/ClientDriver/ClientDriver.cs

@ -365,175 +365,216 @@ namespace ClientDriver
private void ReciveData() private void ReciveData()
{ {
if (!_active || _plcReader.tcpRecive == null) return; if (!_active || _plcReader.tcpRecive == null) return;
List<HistoryData> historys = null; List<HistoryData> historys = new List<HistoryData>(); ;
byte[] bytes = new byte[ushort.MaxValue]; byte[] bytes = new byte[ushort.MaxValue];
byte[] temp = new byte[_tcpRecive.ReceiveBufferSize]; byte[] temp = new byte[ushort.MaxValue];
Storage value = Storage.Empty; Storage value = Storage.Empty;
int result = 0;
int start = 0; int start = 0;
SocketError error; SocketError error;
int result = 0;
do do
{ {
if (!_tcpRecive.Connected) return; if (!_tcpRecive.Connected || !_active) return;
result = _tcpRecive.Receive(bytes, 0, bytes.Length, SocketFlags.None, out error); try
if (error == SocketError.Success)
{ {
if (DataChange != null) result = _tcpRecive.Receive(bytes, 0, bytes.Length, SocketFlags.None, out error);
historys = new List<HistoryData>(); if (error == SocketError.Success)
//DateTime time = DateTime.Now;//当前时间戳
if (start != 0 && temp[0] == FCTCOMMAND.fctHead)
{ {
int j = 3; if (start != 0 && temp[0] == FCTCOMMAND.fctHead)
if (start < 0)
{ {
Array.Copy(bytes, 0, temp, -start, 5 + start); int j = 3;
} if (start < 0)
short tc = BitConverter.ToInt16(temp, j);//总字节数 {
if (start < 0) Array.Copy(bytes, 0, temp, -start, 5 + start);
start += tc; }
Array.Copy(bytes, 0, temp, tc - start, start); short tc = BitConverter.ToInt16(temp, j);//总字节数
j += 2; if (start < 0)
while (j < tc) start += tc;
{ Array.Copy(bytes, 0, temp, tc - start, start);
short id = BitConverter.ToInt16(temp, j);//标签ID、数据长度、数据值(T,L,V)
j += 2; j += 2;
byte length = temp[j++]; while (j < tc)
ITag tag;
if (_items.TryGetValue(id, out tag))
{ {
//数据类型 short id = BitConverter.ToInt16(temp, j);//标签ID、数据长度、数据值(T,L,V)
switch (tag.Address.VarType) j += 2;
byte length = temp[j++];
ITag tag;
if (_items.TryGetValue(id, out tag))
{ {
case DataType.BOOL: //数据类型
value.Boolean = BitConverter.ToBoolean(temp, j); switch (tag.Address.VarType)
break; {
case DataType.BYTE: case DataType.BOOL:
value.Byte = temp[j]; value.Boolean = BitConverter.ToBoolean(temp, j);
break; break;
case DataType.WORD: case DataType.BYTE:
value.Word = BitConverter.ToUInt16(temp, j);//需测试 value.Byte = temp[j];
break; break;
case DataType.SHORT: case DataType.WORD:
value.Int16 = BitConverter.ToInt16(temp, j);//需测试 value.Word = BitConverter.ToUInt16(temp, j);//需测试
break; break;
case DataType.DWORD: case DataType.SHORT:
value.DWord = BitConverter.ToUInt32(temp, j);//需测试 value.Int16 = BitConverter.ToInt16(temp, j);//需测试
break; break;
case DataType.INT: case DataType.DWORD:
value.Int32 = BitConverter.ToInt32(temp, j);//需测试 value.DWord = BitConverter.ToUInt32(temp, j);//需测试
break; break;
case DataType.FLOAT: case DataType.INT:
value.Single = BitConverter.ToSingle(temp, j); value.Int32 = BitConverter.ToInt32(temp, j);//需测试
break; break;
case DataType.STR: case DataType.FLOAT:
StringTag strTag = tag as StringTag; value.Single = BitConverter.ToSingle(temp, j);
if (strTag != null) break;
{ case DataType.STR:
strTag.String = Encoding.ASCII.GetString(temp, j, length).Trim((char)0); StringTag strTag = tag as StringTag;
} if (strTag != null)
break; {
default: strTag.String = Encoding.ASCII.GetString(temp, j, length).Trim((char)0);
break; }
break;
default:
break;
}
j += length;
try
{
DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(temp, j));
//tag.Update(value, time, QUALITIES.QUALITY_GOOD);
//if (historys != null)
historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time));
}
catch (Exception exp)
{
}
j += 8;
} }
j += length; else
DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(temp, j)); j += length + 8;
j += 8;
tag.Update(value, time, QUALITIES.QUALITY_GOOD);
if (historys != null)
historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time));
} }
else
j += length + 8;
} }
} byte head = bytes[start];
byte head = bytes[start]; int count = start;
int count = start; while (head == FCTCOMMAND.fctHead && result > count)
while (head == FCTCOMMAND.fctHead && result > count)
{
if (count + 5 > bytes.Length)
{ {
start = count - bytes.Length; if (count + 5 > bytes.Length)
Array.Copy(bytes, count, temp, 0, -start); {
break; start = count - bytes.Length;
} Array.Copy(bytes, count, temp, 0, -start);
int j = count + 3; break;
short tc = BitConverter.ToInt16(bytes, j);//总标签数 }
count += tc; int j = count + 3;
if (count >= bytes.Length) short tc = BitConverter.ToInt16(bytes, j);//总标签数
{ count += tc;
start = count - bytes.Length; if (count >= bytes.Length)
Array.Copy(bytes, count - tc, temp, 0, tc - start); {
break; start = count - bytes.Length;
} Array.Copy(bytes, count - tc, temp, 0, tc - start);
else start = 0; break;
j += 2; }
while (j < count) else start = 0;
{
short id = BitConverter.ToInt16(bytes, j);//标签ID、数据长度、数据值(T,L,V)
j += 2; j += 2;
byte length = bytes[j++]; while (j < count)
ITag tag;
if (_items.TryGetValue(id, out tag))
{ {
//数据类型 short id = BitConverter.ToInt16(bytes, j);//标签ID、数据长度、数据值(T,L,V)
switch (tag.Address.VarType) j += 2;
byte length = bytes[j++];
ITag tag;
if (_items.TryGetValue(id, out tag))
{ {
case DataType.BOOL: //数据类型
value.Boolean = BitConverter.ToBoolean(bytes, j); switch (tag.Address.VarType)
break; {
case DataType.BYTE: case DataType.BOOL:
value.Byte = bytes[j]; value.Boolean = BitConverter.ToBoolean(bytes, j);
break; break;
case DataType.WORD: case DataType.BYTE:
value.Word = BitConverter.ToUInt16(bytes, j);//需测试 value.Byte = bytes[j];
break; break;
case DataType.SHORT: case DataType.WORD:
value.Int16 = BitConverter.ToInt16(bytes, j);//需测试 value.Word = BitConverter.ToUInt16(bytes, j);//需测试
break; break;
case DataType.DWORD: case DataType.SHORT:
value.DWord = BitConverter.ToUInt32(bytes, j);//需测试 value.Int16 = BitConverter.ToInt16(bytes, j);//需测试
break; break;
case DataType.INT: case DataType.DWORD:
value.Int32 = BitConverter.ToInt32(bytes, j);//需测试 value.DWord = BitConverter.ToUInt32(bytes, j);//需测试
break; break;
case DataType.FLOAT: case DataType.INT:
value.Single = BitConverter.ToSingle(bytes, j); value.Int32 = BitConverter.ToInt32(bytes, j);//需测试
break; break;
case DataType.STR: case DataType.FLOAT:
StringTag strTag = tag as StringTag; value.Single = BitConverter.ToSingle(bytes, j);
if (strTag != null) break;
{ case DataType.STR:
strTag.String = Encoding.ASCII.GetString(bytes, j, length).Trim((char)0); StringTag strTag = tag as StringTag;
} if (strTag != null)
break; {
default: strTag.String = Encoding.ASCII.GetString(bytes, j, length).Trim((char)0);
break; }
break;
default:
break;
}
j += length;
try
{
DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(bytes, j));
//tag.Update(value, time, QUALITIES.QUALITY_GOOD);
//if (historys != null)
historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time));
}
catch (Exception exp)
{
}
j += 8;
} }
j += length; else
DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(bytes, j)); j += length + 8;
j += 8;
tag.Update(value, time, QUALITIES.QUALITY_GOOD);
if (historys != null)
historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time));
} }
else head = bytes[count];
j += length + 8;
} }
head = bytes[count]; ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(this.OnRecieve), historys);
} //if (DataChange != null && historys.Count > 0)
if (DataChange != null && historys.Count > 0) // DataChange(this, new DataChangeEventArgs(1, historys));
DataChange(this, new DataChangeEventArgs(1, historys));
}
else if (error == SocketError.ConnectionReset || error == SocketError.Interrupted
|| error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{
_tcpRecive.Dispose();
return;
}
} }
else if (error == SocketError.ConnectionReset || error == SocketError.Interrupted catch (Exception)
|| error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{ {
_tcpRecive.Dispose(); _tcpRecive.Dispose();
_active = false;
return; return;
} }
} }
while (result > 0); while (result > 0);
try
{
_tcpRecive.Dispose();
}
catch (Exception)
{
}
}
private void OnRecieve(object stateInfo)
{
var historys = stateInfo as List<HistoryData>;
if (historys == null) return;
if (DataChange != null && historys.Count > 0)
DataChange(this, new DataChangeEventArgs(1, historys));
for (int i = 0; i < historys.Count; i++)
{
var data = historys[i];
ITag tag;
if (_items.TryGetValue(data.ID, out tag))
{
tag.Update(data.Value, data.TimeStamp, data.Quality);
}
}
} }
public void OnUpdate(object stateInfo) public void OnUpdate(object stateInfo)

Loading…
Cancel
Save