|
@@ -1,19 +1,12 @@
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
+using PlcSiemens.Communication;
|
|
|
+using PlcSiemens.Protocol.Common;
|
|
|
+using PlcSiemens.Protocol.ReadData;
|
|
|
+using PlcSiemens.Protocol.Szl;
|
|
|
+using PlcSiemens.ProtocolHandle;
|
|
|
using System.Text;
|
|
|
-using System.Threading;
|
|
|
-using PLC.Siemens.Communication;
|
|
|
-using PLC.Siemens.Protocol.Common;
|
|
|
-using PLC.Siemens.Protocol.Iso;
|
|
|
-using PLC.Siemens.Protocol.Negotiate;
|
|
|
-using PLC.Siemens.Protocol.ReadData;
|
|
|
-using PLC.Siemens.Protocol.Szl;
|
|
|
-using PLC.Siemens.ProtocolHandle;
|
|
|
-using PLC.Siemens.O;
|
|
|
-using System.Linq;
|
|
|
-
|
|
|
-namespace PLC.Siemens.O
|
|
|
-
|
|
|
+
|
|
|
+namespace PlcSiemens.O
|
|
|
+
|
|
|
{
|
|
|
public class SimenssPlc
|
|
|
{
|
|
@@ -26,6 +19,7 @@ namespace PLC.Siemens.O
|
|
|
public int RecvTimeout { get; set; }
|
|
|
public int RemotePort { get; set; }
|
|
|
public int IsoPduSize { get; set; }
|
|
|
+
|
|
|
// ReSharper disable once InconsistentNaming
|
|
|
public string IP { get; private set; }
|
|
|
|
|
@@ -36,11 +30,10 @@ namespace PLC.Siemens.O
|
|
|
if (_socket == null)
|
|
|
return false;
|
|
|
return _socket.Connected;
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private Action<PLC.Siemens.O.MessageEvent> _onMessageEvent;
|
|
|
+ private Action<PlcSiemens.O.MessageEvent> _onMessageEvent;
|
|
|
|
|
|
private readonly IsoSocket _socket;
|
|
|
|
|
@@ -62,12 +55,13 @@ namespace PLC.Siemens.O
|
|
|
DstRef = 0x0000;
|
|
|
SrcTSap = 0x0100;
|
|
|
DstTSap = 0x0000; // It's filled by connection functions
|
|
|
- ConnectionType = ConnectionType.PG;
|
|
|
+ ConnectionType = ConnectionType.PG;
|
|
|
}
|
|
|
+
|
|
|
public SimenssPlc(string ip, int rack, int slot)
|
|
|
{
|
|
|
_socket = new IsoSocket();
|
|
|
- PduRequest = 480*2;
|
|
|
+ PduRequest = 480 * 2;
|
|
|
IsoPduSize = 1024;
|
|
|
RecvTimeout = 3000;
|
|
|
RemotePort = 102;
|
|
@@ -85,10 +79,11 @@ namespace PLC.Siemens.O
|
|
|
ConnectionType = ConnectionType.PG;
|
|
|
|
|
|
var remoteTsap = (ushort)(((ushort)ConnectionType << 8) + (rack * 0x20) + slot);
|
|
|
- SetConnectionParams(ip, SrcTSap, remoteTsap);
|
|
|
+ SetConnectionParams(ip, SrcTSap, remoteTsap);
|
|
|
}
|
|
|
|
|
|
#region 连接
|
|
|
+
|
|
|
public void RegisterMessageEvent(Action<MessageEvent> messageAction)
|
|
|
{
|
|
|
if (messageAction == null) throw new ArgumentNullException("messageAction");
|
|
@@ -97,7 +92,7 @@ namespace PLC.Siemens.O
|
|
|
|
|
|
private void OnMessage(string method, string message)
|
|
|
{
|
|
|
- if(_onMessageEvent!=null)
|
|
|
+ if (_onMessageEvent != null)
|
|
|
_onMessageEvent.Invoke(new MessageEvent(method, message));
|
|
|
}
|
|
|
|
|
@@ -122,8 +117,10 @@ namespace PLC.Siemens.O
|
|
|
|
|
|
//暂时用系统SOCKET测试
|
|
|
private int _connecting;//连接中
|
|
|
+
|
|
|
private int _processing;//处理中
|
|
|
private string _processMehtod;
|
|
|
+
|
|
|
public bool Connect()
|
|
|
{
|
|
|
try
|
|
@@ -133,7 +130,7 @@ namespace PLC.Siemens.O
|
|
|
OnMessage("Connect", "正在连接");
|
|
|
if (!_socket.Connect(IP, 102))
|
|
|
return false;
|
|
|
-
|
|
|
+
|
|
|
IsoControlPduHandle pduHandle = new IsoControlPduHandle()
|
|
|
{
|
|
|
IsoPduSize = IsoPduSize,
|
|
@@ -172,8 +169,9 @@ namespace PLC.Siemens.O
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
- #endregion
|
|
|
-
|
|
|
+
|
|
|
+ #endregion 连接
|
|
|
+
|
|
|
public bool GetLock(string method)
|
|
|
{
|
|
|
if (Interlocked.CompareExchange(ref _processing, 1, 0) != 0)
|
|
@@ -199,7 +197,7 @@ namespace PLC.Siemens.O
|
|
|
handle.Handle(_socket);
|
|
|
return handle.SzlResponseFirst;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public SzlResponse ReadSzl(SzlCmd szlcmd)
|
|
|
{
|
|
|
if (!GetLock("ReadSzl"))
|
|
@@ -273,7 +271,6 @@ namespace PLC.Siemens.O
|
|
|
cpuInfo.AsName = Encoding.UTF8.GetString(aaaa.Data.Data, 2, 24).TrimEnd('\0');
|
|
|
cpuInfo.Copyright = Encoding.UTF8.GetString(aaaa.Data.Data, 104, 26).TrimEnd('\0');
|
|
|
cpuInfo.ModuleName = Encoding.UTF8.GetString(aaaa.Data.Data, 36, 24).TrimEnd('\0');
|
|
|
-
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -317,7 +314,8 @@ namespace PLC.Siemens.O
|
|
|
}
|
|
|
return info;
|
|
|
}
|
|
|
- #endregion
|
|
|
+
|
|
|
+ #endregion [System Info functions]
|
|
|
|
|
|
private int DWordAt(byte[] buffer, int index, int length)
|
|
|
{
|
|
@@ -331,6 +329,7 @@ namespace PLC.Siemens.O
|
|
|
}
|
|
|
|
|
|
#region [Date/Time functions]
|
|
|
+
|
|
|
public DateTime GetS7DateTime()
|
|
|
{
|
|
|
if (!GetLock("GetS7DateTime"))
|
|
@@ -383,9 +382,11 @@ namespace PLC.Siemens.O
|
|
|
ReleaseLock();
|
|
|
}
|
|
|
}
|
|
|
- #endregion
|
|
|
+
|
|
|
+ #endregion [Date/Time functions]
|
|
|
|
|
|
#region [Control functions]
|
|
|
+
|
|
|
public S7CpuStatus GetPlcStatus()
|
|
|
{
|
|
|
S7CpuStatus status = S7CpuStatus.Unknown;
|
|
@@ -515,7 +516,6 @@ namespace PLC.Siemens.O
|
|
|
|
|
|
public bool Compress()
|
|
|
{
|
|
|
-
|
|
|
if (!GetLock("Compress"))
|
|
|
return false;
|
|
|
|
|
@@ -536,12 +536,12 @@ namespace PLC.Siemens.O
|
|
|
{
|
|
|
ReleaseLock();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
+ #endregion [Control functions]
|
|
|
|
|
|
#region [Security functions]
|
|
|
+
|
|
|
public ProtectionInfo GetProtection()
|
|
|
{
|
|
|
ProtectionInfo info = new ProtectionInfo();
|
|
@@ -618,9 +618,11 @@ namespace PLC.Siemens.O
|
|
|
ReleaseLock();
|
|
|
}
|
|
|
}
|
|
|
- #endregion
|
|
|
+
|
|
|
+ #endregion [Security functions]
|
|
|
|
|
|
#region [Blocks]
|
|
|
+
|
|
|
public ListBlocksHandle ListBlocks()
|
|
|
{
|
|
|
if (!GetLock("ListBlocks")) return null;
|
|
@@ -685,23 +687,22 @@ namespace PLC.Siemens.O
|
|
|
{
|
|
|
ReleaseLock();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
- #endregion
|
|
|
|
|
|
- object readlock = new object();
|
|
|
+ #endregion [Blocks]
|
|
|
+
|
|
|
+ private object readlock = new object();
|
|
|
+
|
|
|
public DataItem ReadArea(AreaType area, ushort db, ushort start, ushort amount, DataType dataType)
|
|
|
{
|
|
|
lock (readlock)
|
|
|
{
|
|
|
-
|
|
|
if (!GetLock("ReadArea"))
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
-
|
|
|
}
|
|
|
try
|
|
|
{
|
|
@@ -756,7 +757,6 @@ namespace PLC.Siemens.O
|
|
|
}
|
|
|
finally
|
|
|
|
|
|
-
|
|
|
{
|
|
|
ReleaseLock();
|
|
|
}
|
|
@@ -770,7 +770,6 @@ namespace PLC.Siemens.O
|
|
|
{
|
|
|
if (writeData.Max() == 0)
|
|
|
{
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
else if (start % 36 == 0)
|
|
@@ -779,12 +778,10 @@ namespace PLC.Siemens.O
|
|
|
{
|
|
|
if (writeData.Skip(16).Take(2).Max() == 0)
|
|
|
{
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
+ else
|
|
|
+ {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -808,7 +805,7 @@ namespace PLC.Siemens.O
|
|
|
};
|
|
|
OnMessage("WriteArea", handle.ResultCode.ToString());
|
|
|
handle.Handle(_socket);
|
|
|
- return handle.ResultCode == ResultCode.OK|| handle.ResultCode == ResultCode.OKFF;
|
|
|
+ return handle.ResultCode == ResultCode.OK || handle.ResultCode == ResultCode.OKFF;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -844,7 +841,7 @@ namespace PLC.Siemens.O
|
|
|
ReleaseLock();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// ReSharper disable once InconsistentNaming
|
|
|
public DataItem DBGet(ushort db)
|
|
|
{
|
|
@@ -866,7 +863,6 @@ namespace PLC.Siemens.O
|
|
|
}
|
|
|
else
|
|
|
OnMessage("DBGet", "RequestBlock:{handle.ResultCode}");
|
|
|
-
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -927,8 +923,7 @@ namespace PLC.Siemens.O
|
|
|
handle1.UploadID = handle.Response.Params.UploadID;
|
|
|
handle1.Handle(_socket);
|
|
|
|
|
|
-
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+}
|