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. 73
      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.

73
SCADA/Program/ClientDriver/ClientDriver.cs

@ -365,22 +365,21 @@ 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;
try
{
result = _tcpRecive.Receive(bytes, 0, bytes.Length, SocketFlags.None, out error); result = _tcpRecive.Receive(bytes, 0, bytes.Length, SocketFlags.None, out error);
if (error == SocketError.Success) if (error == SocketError.Success)
{ {
if (DataChange != null)
historys = new List<HistoryData>();
//DateTime time = DateTime.Now;//当前时间戳
if (start != 0 && temp[0] == FCTCOMMAND.fctHead) if (start != 0 && temp[0] == FCTCOMMAND.fctHead)
{ {
int j = 3; int j = 3;
@ -436,12 +435,18 @@ namespace ClientDriver
break; break;
} }
j += length; j += length;
try
{
DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(temp, j)); DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(temp, j));
j += 8; //tag.Update(value, time, QUALITIES.QUALITY_GOOD);
tag.Update(value, time, QUALITIES.QUALITY_GOOD); //if (historys != null)
if (historys != null)
historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time)); historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time));
} }
catch (Exception exp)
{
}
j += 8;
}
else else
j += length + 8; j += length + 8;
} }
@ -510,30 +515,66 @@ namespace ClientDriver
break; break;
} }
j += length; j += length;
try
{
DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(bytes, j)); DateTime time = DateTime.FromFileTime(BitConverter.ToInt64(bytes, j));
j += 8; //tag.Update(value, time, QUALITIES.QUALITY_GOOD);
tag.Update(value, time, QUALITIES.QUALITY_GOOD); //if (historys != null)
if (historys != null)
historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time)); historys.Add(new HistoryData(id, QUALITIES.QUALITY_GOOD, value, time));
} }
catch (Exception exp)
{
}
j += 8;
}
else else
j += length + 8; j += length + 8;
} }
head = bytes[count]; head = bytes[count];
} }
if (DataChange != null && historys.Count > 0) ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(this.OnRecieve), historys);
DataChange(this, new DataChangeEventArgs(1, historys)); //if (DataChange != null && historys.Count > 0)
// DataChange(this, new DataChangeEventArgs(1, historys));
} }
else if (error == SocketError.ConnectionReset || error == SocketError.Interrupted else if (error == SocketError.ConnectionReset || error == SocketError.Interrupted
|| error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown) || error == SocketError.HostDown || error == SocketError.NetworkDown || error == SocketError.Shutdown)
{ {
_tcpRecive.Dispose(); _tcpRecive.Dispose();
_active = false; return;
}
}
catch (Exception)
{
_tcpRecive.Dispose();
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