diff --git a/S7.Net.Common/Interfaces/IPLC.cs b/S7.Net.Common/Interfaces/IPLC.cs index 20efc32..ff23de6 100644 --- a/S7.Net.Common/Interfaces/IPLC.cs +++ b/S7.Net.Common/Interfaces/IPLC.cs @@ -4,27 +4,141 @@ namespace S7.Net.Interfaces { public interface IPlc : IDisposable { + /// + /// Ip address of the plc + /// string IP { get; set; } + + /// + /// Checks if the socket is connected and polls the other peer (the plc) to see if it's connected. + /// This is the variable that you should continously check to see if the communication is working + /// See also: http://stackoverflow.com/questions/2661764/how-to-check-if-a-socket-is-connected-disconnected-in-c + /// bool IsConnected { get; } + + /// + /// Cpu type of the plc + /// CpuType CPU { get; set; } + + /// + /// Rack of the plc + /// Int16 Rack { get; set; } + + /// + /// Slot of the CPU of the plc + /// Int16 Slot { get; set; } + + /// + /// Name of the plc (optional, is not used anywhere in the driver) + /// string Name { get; set; } + + /// + /// Tag of the plc (optional, is not used anywhere in the driver) + /// object Tag { get; set; } + + /// + /// Pings the IP address and returns true if the result of the ping is Success. + /// bool IsAvailable { get; } + + /// + /// Contains the last error registered when executing a function + /// + string LastErrorString { get; } + + /// + /// Contains the last error code registered when executing a function + /// + ErrorCode LastErrorCode { get; } + + /// + /// Open a socket and connects to the plc, sending all the corrected package and returning if the connection was successful (ErroreCode.NoError) of it was wrong. + /// + /// Returns ErrorCode.NoError if the connection was successful, otherwise check the ErrorCode ErrorCode Open(); + + /// + /// Disonnects from the plc and close the socket + /// void Close(); + + /// + /// Reads up to 200 bytes from the plc and returns an array of bytes. You must specify the memory area type, memory are address, byte start address and bytes count. + /// If the read was not successful, check LastErrorCode or LastErrorString. + /// + /// Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output. + /// Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc. + /// Start byte address. If you want to read DB1.DBW200, this is 200. + /// Byte count, if you want to read 120 bytes, set this to 120. This parameter can't be higher than 200. If you need more, use recursion. + /// Returns the bytes in an array byte[] ReadBytes(DataType dataType, int DB, int startByteAdr, int count); + + /// + /// Read and decode a certain number of bytes of the "VarType" provided. + /// This can be used to read multiple consecutive variables of the same type (Word, DWord, Int, etc). + /// If the read was not successful, check LastErrorCode or LastErrorString. + /// + /// Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output. + /// Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc. + /// Start byte address. If you want to read DB1.DBW200, this is 200. + /// Type of the variable/s that you are reading + /// Number of the variables (NOT number of bytes) to read + /// object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount); + + /// + /// Reads a single variable from the plc, takes in input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc. + /// If the read was not successful, check LastErrorCode or LastErrorString. + /// + /// Input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc. + /// Returns an object that contains the value. This object must be cast accordingly. object Read(string variable); + + /// + /// Reads all the bytes needed to fill a struct in C#, and return an object that can be casted to the struct. + /// + /// Type of the struct to be readed (es.: TypeOf(MyStruct)). + /// Address of the DB. + /// Returns a struct that must be cast. object ReadStruct(Type structType, int db); + + /// + /// Reads all the bytes needed to fill a struct in C#, starting from a certain address, and return an object that can be casted to the struct. + /// + /// Type of the struct to be readed (es.: TypeOf(MyStruct)). + /// Address of the DB. + /// Start byte address. If you want to read DB1.DBW200, this is 200. + /// Returns a struct that must be cast. + object ReadStruct(Type structType, int db, int startByteAdr); + + /// + /// Reads all the bytes needed to fill a class in C#, and set all the properties values to the value that are read from the plc. + /// This reads ony properties, it doesn't read private variable or public variable without {get;set;} specified. + /// + /// Instance of the class that will store the values + /// Index of the DB; es.: 1 is for DB1 void ReadClass(object sourceClass, int db); + + /// + /// Reads all the bytes needed to fill a class in C#, starting from a certain address, and set all the properties values to the value that are read from the plc. + /// This reads ony properties, it doesn't read private variable or public variable without {get;set;} specified. + /// + /// Instance of the class that will store the values + /// Index of the DB; es.: 1 is for DB1 + /// Start byte address. If you want to read DB1.DBW200, this is 200. + void ReadClass(object sourceClass, int db, int startByteAdr); + ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value); object Write(DataType dataType, int db, int startByteAdr, object value); object Write(string variable, object value); ErrorCode WriteStruct(object structValue, int db); ErrorCode WriteClass(object classValue, int db); - string LastErrorString { get; } - ErrorCode LastErrorCode { get; } + + } } \ No newline at end of file diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index 9897d6a..23fe0ce 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -14,11 +14,34 @@ namespace S7.Net { private Socket _mSocket; //TCP connection to device + /// + /// Ip address of the plc + /// public string IP { get; set; } + + /// + /// Cpu type of the plc + /// public CpuType CPU { get; set; } + + /// + /// Rack of the plc + /// public Int16 Rack { get; set; } + + /// + /// Slot of the CPU of the plc + /// public Int16 Slot { get; set; } + + /// + /// Name of the plc (optional, is not used anywhere in the driver) + /// public string Name { get; set; } + + /// + /// Tag of the plc (optional, is not used anywhere in the driver) + /// public object Tag { get; set; } /// @@ -62,9 +85,21 @@ namespace S7.Net catch { return false; } } } + + /// + /// Contains the last error registered when executing a function + /// public string LastErrorString { get; private set; } + + /// + /// Contains the last error code registered when executing a function + /// public ErrorCode LastErrorCode { get; private set; } + /// + /// Creates a plc with CpuType S7400 and ip: localhost. This constructor makes no sense and will be removed in future versions. + /// + [Obsolete("Use Plc(CpuType cpu, string ip, Int16 rack, Int16 slot)")] public Plc() : this(CpuType.S7400, "localhost", 0, 2) { } /// @@ -73,12 +108,13 @@ namespace S7.Net /// You need slot > 0 if you are connecting to external ethernet card (CP). /// For S7-300 and S7-400 the default is rack = 0 and slot = 2. /// - /// - /// - /// - /// - /// - /// + /// CpuType of the plc (select from the enum) + /// Ip address of the plc + /// rack of the plc, usually it's 0, but check in the hardware configuration of Step7 or TIA portal + /// slot of the CPU of the plc, usually it's 2 for S7300-S7400, 0 for S7-1200 and S7-1500. + /// If you use an external ethernet card, this must be set accordingly. + /// Name of the plc (optional, is not used anywhere in the driver) + /// Tag of the plc (optional, is not used anywhere in the driver) public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot, string name = "", object tag = null) { IP = ip; @@ -89,6 +125,10 @@ namespace S7.Net Tag = tag; } + /// + /// Open a socket and connects to the plc, sending all the corrected package and returning if the connection was successful (ErroreCode.NoError) of it was wrong. + /// + /// Returns ErrorCode.NoError if the connection was successful, otherwise check the ErrorCode public ErrorCode Open() { byte[] bReceive = new byte[256]; @@ -206,6 +246,9 @@ namespace S7.Net return ErrorCode.NoError; } + /// + /// Disonnects from the plc and close the socket + /// public void Close() { if (_mSocket != null && _mSocket.Connected) @@ -214,6 +257,15 @@ namespace S7.Net } } + /// + /// Reads up to 200 bytes from the plc and returns an array of bytes. You must specify the memory area type, memory are address, byte start address and bytes count. + /// If the read was not successful, check LastErrorCode or LastErrorString. + /// + /// Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output. + /// Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc. + /// Start byte address. If you want to read DB1.DBW200, this is 200. + /// Byte count, if you want to read 120 bytes, set this to 120. This parameter can't be higher than 200. If you need more, use recursion. + /// Returns the bytes in an array public byte[] ReadBytes(DataType dataType, int DB, int startByteAdr, int count) { byte[] bytes = new byte[count]; @@ -281,6 +333,17 @@ namespace S7.Net } } + /// + /// Read and decode a certain number of bytes of the "VarType" provided. + /// This can be used to read multiple consecutive variables of the same type (Word, DWord, Int, etc). + /// If the read was not successful, check LastErrorCode or LastErrorString. + /// + /// Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output. + /// Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc. + /// Start byte address. If you want to read DB1.DBW200, this is 200. + /// Type of the variable/s that you are reading + /// Number of the variables (NOT number of bytes) to read + /// public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount) { byte[] bytes = null; @@ -371,6 +434,12 @@ namespace S7.Net } } + /// + /// Reads a single variable from the plc, takes in input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc. + /// If the read was not successful, check LastErrorCode or LastErrorString. + /// + /// Input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc. + /// Returns an object that contains the value. This object must be cast accordingly. public object Read(string variable) { DataType mDataType; @@ -506,11 +575,24 @@ namespace S7.Net } } + /// + /// Reads all the bytes needed to fill a struct in C#, and return an object that can be casted to the struct. + /// + /// Type of the struct to be readed (es.: TypeOf(MyStruct)). + /// Address of the DB. + /// Returns a struct that must be cast. public object ReadStruct(Type structType, int db) { return ReadStruct(structType, db, 0); } + /// + /// Reads all the bytes needed to fill a struct in C#, starting from a certain address, and return an object that can be casted to the struct. + /// + /// Type of the struct to be readed (es.: TypeOf(MyStruct)). + /// Address of the DB. + /// Start byte address. If you want to read DB1.DBW200, this is 200. + /// Returns a struct that must be cast. public object ReadStruct(Type structType, int db, int startByteAdr) { int numBytes = Types.Struct.GetStructSize(structType); @@ -522,7 +604,8 @@ namespace S7.Net } /// - /// Read a class from plc. Only properties are readed + /// Reads all the bytes needed to fill a class in C#, and set all the properties values to the value that are read from the plc. + /// This reads ony properties, it doesn't read private variable or public variable without {get;set;} specified. /// /// Instance of the class that will store the values /// Index of the DB; es.: 1 is for DB1 @@ -531,6 +614,13 @@ namespace S7.Net ReadClass(sourceClass, db, 0); } + /// + /// Reads all the bytes needed to fill a class in C#, starting from a certain address, and set all the properties values to the value that are read from the plc. + /// This reads ony properties, it doesn't read private variable or public variable without {get;set;} specified. + /// + /// Instance of the class that will store the values + /// Index of the DB; es.: 1 is for DB1 + /// Start byte address. If you want to read DB1.DBW200, this is 200. public void ReadClass(object sourceClass, int db, int startByteAdr) { Type classType = sourceClass.GetType(); @@ -880,6 +970,13 @@ namespace S7.Net return ReadMultipleBytes(numBytes, db, 0); } + /// + /// Reads a number of bytes from a DB starting from a specified index. This handles more than 200 bytes with multiple requests. + /// + /// + /// + /// + /// private List ReadMultipleBytes(int numBytes, int db, int startByteAdr) { List resultBytes = new List();