diff --git a/S7.Net.Common/Types/DataItem.cs b/S7.Net.Common/Types/DataItem.cs index 0cda855..87a294e 100644 --- a/S7.Net.Common/Types/DataItem.cs +++ b/S7.Net.Common/Types/DataItem.cs @@ -6,7 +6,6 @@ public VarType VarType { get; set; } public int DB { get; set; } public int StartByteAdr { get; set; } - public int StartBitAdr { get; set; } = 0; public int Count { get; set; } = 1; public object Value { get; set; } diff --git a/S7.Net.UnitTest/S7NetTests.cs b/S7.Net.UnitTest/S7NetTests.cs index 87ba2bc..2abf790 100644 --- a/S7.Net.UnitTest/S7NetTests.cs +++ b/S7.Net.UnitTest/S7NetTests.cs @@ -403,7 +403,6 @@ namespace S7.Net.UnitTest Count = 1, DataType = DataType.DataBlock, DB = 2, - StartBitAdr = 0, StartByteAdr = 16384, VarType = VarType.Word }, @@ -412,7 +411,6 @@ namespace S7.Net.UnitTest Count = 1, DataType = DataType.DataBlock, DB = 2, - StartBitAdr = 0, StartByteAdr = 16, VarType = VarType.Word } diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index 22482b3..4f88b85 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -17,22 +17,22 @@ namespace S7.Net /// /// Ip address of the plc /// - public string IP { get; set; } + public string IP { get; private set; } /// /// Cpu type of the plc /// - public CpuType CPU { get; set; } + public CpuType CPU { get; private set; } /// /// Rack of the plc /// - public Int16 Rack { get; set; } + public Int16 Rack { get; private set; } /// /// Slot of the CPU of the plc /// - public Int16 Slot { get; set; } + public Int16 Slot { get; private set; } /// /// Name of the plc (optional, is not used anywhere in the driver) @@ -95,13 +95,7 @@ namespace S7.Net /// 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) { } - + /// /// Creates a PLC object with all the parameters needed for connections. /// For S7-1200 and S7-1500, the default is rack = 0 and slot = 0. @@ -256,23 +250,6 @@ namespace S7.Net _mSocket.Close(); } } - - private Types.ByteArray ReadHeaderPackage(int amount = 1) - { - //header size = 19 bytes - var package = new Types.ByteArray(19); - package.Add(new byte[] {0x03, 0x00, 0x00}); - //complete package size - package.Add((byte) (19 + (12*amount))); - package.Add(new byte[] {0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00}); - //data part size - package.Add(Types.Word.ToByteArray((ushort)(2 + (amount*12)))); - package.Add(new byte[] {0x00, 0x00, 0x04}); - //amount of requests - package.Add((byte)amount); - - return package; - } private Types.ByteArray ReadDataRequestPackage(DataType dataType, int DB, int startByteAdr, int count = 1) { @@ -309,6 +286,12 @@ namespace S7.Net return package; } + /// + /// Reads multiple vars in a single request. You have to create and pass a list of DataItems and you obtain in response the same list with the values. + /// DataItems must not be more than 20 (protocol restriction) and bytes must not be more than 200 + 22 of header (protocol restriction). + /// + /// List of dataitems that contains the list of variables that must be read. Maximum 20 dataitems are accepted. + /// List of dataItem containing the values of the variables public List ReadMultipleVars(List dataItems) { int cntBytes = dataItems.Sum(dataItem => VarTypeToByteLength(dataItem.VarType, dataItem.Count)); @@ -651,17 +634,6 @@ 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. /// @@ -669,7 +641,7 @@ namespace S7.Net /// 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) + public object ReadStruct(Type structType, int db, int startByteAdr = 0) { int numBytes = Types.Struct.GetStructSize(structType); // now read the package @@ -679,17 +651,6 @@ namespace S7.Net return Types.Struct.FromBytes(structType, resultBytes.ToArray()); } - /// - /// 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 - public void ReadClass(object sourceClass, int db) - { - 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. @@ -697,7 +658,7 @@ namespace S7.Net /// 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) + public void ReadClass(object sourceClass, int db, int startByteAdr = 0) { Type classType = sourceClass.GetType(); int numBytes = Types.Class.GetClassSize(classType); @@ -973,42 +934,21 @@ namespace S7.Net } } - public ErrorCode WriteStruct(object structValue, int db) - { - return WriteStruct(structValue, db, 0); - } - - public ErrorCode WriteStruct(object structValue, int db, int startByteAdr) + public ErrorCode WriteStruct(object structValue, int db, int startByteAdr = 0) { var bytes = Types.Struct.ToBytes(structValue).ToList(); var errCode = WriteMultipleBytes(bytes, db, startByteAdr); return errCode; } - public ErrorCode WriteClass(object classValue, int db) - { - return WriteClass(classValue, db, 0); - } - - public ErrorCode WriteClass(object classValue, int db, int startByteAdr) + public ErrorCode WriteClass(object classValue, int db, int startByteAdr = 0) { var bytes = Types.Class.ToBytes(classValue).ToList(); var errCode = WriteMultipleBytes(bytes, db, startByteAdr); return errCode; } - /// - /// Writes multiple bytes in a DB starting from index 0. This handles more than 200 bytes with multiple requests. - /// - /// The bytes to be written - /// The DB number - /// ErrorCode when writing (NoError if everything was ok) - private ErrorCode WriteMultipleBytes(List bytes, int db) - { - return WriteMultipleBytes(bytes, db, 0); - } - - private ErrorCode WriteMultipleBytes(List bytes, int db, int startByteAdr) + private ErrorCode WriteMultipleBytes(List bytes, int db, int startByteAdr = 0) { ErrorCode errCode = ErrorCode.NoError; int index = startByteAdr; @@ -1035,17 +975,6 @@ namespace S7.Net return errCode; } - /// - /// Reads a number of bytes from a DB starting from index 0. This handles more than 200 bytes with multiple requests. - /// - /// - /// - /// - private List ReadMultipleBytes(int numBytes, int db) - { - 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. /// @@ -1053,7 +982,7 @@ namespace S7.Net /// /// /// - private List ReadMultipleBytes(int numBytes, int db, int startByteAdr) + private List ReadMultipleBytes(int numBytes, int db, int startByteAdr = 0) { List resultBytes = new List(); int index = startByteAdr; @@ -1070,6 +999,28 @@ namespace S7.Net return resultBytes; } + /// + /// Creates the header to read bytes from the plc + /// + /// + /// + private Types.ByteArray ReadHeaderPackage(int amount = 1) + { + //header size = 19 bytes + var package = new Types.ByteArray(19); + package.Add(new byte[] { 0x03, 0x00, 0x00 }); + //complete package size + package.Add((byte)(19 + (12 * amount))); + package.Add(new byte[] { 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00 }); + //data part size + package.Add(Types.Word.ToByteArray((ushort)(2 + (amount * 12)))); + package.Add(new byte[] { 0x00, 0x00, 0x04 }); + //amount of requests + package.Add((byte)amount); + + return package; + } + #region IDisposable members