From d9d2c19dcd94280e93d052f391db9e2bbc789d3d Mon Sep 17 00:00:00 2001 From: Michele Cattafesta Date: Mon, 12 Sep 2016 23:10:16 +0200 Subject: [PATCH] WriteBytesWithASingleRequest private. Signed-off-by: Michele Cattafesta --- S7.Net/PLC.cs | 116 +++++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index 264a02d..997259d 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -559,64 +559,6 @@ namespace S7.Net return ErrorCode.NoError; } - /// - /// Writes up to 200 bytes to the plc and returns NoError if successful. You must specify the memory area type, memory are address, byte start address and bytes count. - /// If the write 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. - /// Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion. - /// NoError if it was successful, or the error is specified - public ErrorCode WriteBytesWithASingleRequest(DataType dataType, int db, int startByteAdr, byte[] value) - { - byte[] bReceive = new byte[513]; - int varCount = 0; - - try - { - varCount = value.Length; - // first create the header - int packageSize = 35 + value.Length; - Types.ByteArray package = new Types.ByteArray(packageSize); - - package.Add(new byte[] { 3, 0, 0 }); - package.Add((byte)packageSize); - package.Add(new byte[] { 2, 0xf0, 0x80, 0x32, 1, 0, 0 }); - package.Add(Types.Word.ToByteArray((ushort)(varCount - 1))); - package.Add(new byte[] { 0, 0x0e }); - package.Add(Types.Word.ToByteArray((ushort)(varCount + 4))); - package.Add(new byte[] { 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02 }); - package.Add(Types.Word.ToByteArray((ushort)varCount)); - package.Add(Types.Word.ToByteArray((ushort)(db))); - package.Add((byte)dataType); - var overflow = (int) (startByteAdr*8/0xffffU); // handles words with address bigger than 8191 - package.Add((byte)overflow); - package.Add(Types.Word.ToByteArray((ushort)(startByteAdr * 8))); - package.Add(new byte[] { 0, 4 }); - package.Add(Types.Word.ToByteArray((ushort)(varCount * 8))); - - // now join the header and the data - package.Add(value); - - _mSocket.Send(package.array, package.array.Length, SocketFlags.None); - - int numReceived = _mSocket.Receive(bReceive, 512, SocketFlags.None); - if (bReceive[21] != 0xff) - { - throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString()); - } - - return ErrorCode.NoError; - } - catch(Exception exc) - { - LastErrorCode = ErrorCode.WriteData; - LastErrorString = exc.Message; - return LastErrorCode; - } - } - /// /// Takes in input an object and tries to parse it to an array of values. This can be used to write many data, all of the same type. /// You must specify the memory area type, memory are address, byte start address and bytes count. @@ -1027,6 +969,64 @@ namespace S7.Net } } + /// + /// Writes up to 200 bytes to the plc and returns NoError if successful. You must specify the memory area type, memory are address, byte start address and bytes count. + /// If the write 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. + /// Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion. + /// NoError if it was successful, or the error is specified + private ErrorCode WriteBytesWithASingleRequest(DataType dataType, int db, int startByteAdr, byte[] value) + { + byte[] bReceive = new byte[513]; + int varCount = 0; + + try + { + varCount = value.Length; + // first create the header + int packageSize = 35 + value.Length; + Types.ByteArray package = new Types.ByteArray(packageSize); + + package.Add(new byte[] { 3, 0, 0 }); + package.Add((byte)packageSize); + package.Add(new byte[] { 2, 0xf0, 0x80, 0x32, 1, 0, 0 }); + package.Add(Types.Word.ToByteArray((ushort)(varCount - 1))); + package.Add(new byte[] { 0, 0x0e }); + package.Add(Types.Word.ToByteArray((ushort)(varCount + 4))); + package.Add(new byte[] { 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02 }); + package.Add(Types.Word.ToByteArray((ushort)varCount)); + package.Add(Types.Word.ToByteArray((ushort)(db))); + package.Add((byte)dataType); + var overflow = (int)(startByteAdr * 8 / 0xffffU); // handles words with address bigger than 8191 + package.Add((byte)overflow); + package.Add(Types.Word.ToByteArray((ushort)(startByteAdr * 8))); + package.Add(new byte[] { 0, 4 }); + package.Add(Types.Word.ToByteArray((ushort)(varCount * 8))); + + // now join the header and the data + package.Add(value); + + _mSocket.Send(package.array, package.array.Length, SocketFlags.None); + + int numReceived = _mSocket.Receive(bReceive, 512, SocketFlags.None); + if (bReceive[21] != 0xff) + { + throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString()); + } + + return ErrorCode.NoError; + } + catch (Exception exc) + { + LastErrorCode = ErrorCode.WriteData; + LastErrorString = exc.Message; + return LastErrorCode; + } + } + /// /// Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format. ///