From 0d8ea7407178fe85529ca5b56733635834e49f32 Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Thu, 7 May 2020 00:58:24 +0200 Subject: [PATCH 01/11] Check Connection on Socket close --- Sharp7/S7Client.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sharp7/S7Client.cs b/Sharp7/S7Client.cs index 06aea3a..be06749 100644 --- a/Sharp7/S7Client.cs +++ b/Sharp7/S7Client.cs @@ -723,7 +723,10 @@ namespace Sharp7 public int Disconnect() { - Socket.Close(); + if (Socket != null && Socket.Connected) + { + Socket.Close(); + } return 0; } From 63c3e858e0d412995cb33863588ed8be343ac0f5 Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 12:07:53 +0200 Subject: [PATCH 02/11] updated Readme --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 9b9a3b2..77b062e 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,28 @@ PM> Install-Package Sharp7 ## Do you need more power? Try [Sharp7Reactive](https://github.com/evopro-ag/Sharp7Reactive) + +## Get Started + +### Supported Targets +- S7 300/400/WinAC CPU (fully supported) +- S7 1200/1500 CPU +- CP (Communication processor - 343/443/IE) + +### S7 1200/1500 Notes + +An external equipment can access to S71200/1500 CPU using the S7 'base' protocol, only working as an HMI, i.e. only basic data transfer are allowed. + +All other PG operations (control/directory/etc..) must follow the extended protocol, not implemented yet. + +Particularly **to access a DB in S71500 some additional setting plc-side are needed**. + +- Only global DBs can be accessed. + +![DB_props](http://snap7.sourceforge.net/snap7_client_file/db_1500.bmp) + +- The optimized block access must be turned off. + +- The access level must be “full” and the “connection mechanism” must allow GET/PUT. + +![DB_sec](http://snap7.sourceforge.net/snap7_client_file/cpu_1500.bmp) \ No newline at end of file From 906846775edf4eee64708aa76435f249ba82081f Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 12:09:54 +0200 Subject: [PATCH 03/11] changed to expression body solution wide --- Sharp7.Tests/S7Server.cs | 15 ++------- Sharp7/MsgSocket.cs | 39 ++++------------------ Sharp7/S7Client.cs | 70 +++++++--------------------------------- Sharp7/S7Timer.cs | 36 +++++---------------- 4 files changed, 30 insertions(+), 130 deletions(-) diff --git a/Sharp7.Tests/S7Server.cs b/Sharp7.Tests/S7Server.cs index c4644a7..ca76520 100644 --- a/Sharp7.Tests/S7Server.cs +++ b/Sharp7.Tests/S7Server.cs @@ -345,10 +345,7 @@ namespace Sharp7.Tests else return 0; } - set - { - Srv_SetMask(server, S7Server.MkLog, value); - } + set => Srv_SetMask(server, S7Server.MkLog, value); } // Property EventMask R/W @@ -365,10 +362,7 @@ namespace Sharp7.Tests else return 0; } - set - { - Srv_SetMask(server, S7Server.MkEvent, value); - } + set => Srv_SetMask(server, S7Server.MkEvent, value); } @@ -395,10 +389,7 @@ namespace Sharp7.Tests else return -1; } - set - { - Srv_SetCpuStatus(server, value); - } + set => Srv_SetCpuStatus(server, value); } // Property Server Status Read Only diff --git a/Sharp7/MsgSocket.cs b/Sharp7/MsgSocket.cs index ddefa8d..698a122 100644 --- a/Sharp7/MsgSocket.cs +++ b/Sharp7/MsgSocket.cs @@ -160,48 +160,23 @@ namespace Sharp7 return LastError; } - public bool Connected - { - get - { - return (TCPSocket != null) && (TCPSocket.Connected); - } - } + public bool Connected => (TCPSocket != null) && (TCPSocket.Connected); public int ReadTimeout { - get - { - return _ReadTimeout; - } - set - { - _ReadTimeout = value; - } + get => _ReadTimeout; + set => _ReadTimeout = value; } public int WriteTimeout { - get - { - return _WriteTimeout; - } - set - { - _WriteTimeout = value; - } - + get => _WriteTimeout; + set => _WriteTimeout = value; } public int ConnectTimeout { - get - { - return _ConnectTimeout; - } - set - { - _ConnectTimeout = value; - } + get => _ConnectTimeout; + set => _ConnectTimeout = value; } } } diff --git a/Sharp7/S7Client.cs b/Sharp7/S7Client.cs index be06749..542864d 100644 --- a/Sharp7/S7Client.cs +++ b/Sharp7/S7Client.cs @@ -2235,28 +2235,13 @@ namespace Sharp7 return Time_ms; } - public int ExecutionTime - { - get - { - return Time_ms; - } - } + public int ExecutionTime => Time_ms; - public int PduSizeNegotiated - { - get - { - return _PDULength; - } - } + public int PduSizeNegotiated => _PDULength; public int PduSizeRequested { - get - { - return _PduSizeRequested; - } + get => _PduSizeRequested; set { if (value < MinPduSizeToRequest) @@ -2269,60 +2254,29 @@ namespace Sharp7 public int PLCPort { - get - { - return _PLCPort; - } - set - { - _PLCPort = value; - } + get => _PLCPort; + set => _PLCPort = value; } public int ConnTimeout { - get - { - return Socket.ConnectTimeout; - } - set - { - Socket.ConnectTimeout = value; - } + get => Socket.ConnectTimeout; + set => Socket.ConnectTimeout = value; } public int RecvTimeout { - get - { - return Socket.ReadTimeout; - } - set - { - Socket.ReadTimeout = value; - } + get => Socket.ReadTimeout; + set => Socket.ReadTimeout = value; } public int SendTimeout { - get - { - return Socket.WriteTimeout; - } - set - { - Socket.WriteTimeout = value; - } - } - - public bool Connected - { - get - { - return (Socket != null) && (Socket.Connected); - } + get => Socket.WriteTimeout; + set => Socket.WriteTimeout = value; } + public bool Connected => (Socket != null) && (Socket.Connected); #endregion } diff --git a/Sharp7/S7Timer.cs b/Sharp7/S7Timer.cs index ef98df6..ac63bde 100644 --- a/Sharp7/S7Timer.cs +++ b/Sharp7/S7Timer.cs @@ -51,34 +51,14 @@ namespace Sharp7 this.q = (buff[8] & 0x02) == 0x02; } } - public TimeSpan PT - { - get - { - return pt; - } - } - public TimeSpan ET - { - get - { - return et; - } - } - public bool IN - { - get - { - return input; - } - } - public bool Q - { - get - { - return q; - } - } + public TimeSpan PT => pt; + + public TimeSpan ET => et; + + public bool IN => input; + + public bool Q => q; + #endregion } } \ No newline at end of file From edb3e9d1a6ef247ee6c71a2d73a30fb8afc30fcf Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 12:12:47 +0200 Subject: [PATCH 04/11] changed to extension method calls --- Sharp7/S7.cs | 2 +- Sharp7/S7Client.cs | 148 +++++++++++++++++++++---------------------- Sharp7/S7MultiVar.cs | 2 +- 3 files changed, 76 insertions(+), 76 deletions(-) diff --git a/Sharp7/S7.cs b/Sharp7/S7.cs index 8fc2867..0f30989 100644 --- a/Sharp7/S7.cs +++ b/Sharp7/S7.cs @@ -434,7 +434,7 @@ namespace Sharp7 { try { - return new DateTime(0).AddMilliseconds(S7.GetDIntAt(buffer, pos)); + return new DateTime(0).AddMilliseconds(buffer.GetDIntAt(pos)); } catch (System.ArgumentOutOfRangeException) { diff --git a/Sharp7/S7Client.cs b/Sharp7/S7Client.cs index 542864d..16e1deb 100644 --- a/Sharp7/S7Client.cs +++ b/Sharp7/S7Client.cs @@ -551,7 +551,7 @@ namespace Sharp7 RecvPacket(PDU, 0, 4); if (_LastError == 0) { - Size = S7.GetWordAt(PDU, 2); + Size = PDU.GetWordAt(2); // Check 0 bytes Data Packet (only TPKT+COTP = 7 bytes) if (Size == IsoHSize) RecvPacket(PDU, 4, 3); // Skip remaining 3 bytes and Done is still false @@ -612,7 +612,7 @@ namespace Sharp7 { int Length; // Set PDU Size Requested - S7.SetWordAt(S7_PN, 23, (ushort)_PduSizeRequested); + S7_PN.SetWordAt(23, (ushort)_PduSizeRequested); // Sends the connection request telegram SendPacket(S7_PN); if (_LastError == 0) @@ -624,7 +624,7 @@ namespace Sharp7 if ((Length == 27) && (PDU[17] == 0) && (PDU[18] == 0)) // 20 = size of Negotiate Answer { // Get PDU Size Negotiated - _PDULength = S7.GetWordAt(PDU, 25); + _PDULength = PDU.GetWordAt(25); if (_PDULength <= 0) _LastError = S7Consts.errCliNegotiatingPDU; } @@ -846,7 +846,7 @@ namespace Sharp7 WordLen = S7Consts.S7WLTimer; // Calc Word size - WordSize = S7.DataSizeByte(WordLen); + WordSize = WordLen.DataSizeByte(); if (WordSize == 0) return S7Consts.errCliInvalidWordLen; @@ -879,7 +879,7 @@ namespace Sharp7 PDU[27] = (byte)Area; // Set Area if (Area == S7Consts.S7AreaDB) - S7.SetWordAt(PDU, 25, (ushort)DBNumber); + PDU.SetWordAt(25, (ushort)DBNumber); // Adjusts Start and word length if ((WordLen == S7Consts.S7WLBit) || (WordLen == S7Consts.S7WLCounter) || (WordLen == S7Consts.S7WLTimer)) @@ -891,7 +891,7 @@ namespace Sharp7 Address = Start << 3; // Num elements - S7.SetWordAt(PDU, 23, (ushort)NumElements); + PDU.SetWordAt(23, (ushort)NumElements); // Address into the PLC (only 3 bytes) PDU[30] = (byte)(Address & 0x0FF); @@ -962,7 +962,7 @@ namespace Sharp7 WordLen = S7Consts.S7WLTimer; // Calc Word size - WordSize = S7.DataSizeByte(WordLen); + WordSize = WordLen.DataSizeByte(); if (WordSize == 0) return S7Consts.errCliInvalidWordLen; @@ -993,16 +993,16 @@ namespace Sharp7 // Setup the telegram Array.Copy(S7_RW, 0, PDU, 0, Size_WR); // Whole telegram Size - S7.SetWordAt(PDU, 2, (ushort)IsoSize); + PDU.SetWordAt(2, (ushort)IsoSize); // Data Length Length = DataSize + 4; - S7.SetWordAt(PDU, 15, (ushort)Length); + PDU.SetWordAt(15, (ushort)Length); // Function PDU[17] = (byte)0x05; // Set DB Number PDU[27] = (byte)Area; if (Area == S7Consts.S7AreaDB) - S7.SetWordAt(PDU, 25, (ushort)DBNumber); + PDU.SetWordAt(25, (ushort)DBNumber); // Adjusts Start and word length @@ -1019,7 +1019,7 @@ namespace Sharp7 } // Num elements - S7.SetWordAt(PDU, 23, (ushort)NumElements); + PDU.SetWordAt(23, (ushort)NumElements); // Address into the PLC PDU[30] = (byte)(Address & 0x0FF); Address = Address >> 8; @@ -1042,7 +1042,7 @@ namespace Sharp7 break; }; // Length - S7.SetWordAt(PDU, 33, (ushort)Length); + PDU.SetWordAt(33, (ushort)Length); // Copies the Data Array.Copy(Buffer, Offset, PDU, 35, DataSize); @@ -1096,7 +1096,7 @@ namespace Sharp7 // Fills Header Array.Copy(S7_MRD_HEADER, 0, PDU, 0, S7_MRD_HEADER.Length); - S7.SetWordAt(PDU, 13, (ushort)(ItemsCount * S7Item.Length + 2)); + PDU.SetWordAt(13, (ushort)(ItemsCount * S7Item.Length + 2)); PDU[18] = (byte)ItemsCount; // Fills the Items Offset = 19; @@ -1104,9 +1104,9 @@ namespace Sharp7 { Array.Copy(S7_MRD_ITEM, S7Item, S7Item.Length); S7Item[3] = (byte)Items[c].WordLen; - S7.SetWordAt(S7Item, 4, (ushort)Items[c].Amount); + S7Item.SetWordAt(4, (ushort)Items[c].Amount); if (Items[c].Area == S7Consts.S7AreaDB) - S7.SetWordAt(S7Item, 6, (ushort)Items[c].DBNumber); + S7Item.SetWordAt(6, (ushort)Items[c].DBNumber); S7Item[8] = (byte)Items[c].Area; // Address into the PLC @@ -1124,7 +1124,7 @@ namespace Sharp7 if (Offset > _PDULength) return S7Consts.errCliSizeOverPDU; - S7.SetWordAt(PDU, 2, (ushort)Offset); // Whole size + PDU.SetWordAt(2, (ushort)Offset); // Whole size SendPacket(PDU, Offset); if (_LastError != 0) @@ -1140,11 +1140,11 @@ namespace Sharp7 return _LastError; } // Check Global Operation Result - _LastError = CpuError(S7.GetWordAt(PDU, 17)); + _LastError = CpuError(PDU.GetWordAt(17)); if (_LastError != 0) return _LastError; // Get true ItemsCount - int ItemsRead = S7.GetByteAt(PDU, 20); + int ItemsRead = PDU.GetByteAt(20); if ((ItemsRead != ItemsCount) || (ItemsRead>MaxVars)) { _LastError = S7Consts.errCliInvalidPlcAnswer; @@ -1158,7 +1158,7 @@ namespace Sharp7 Array.Copy(PDU, Offset, S7ItemRead, 0, Length-Offset); if (S7ItemRead[0] == 0xff) { - ItemSize = (int)S7.GetWordAt(S7ItemRead, 2); + ItemSize = (int)S7ItemRead.GetWordAt(2); if ((S7ItemRead[1] != TS_ResOctet) && (S7ItemRead[1] != TS_ResReal) && (S7ItemRead[1] != TS_ResBit)) ItemSize = ItemSize >> 3; Marshal.Copy(S7ItemRead, 4, Items[c].pData, ItemSize); @@ -1196,7 +1196,7 @@ namespace Sharp7 // Fills Header Array.Copy(S7_MWR_HEADER, 0, PDU, 0, S7_MWR_HEADER.Length); ParLength = ItemsCount * S7_MWR_PARAM.Length + 2; - S7.SetWordAt(PDU, 13, (ushort)ParLength); + PDU.SetWordAt(13, (ushort)ParLength); PDU[18] = (byte)ItemsCount; // Fills Params Offset = S7_MWR_HEADER.Length; @@ -1205,8 +1205,8 @@ namespace Sharp7 Array.Copy(S7_MWR_PARAM, 0, S7ParItem, 0, S7_MWR_PARAM.Length); S7ParItem[3] = (byte)Items[c].WordLen; S7ParItem[8] = (byte)Items[c].Area; - S7.SetWordAt(S7ParItem, 4, (ushort)Items[c].Amount); - S7.SetWordAt(S7ParItem, 6, (ushort)Items[c].DBNumber); + S7ParItem.SetWordAt(4, (ushort)Items[c].Amount); + S7ParItem.SetWordAt(6, (ushort)Items[c].DBNumber); // Address into the PLC int Address = Items[c].Start; S7ParItem[11] = (byte)(Address & 0x0FF); @@ -1241,9 +1241,9 @@ namespace Sharp7 ItemDataSize = Items[c].Amount; if ((S7DataItem[1] != TS_ResOctet) && (S7DataItem[1] != TS_ResBit)) - S7.SetWordAt(S7DataItem, 2, (ushort)(ItemDataSize*8)); + S7DataItem.SetWordAt(2, (ushort)(ItemDataSize*8)); else - S7.SetWordAt(S7DataItem, 2, (ushort)ItemDataSize); + S7DataItem.SetWordAt(2, (ushort)ItemDataSize); Marshal.Copy(Items[c].pData, S7DataItem, 4, ItemDataSize); if (ItemDataSize % 2 != 0) @@ -1260,19 +1260,19 @@ namespace Sharp7 if (Offset > _PDULength) return S7Consts.errCliSizeOverPDU; - S7.SetWordAt(PDU, 2, (ushort)Offset); // Whole size - S7.SetWordAt(PDU, 15, (ushort)DataLength); // Whole size + PDU.SetWordAt(2, (ushort)Offset); // Whole size + PDU.SetWordAt(15, (ushort)DataLength); // Whole size SendPacket(PDU, Offset); RecvIsoPacket(); if (_LastError==0) { // Check Global Operation Result - _LastError = CpuError(S7.GetWordAt(PDU, 17)); + _LastError = CpuError(PDU.GetWordAt(17)); if (_LastError != 0) return _LastError; // Get true ItemsCount - int ItemsWritten = S7.GetByteAt(PDU, 20); + int ItemsWritten = PDU.GetByteAt(20); if ((ItemsWritten != ItemsCount) || (ItemsWritten > MaxVars)) { _LastError = S7Consts.errCliInvalidPlcAnswer; @@ -1429,24 +1429,24 @@ namespace Sharp7 int Length = RecvIsoPacket(); if (Length > 32) // the minimum expected { - ushort Result = S7.GetWordAt(PDU, 27); + ushort Result = PDU.GetWordAt(27); if (Result == 0) { Info.BlkFlags= PDU[42]; Info.BlkLang = PDU[43]; Info.BlkType = PDU[44]; - Info.BlkNumber = S7.GetWordAt(PDU, 45); - Info.LoadSize = S7.GetDIntAt(PDU, 47); - Info.CodeDate = SiemensTimestamp(S7.GetWordAt(PDU, 59)); - Info.IntfDate = SiemensTimestamp(S7.GetWordAt(PDU, 65)); - Info.SBBLength = S7.GetWordAt(PDU, 67); - Info.LocalData = S7.GetWordAt(PDU, 71); - Info.MC7Size = S7.GetWordAt(PDU, 73); - Info.Author = S7.GetCharsAt(PDU, 75, 8).Trim(new char[]{(char)0}); - Info.Family = S7.GetCharsAt(PDU, 83, 8).Trim(new char[]{(char)0}); - Info.Header = S7.GetCharsAt(PDU, 91, 8).Trim(new char[]{(char)0}); + Info.BlkNumber = PDU.GetWordAt(45); + Info.LoadSize = PDU.GetDIntAt(47); + Info.CodeDate = SiemensTimestamp(PDU.GetWordAt(59)); + Info.IntfDate = SiemensTimestamp(PDU.GetWordAt(65)); + Info.SBBLength = PDU.GetWordAt(67); + Info.LocalData = PDU.GetWordAt(71); + Info.MC7Size = PDU.GetWordAt(73); + Info.Author = PDU.GetCharsAt(75, 8).Trim(new char[]{(char)0}); + Info.Family = PDU.GetCharsAt(83, 8).Trim(new char[]{(char)0}); + Info.Header = PDU.GetCharsAt(91, 8).Trim(new char[]{(char)0}); Info.Version = PDU[99]; - Info.CheckSum = S7.GetWordAt(PDU, 101); + Info.CheckSum = PDU.GetWordAt(101); } else _LastError = CpuError(Result); @@ -1558,9 +1558,9 @@ namespace Sharp7 Length = RecvIsoPacket(); if (Length > 30) // the minimum expected { - if ((S7.GetWordAt(PDU, 27) == 0) && (PDU[29] == 0xFF)) + if ((PDU.GetWordAt(27) == 0) && (PDU[29] == 0xFF)) { - DT = S7.GetDateTimeAt(PDU, 35); + DT = PDU.GetDateTimeAt(35); } else _LastError = S7Consts.errCliInvalidPlcAnswer; @@ -1582,14 +1582,14 @@ namespace Sharp7 Time_ms = 0; int Elapsed = Environment.TickCount; - S7.SetDateTimeAt(S7_SET_DT, 31, DT); + S7_SET_DT.SetDateTimeAt(31, DT); SendPacket(S7_SET_DT); if (_LastError == 0) { Length = RecvIsoPacket(); if (Length > 30) // the minimum expected { - if (S7.GetWordAt(PDU, 27) != 0) + if (PDU.GetWordAt(27) != 0) _LastError = S7Consts.errCliInvalidPlcAnswer; } else @@ -1619,7 +1619,7 @@ namespace Sharp7 _LastError = ReadSZL(0x0011, 0x000, ref SZL, ref Size); if (_LastError == 0) { - Info.Code = S7.GetCharsAt(SZL.Data, 2, 20); + Info.Code = SZL.Data.GetCharsAt(2, 20); Info.V1 = SZL.Data[Size - 3]; Info.V2 = SZL.Data[Size - 2]; Info.V3 = SZL.Data[Size - 1]; @@ -1638,11 +1638,11 @@ namespace Sharp7 _LastError = ReadSZL(0x001C, 0x000, ref SZL, ref Size); if (_LastError == 0) { - Info.ModuleTypeName = S7.GetCharsAt(SZL.Data, 172, 32); - Info.SerialNumber = S7.GetCharsAt(SZL.Data, 138, 24); - Info.ASName = S7.GetCharsAt(SZL.Data, 2, 24); - Info.Copyright = S7.GetCharsAt(SZL.Data, 104, 26); - Info.ModuleName = S7.GetCharsAt(SZL.Data, 36, 24); + Info.ModuleTypeName = SZL.Data.GetCharsAt(172, 32); + Info.SerialNumber = SZL.Data.GetCharsAt(138, 24); + Info.ASName = SZL.Data.GetCharsAt(2, 24); + Info.Copyright = SZL.Data.GetCharsAt(104, 26); + Info.ModuleName = SZL.Data.GetCharsAt(36, 24); } if (_LastError == 0) Time_ms = Environment.TickCount - Elapsed; @@ -1658,10 +1658,10 @@ namespace Sharp7 _LastError = ReadSZL(0x0131, 0x001, ref SZL, ref Size); if (_LastError == 0) { - Info.MaxPduLength = S7.GetIntAt(PDU, 2); - Info.MaxConnections = S7.GetIntAt(PDU, 4); - Info.MaxMpiRate = S7.GetDIntAt(PDU, 6); - Info.MaxBusRate = S7.GetDIntAt(PDU, 10); + Info.MaxPduLength = PDU.GetIntAt(2); + Info.MaxConnections = PDU.GetIntAt(4); + Info.MaxMpiRate = PDU.GetDIntAt(6); + Info.MaxBusRate = PDU.GetDIntAt(10); } if (_LastError == 0) Time_ms = Environment.TickCount - Elapsed; @@ -1687,14 +1687,14 @@ namespace Sharp7 { if (First) { - S7.SetWordAt(S7_SZL_FIRST, 11, ++Seq_out); - S7.SetWordAt(S7_SZL_FIRST, 29, (ushort)ID); - S7.SetWordAt(S7_SZL_FIRST, 31, (ushort)Index); + S7_SZL_FIRST.SetWordAt(11, ++Seq_out); + S7_SZL_FIRST.SetWordAt(29, (ushort)ID); + S7_SZL_FIRST.SetWordAt(31, (ushort)Index); SendPacket(S7_SZL_FIRST); } else { - S7.SetWordAt(S7_SZL_NEXT, 11, ++Seq_out); + S7_SZL_NEXT.SetWordAt(11, ++Seq_out); PDU[24] = (byte)Seq_in; SendPacket(S7_SZL_NEXT); } @@ -1708,14 +1708,14 @@ namespace Sharp7 { if (Length > 32) // the minimum expected { - if ((S7.GetWordAt(PDU, 27) == 0) && (PDU[29] == (byte)0xFF)) + if ((PDU.GetWordAt(27) == 0) && (PDU[29] == (byte)0xFF)) { // Gets Amount of this slice - DataSZL = S7.GetWordAt(PDU, 31) - 8; // Skips extra params (ID, Index ...) + DataSZL = PDU.GetWordAt(31) - 8; // Skips extra params (ID, Index ...) Done = PDU[26] == 0x00; Seq_in = (byte)PDU[24]; // Slice sequence - SZL.Header.LENTHDR = S7.GetWordAt(PDU, 37); - SZL.Header.N_DR = S7.GetWordAt(PDU, 39); + SZL.Header.LENTHDR = PDU.GetWordAt(37); + SZL.Header.N_DR = PDU.GetWordAt(39); Array.Copy(PDU, 41, SZL.Data, Offset, DataSZL); // SZL.Copy(PDU, 41, Offset, DataSZL); Offset += DataSZL; @@ -1731,10 +1731,10 @@ namespace Sharp7 { if (Length > 32) // the minimum expected { - if ((S7.GetWordAt(PDU, 27) == 0) && (PDU[29] == (byte)0xFF)) + if ((PDU.GetWordAt(27) == 0) && (PDU[29] == (byte)0xFF)) { // Gets Amount of this slice - DataSZL = S7.GetWordAt(PDU, 31); + DataSZL = PDU.GetWordAt(31); Done = PDU[26] == 0x00; Seq_in = (byte)PDU[24]; // Slice sequence Array.Copy(PDU, 37, SZL.Data, Offset, DataSZL); @@ -1876,7 +1876,7 @@ namespace Sharp7 int Length = RecvIsoPacket(); if (Length > 30) // the minimum expected { - ushort Result = S7.GetWordAt(PDU, 27); + ushort Result = PDU.GetWordAt(27); if (Result == 0) { switch (PDU[44]) @@ -1918,7 +1918,7 @@ namespace Sharp7 _LastError = 0; int Elapsed = Environment.TickCount; // Encodes the Password - S7.SetCharsAt(pwd, 0, Password); + pwd.SetCharsAt(0, Password); pwd[0] = (byte)(pwd[0] ^ 0x55); pwd[1] = (byte)(pwd[1] ^ 0x55); for (int c = 2; c < 8; c++) @@ -1933,7 +1933,7 @@ namespace Sharp7 Length = RecvIsoPacket(); if (Length > 32) // the minimum expected { - ushort Result = S7.GetWordAt(PDU, 27); + ushort Result = PDU.GetWordAt(27); if (Result != 0) _LastError = CpuError(Result); } @@ -1956,7 +1956,7 @@ namespace Sharp7 Length = RecvIsoPacket(); if (Length > 30) // the minimum expected { - ushort Result = S7.GetWordAt(PDU, 27); + ushort Result = PDU.GetWordAt(27); if (Result != 0) _LastError = CpuError(Result); } @@ -1974,11 +1974,11 @@ namespace Sharp7 _LastError = ReadSZL(0x0232, 0x0004, ref SZL, ref Size); if (_LastError == 0) { - Protection.sch_schal = S7.GetWordAt(SZL.Data, 2); - Protection.sch_par = S7.GetWordAt(SZL.Data, 4); - Protection.sch_rel = S7.GetWordAt(SZL.Data, 6); - Protection.bart_sch = S7.GetWordAt(SZL.Data, 8); - Protection.anl_sch = S7.GetWordAt(SZL.Data, 10); + Protection.sch_schal = SZL.Data.GetWordAt(2); + Protection.sch_par = SZL.Data.GetWordAt(4); + Protection.sch_rel = SZL.Data.GetWordAt(6); + Protection.bart_sch = SZL.Data.GetWordAt(8); + Protection.anl_sch = SZL.Data.GetWordAt(10); } return _LastError; } @@ -1992,7 +1992,7 @@ namespace Sharp7 Time_ms = 0; int Elapsed = Environment.TickCount; Array.Copy(TPKT_ISO, 0, PDU, 0, TPKT_ISO.Length); - S7.SetWordAt(PDU, 2, (ushort)(Size + TPKT_ISO.Length)); + PDU.SetWordAt(2, (ushort)(Size + TPKT_ISO.Length)); try { Array.Copy(Buffer, 0, PDU, TPKT_ISO.Length, Size); diff --git a/Sharp7/S7MultiVar.cs b/Sharp7/S7MultiVar.cs index 08f8e4b..d0c804d 100644 --- a/Sharp7/S7MultiVar.cs +++ b/Sharp7/S7MultiVar.cs @@ -18,7 +18,7 @@ namespace Sharp7 private bool AdjustWordLength(int Area, ref int WordLen, ref int Amount, ref int Start) { // Calc Word size - int WordSize = S7.DataSizeByte(WordLen); + int WordSize = WordLen.DataSizeByte(); if (WordSize == 0) return false; From aa992fe51b6a216c9b7aeae8d7cf2ac02dd468af Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 12:15:50 +0200 Subject: [PATCH 05/11] updated readme --- README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 77b062e..574347f 100644 --- a/README.md +++ b/README.md @@ -15,28 +15,27 @@ or read the [Wiki](https://github.com/fbarresi/Sharp7/wiki). # How to install -## Package Manager +## Package Manager or dotnet CLI ``` PM> Install-Package Sharp7 ``` - -## .NET CLI +or ``` > dotnet add package Sharp7 ``` -## Do you need more power? +# Do you need more power? Try [Sharp7Reactive](https://github.com/evopro-ag/Sharp7Reactive) -## Get Started +# Get Started -### Supported Targets +## Supported Targets - S7 300/400/WinAC CPU (fully supported) - S7 1200/1500 CPU - CP (Communication processor - 343/443/IE) -### S7 1200/1500 Notes +## S7 1200/1500 Notes An external equipment can access to S71200/1500 CPU using the S7 'base' protocol, only working as an HMI, i.e. only basic data transfer are allowed. From 470f736079226683395fda432c19572ddcd33137 Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 17:19:30 +0200 Subject: [PATCH 06/11] added enums and methods overloads for S7Wordlength and S7Area fix #11 --- Sharp7/S7.cs | 1 + Sharp7/S7Area.cs | 12 ++++++++++++ Sharp7/S7Client.cs | 20 ++++++++++++++++++++ Sharp7/S7Consts.cs | 32 ++++++++++++++++---------------- Sharp7/S7MultiVar.cs | 1 + Sharp7/S7WordLength.cs | 16 ++++++++++++++++ 6 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 Sharp7/S7Area.cs create mode 100644 Sharp7/S7WordLength.cs diff --git a/Sharp7/S7.cs b/Sharp7/S7.cs index 0f30989..dce8a82 100644 --- a/Sharp7/S7.cs +++ b/Sharp7/S7.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +#pragma warning disable 618 namespace Sharp7 { diff --git a/Sharp7/S7Area.cs b/Sharp7/S7Area.cs new file mode 100644 index 0000000..c27b93d --- /dev/null +++ b/Sharp7/S7Area.cs @@ -0,0 +1,12 @@ +namespace Sharp7 +{ + public enum S7Area + { + PE = 0x81, + PA = 0x82, + MK = 0x83, + DB = 0x84, + CT = 0x1C, + TM = 0x1D, + } +} \ No newline at end of file diff --git a/Sharp7/S7Client.cs b/Sharp7/S7Client.cs index 16e1deb..9215011 100644 --- a/Sharp7/S7Client.cs +++ b/Sharp7/S7Client.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Runtime.InteropServices; +#pragma warning disable 618 namespace Sharp7 { @@ -819,6 +820,15 @@ namespace Sharp7 #region [Data I/O main functions] + public int ReadArea(S7Area Area, int DBNumber, int Start, int Amount, S7WordLength WordLen, byte[] Buffer) + { + return ReadArea((int)Area, DBNumber, Start, Amount, (int)WordLen, Buffer); + } + + public int ReadArea(S7Area Area, int DBNumber, int Start, int Amount, S7WordLength WordLen, byte[] Buffer, ref int BytesRead) + { + return ReadArea((int)Area, DBNumber, Start, Amount, (int)WordLen, Buffer, ref BytesRead); + } public int ReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer) { int BytesRead = 0; @@ -934,6 +944,16 @@ namespace Sharp7 return _LastError; } + public int WriteArea(S7Area Area, int DBNumber, int Start, int Amount, S7WordLength WordLen, byte[] Buffer) + { + int BytesWritten = 0; + return WriteArea((int) Area, DBNumber, Start, Amount, (int) WordLen, Buffer, ref BytesWritten); + } + + public int WriteArea(S7Area Area, int DBNumber, int Start, int Amount, S7WordLength WordLen, byte[] Buffer, ref int BytesWritten) + { + return WriteArea((int) Area, DBNumber, Start, Amount, (int) WordLen, Buffer, ref BytesWritten); + } public int WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer) { int BytesWritten = 0; diff --git a/Sharp7/S7Consts.cs b/Sharp7/S7Consts.cs index 6116bc2..0a4a5b5 100644 --- a/Sharp7/S7Consts.cs +++ b/Sharp7/S7Consts.cs @@ -81,23 +81,23 @@ namespace Sharp7 public const Int32 p_u32_RecoveryTime = 14; // Not applicable here public const Int32 p_u32_KeepAliveTime = 15; // Not applicable here // Area ID - public const byte S7AreaPE = 0x81; - public const byte S7AreaPA = 0x82; - public const byte S7AreaMK = 0x83; - public const byte S7AreaDB = 0x84; - public const byte S7AreaCT = 0x1C; - public const byte S7AreaTM = 0x1D; + [Obsolete("Use enum S7Area.PE instead")]public const byte S7AreaPE = 0x81; + [Obsolete("Use enum S7Area.PA instead")]public const byte S7AreaPA = 0x82; + [Obsolete("Use enum S7Area.MK instead")]public const byte S7AreaMK = 0x83; + [Obsolete("Use enum S7Area.DB instead")]public const byte S7AreaDB = 0x84; + [Obsolete("Use enum S7Area.CT instead")]public const byte S7AreaCT = 0x1C; + [Obsolete("Use enum S7Area.TM instead")]public const byte S7AreaTM = 0x1D; // Word Length - public const int S7WLBit = 0x01; - public const int S7WLByte = 0x02; - public const int S7WLChar = 0x03; - public const int S7WLWord = 0x04; - public const int S7WLInt = 0x05; - public const int S7WLDWord = 0x06; - public const int S7WLDInt = 0x07; - public const int S7WLReal = 0x08; - public const int S7WLCounter = 0x1C; - public const int S7WLTimer = 0x1D; + [Obsolete("Use enum S7WordLength.Bit instead")]public const int S7WLBit = 0x01; + [Obsolete("Use enum S7WordLength.Byte instead")]public const int S7WLByte = 0x02; + [Obsolete("Use enum S7WordLength.Char instead")]public const int S7WLChar = 0x03; + [Obsolete("Use enum S7WordLength.Word instead")]public const int S7WLWord = 0x04; + [Obsolete("Use enum S7WordLength.Int instead")]public const int S7WLInt = 0x05; + [Obsolete("Use enum S7WordLength.DWord instead")]public const int S7WLDWord = 0x06; + [Obsolete("Use enum S7WordLength.DInt instead")]public const int S7WLDInt = 0x07; + [Obsolete("Use enum S7WordLength.Real instead")]public const int S7WLReal = 0x08; + [Obsolete("Use enum S7WordLength.Counter instead")]public const int S7WLCounter = 0x1C; + [Obsolete("Use enum S7WordLength.Timer instead")]public const int S7WLTimer = 0x1D; // PLC Status public const int S7CpuStatusUnknown = 0x00; public const int S7CpuStatusRun = 0x08; diff --git a/Sharp7/S7MultiVar.cs b/Sharp7/S7MultiVar.cs index d0c804d..717ee61 100644 --- a/Sharp7/S7MultiVar.cs +++ b/Sharp7/S7MultiVar.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Runtime.InteropServices; +#pragma warning disable 618 namespace Sharp7 { diff --git a/Sharp7/S7WordLength.cs b/Sharp7/S7WordLength.cs new file mode 100644 index 0000000..ed9b6da --- /dev/null +++ b/Sharp7/S7WordLength.cs @@ -0,0 +1,16 @@ +namespace Sharp7 +{ + public enum S7WordLength + { + Bit = 0x01, + Byte = 0x02, + Char = 0x03, + Word = 0x04, + Int = 0x05, + DWord = 0x06, + DInt = 0x07, + Real = 0x08, + Counter = 0x1C, + Timer = 0x1D, + } +} \ No newline at end of file From 602a8d4f363e03836e47b70ddaf8937dde7ef511 Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 17:34:57 +0200 Subject: [PATCH 07/11] used new enums in tests --- Sharp7.Tests/ClientTest.cs | 4 ++-- Sharp7.Tests/ClientWithoutServer.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sharp7.Tests/ClientTest.cs b/Sharp7.Tests/ClientTest.cs index 55cca48..8d8be98 100644 --- a/Sharp7.Tests/ClientTest.cs +++ b/Sharp7.Tests/ClientTest.cs @@ -177,10 +177,10 @@ namespace Sharp7.Tests var multivar = new S7MultiVar(Client); multivar.ShouldNotBeNull(); - multivar.Add(new Sharp7.S7Consts.S7Tag(){Area = Sharp7.S7Consts.S7AreaDB, DBNumber = index, Elements = 2,Start = 0, WordLen = 2}, ref buffer).ShouldBe(true); + multivar.Add(new Sharp7.S7Consts.S7Tag(){Area = (int)S7Area.DB, DBNumber = index, Elements = 2,Start = 0, WordLen = 2}, ref buffer).ShouldBe(true); multivar.Read().ShouldBe(0); - multivar.Add(new Sharp7.S7Consts.S7Tag() { Area = Sharp7.S7Consts.S7AreaDB, DBNumber = index, Elements = 2, Start = 0, WordLen = 2 }, ref buffer).ShouldBe(true); + multivar.Add(new Sharp7.S7Consts.S7Tag() { Area = (int)S7Area.DB, DBNumber = index, Elements = 2, Start = 0, WordLen = 2 }, ref buffer).ShouldBe(true); multivar.Write().ShouldBe(0); diff --git a/Sharp7.Tests/ClientWithoutServer.cs b/Sharp7.Tests/ClientWithoutServer.cs index aa40b85..a34d2e7 100644 --- a/Sharp7.Tests/ClientWithoutServer.cs +++ b/Sharp7.Tests/ClientWithoutServer.cs @@ -14,7 +14,7 @@ namespace Sharp7.Tests client = new S7Client(); } - public new void Dispose() + public void Dispose() { client.Disconnect(); } From c33a838144810201553c47f3b1b6083e8e0d4b45 Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 17:40:57 +0200 Subject: [PATCH 08/11] added null propagation on disconnect method --- Sharp7/S7Client.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Sharp7/S7Client.cs b/Sharp7/S7Client.cs index 9215011..45a26ae 100644 --- a/Sharp7/S7Client.cs +++ b/Sharp7/S7Client.cs @@ -724,10 +724,7 @@ namespace Sharp7 public int Disconnect() { - if (Socket != null && Socket.Connected) - { - Socket.Close(); - } + Socket?.Close(); return 0; } From de795802acede2fb1cc41b6a85d0000f78b4c9bd Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 22:07:42 +0200 Subject: [PATCH 09/11] added usage of overloaded methods with enums instead of constants --- Sharp7/S7Client.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Sharp7/S7Client.cs b/Sharp7/S7Client.cs index 45a26ae..0b312ee 100644 --- a/Sharp7/S7Client.cs +++ b/Sharp7/S7Client.cs @@ -1314,48 +1314,48 @@ namespace Sharp7 public int DBRead(int DBNumber, int Start, int Size, byte[] Buffer) { - return ReadArea(S7Consts.S7AreaDB, DBNumber, Start, Size, S7Consts.S7WLByte, Buffer); + return ReadArea(S7Area.DB, DBNumber, Start, Size, S7WordLength.Byte, Buffer); } public int DBWrite(int DBNumber, int Start, int Size, byte[] Buffer) { - return WriteArea(S7Consts.S7AreaDB, DBNumber, Start, Size, S7Consts.S7WLByte, Buffer); + return WriteArea(S7Area.DB, DBNumber, Start, Size, S7WordLength.Byte, Buffer); } public int MBRead(int Start, int Size, byte[] Buffer) { - return ReadArea(S7Consts.S7AreaMK, 0, Start, Size, S7Consts.S7WLByte, Buffer); + return ReadArea(S7Area.MK, 0, Start, Size, S7WordLength.Byte, Buffer); } public int MBWrite(int Start, int Size, byte[] Buffer) { - return WriteArea(S7Consts.S7AreaMK, 0, Start, Size, S7Consts.S7WLByte, Buffer); + return WriteArea(S7Area.MK, 0, Start, Size, S7WordLength.Byte, Buffer); } public int EBRead(int Start, int Size, byte[] Buffer) { - return ReadArea(S7Consts.S7AreaPE, 0, Start, Size, S7Consts.S7WLByte, Buffer); + return ReadArea(S7Area.PE, 0, Start, Size, S7WordLength.Byte, Buffer); } public int EBWrite(int Start, int Size, byte[] Buffer) { - return WriteArea(S7Consts.S7AreaPE, 0, Start, Size, S7Consts.S7WLByte, Buffer); + return WriteArea(S7Area.PE, 0, Start, Size, S7WordLength.Byte, Buffer); } public int ABRead(int Start, int Size, byte[] Buffer) { - return ReadArea(S7Consts.S7AreaPA, 0, Start, Size, S7Consts.S7WLByte, Buffer); + return ReadArea(S7Area.PA, 0, Start, Size, S7WordLength.Byte, Buffer); } public int ABWrite(int Start, int Size, byte[] Buffer) { - return WriteArea(S7Consts.S7AreaPA, 0, Start, Size, S7Consts.S7WLByte, Buffer); + return WriteArea(S7Area.PA, 0, Start, Size, S7WordLength.Byte, Buffer); } public int TMRead(int Start, int Amount, ushort[] Buffer) { byte[] sBuffer = new byte[Amount * 2]; - int Result = ReadArea(S7Consts.S7AreaTM, 0, Start, Amount, S7Consts.S7WLTimer, sBuffer); + int Result = ReadArea(S7Area.TM, 0, Start, Amount, S7WordLength.Timer, sBuffer); if (Result == 0) { for (int c = 0; c < Amount; c++) @@ -1374,13 +1374,13 @@ namespace Sharp7 sBuffer[c * 2 + 1] = (byte)((Buffer[c] & 0xFF00) >> 8); sBuffer[c * 2] = (byte)(Buffer[c] & 0x00FF); } - return WriteArea(S7Consts.S7AreaTM, 0, Start, Amount, S7Consts.S7WLTimer, sBuffer); + return WriteArea(S7Area.TM, 0, Start, Amount, S7WordLength.Timer, sBuffer); } public int CTRead(int Start, int Amount, ushort[] Buffer) { byte[] sBuffer = new byte[Amount * 2]; - int Result = ReadArea(S7Consts.S7AreaCT, 0, Start, Amount, S7Consts.S7WLCounter, sBuffer); + int Result = ReadArea(S7Area.CT, 0, Start, Amount, S7WordLength.Counter, sBuffer); if (Result==0) { for (int c=0; c>8); sBuffer[c * 2]= (byte)(Buffer[c] & 0x00FF); } - return WriteArea(S7Consts.S7AreaCT, 0, Start, Amount, S7Consts.S7WLCounter, sBuffer); + return WriteArea(S7Area.CT, 0, Start, Amount, S7WordLength.Counter, sBuffer); } #endregion From db933cf62bcd74bc1f11c54b8d0fb9240a699720 Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 22:11:28 +0200 Subject: [PATCH 10/11] solved all warnings for S7Client --- Sharp7/S7Client.cs | 59 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/Sharp7/S7Client.cs b/Sharp7/S7Client.cs index 0b312ee..6630b5b 100644 --- a/Sharp7/S7Client.cs +++ b/Sharp7/S7Client.cs @@ -1,7 +1,6 @@ using System; using System.Linq; using System.Runtime.InteropServices; -#pragma warning disable 618 namespace Sharp7 { @@ -241,7 +240,7 @@ namespace Sharp7 0x12, // Var spec. 0x0a, // Length of remaining bytes 0x10, // Syntax ID - (byte)S7Consts.S7WLByte, // Transport Size idx=22 + (byte)S7WordLength.Byte, // Transport Size idx=22 0x00,0x00, // Num Elements 0x00,0x00, // DB Number (if any, else 0) 0x84, // Area Type @@ -274,7 +273,7 @@ namespace Sharp7 0x12, // Var spec. 0x0a, // Length of remaining bytes 0x10, // Syntax ID - (byte)S7Consts.S7WLByte, // Transport Size idx=3 + (byte)S7WordLength.Byte, // Transport Size idx=3 0x00,0x00, // Num Elements 0x00,0x00, // DB Number (if any, else 0) 0x84, // Area Type @@ -301,7 +300,7 @@ namespace Sharp7 0x12, // Var spec. 0x0a, // Length of remaining bytes 0x10, // Syntax ID - (byte)S7Consts.S7WLByte, // Transport Size idx=3 + (byte)S7WordLength.Byte, // Transport Size idx=3 0x00,0x00, // Num Elements 0x00,0x00, // DB Number (if any, else 0) 0x84, // Area Type @@ -847,25 +846,25 @@ namespace Sharp7 Time_ms = 0; int Elapsed = Environment.TickCount; // Some adjustment - if (Area == S7Consts.S7AreaCT) - WordLen = S7Consts.S7WLCounter; - if (Area == S7Consts.S7AreaTM) - WordLen = S7Consts.S7WLTimer; + if (Area == (int)S7Area.CT) + WordLen = (int)S7WordLength.Counter; + if (Area == (int)S7Area.TM) + WordLen = (int)S7WordLength.Timer; // Calc Word size WordSize = WordLen.DataSizeByte(); if (WordSize == 0) return S7Consts.errCliInvalidWordLen; - if (WordLen == S7Consts.S7WLBit) + if (WordLen == (int)S7WordLength.Bit) Amount = 1; // Only 1 bit can be transferred at time else { - if ((WordLen != S7Consts.S7WLCounter) && (WordLen != S7Consts.S7WLTimer)) + if ((WordLen != (int)S7WordLength.Counter) && (WordLen != (int)S7WordLength.Timer)) { Amount = Amount * WordSize; WordSize = 1; - WordLen = S7Consts.S7WLByte; + WordLen = (int)S7WordLength.Byte; } } @@ -885,11 +884,11 @@ namespace Sharp7 // Set DB Number PDU[27] = (byte)Area; // Set Area - if (Area == S7Consts.S7AreaDB) + if (Area == (int)S7Area.DB) PDU.SetWordAt(25, (ushort)DBNumber); // Adjusts Start and word length - if ((WordLen == S7Consts.S7WLBit) || (WordLen == S7Consts.S7WLCounter) || (WordLen == S7Consts.S7WLTimer)) + if ((WordLen == (int)S7WordLength.Bit) || (WordLen == (int)S7WordLength.Counter) || (WordLen == (int)S7WordLength.Timer)) { Address = Start; PDU[22] = (byte)WordLen; @@ -973,25 +972,25 @@ namespace Sharp7 Time_ms = 0; int Elapsed = Environment.TickCount; // Some adjustment - if (Area == S7Consts.S7AreaCT) - WordLen = S7Consts.S7WLCounter; - if (Area == S7Consts.S7AreaTM) - WordLen = S7Consts.S7WLTimer; + if (Area == (int)S7Area.CT) + WordLen = (int)S7WordLength.Counter; + if (Area == (int)S7Area.TM) + WordLen = (int)S7WordLength.Timer; // Calc Word size WordSize = WordLen.DataSizeByte(); if (WordSize == 0) return S7Consts.errCliInvalidWordLen; - if (WordLen == S7Consts.S7WLBit) // Only 1 bit can be transferred at time + if (WordLen == (int)S7WordLength.Bit) // Only 1 bit can be transferred at time Amount = 1; else { - if ((WordLen != S7Consts.S7WLCounter) && (WordLen != S7Consts.S7WLTimer)) + if ((WordLen != (int)S7WordLength.Counter) && (WordLen != (int)S7WordLength.Timer)) { Amount = Amount * WordSize; WordSize = 1; - WordLen = S7Consts.S7WLByte; + WordLen = (int)S7WordLength.Byte; } } @@ -1018,12 +1017,12 @@ namespace Sharp7 PDU[17] = (byte)0x05; // Set DB Number PDU[27] = (byte)Area; - if (Area == S7Consts.S7AreaDB) + if (Area == (int)S7Area.DB) PDU.SetWordAt(25, (ushort)DBNumber); // Adjusts Start and word length - if ((WordLen == S7Consts.S7WLBit) || (WordLen == S7Consts.S7WLCounter) || (WordLen == S7Consts.S7WLTimer)) + if ((WordLen == (int)S7WordLength.Bit) || (WordLen == (int)S7WordLength.Counter) || (WordLen == (int)S7WordLength.Timer)) { Address = Start; Length = DataSize; @@ -1047,11 +1046,11 @@ namespace Sharp7 // Transport Size switch (WordLen) { - case S7Consts.S7WLBit: + case (int)S7WordLength.Bit: PDU[32] = TS_ResBit; break; - case S7Consts.S7WLCounter: - case S7Consts.S7WLTimer: + case (int)S7WordLength.Counter: + case (int)S7WordLength.Timer: PDU[32] = TS_ResOctet; break; default: @@ -1122,7 +1121,7 @@ namespace Sharp7 Array.Copy(S7_MRD_ITEM, S7Item, S7Item.Length); S7Item[3] = (byte)Items[c].WordLen; S7Item.SetWordAt(4, (ushort)Items[c].Amount); - if (Items[c].Area == S7Consts.S7AreaDB) + if (Items[c].Area == (int)S7Area.DB) S7Item.SetWordAt(6, (ushort)Items[c].DBNumber); S7Item[8] = (byte)Items[c].Area; @@ -1241,18 +1240,18 @@ namespace Sharp7 S7DataItem[0] = 0x00; switch (Items[c].WordLen) { - case S7Consts.S7WLBit: + case (int)S7WordLength.Bit: S7DataItem[1] = TS_ResBit; break; - case S7Consts.S7WLCounter: - case S7Consts.S7WLTimer: + case (int)S7WordLength.Counter: + case (int)S7WordLength.Timer: S7DataItem[1] = TS_ResOctet; break; default: S7DataItem[1] = TS_ResByte; // byte/word/dword etc. break; }; - if ((Items[c].WordLen==S7Consts.S7WLTimer) || (Items[c].WordLen == S7Consts.S7WLCounter)) + if ((Items[c].WordLen==(int)S7WordLength.Timer) || (Items[c].WordLen == (int)S7WordLength.Counter)) ItemDataSize = Items[c].Amount * 2; else ItemDataSize = Items[c].Amount; From 4aa0aa11d20fd1fcabd5783ec6d32254db049b29 Mon Sep 17 00:00:00 2001 From: Federico Barresi Date: Fri, 5 Jun 2020 22:15:15 +0200 Subject: [PATCH 11/11] solved all warnings replaces obsoleted constants solutionwide --- Sharp7/S7.cs | 21 ++++++++++----------- Sharp7/S7MultiVar.cs | 15 +++++++-------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Sharp7/S7.cs b/Sharp7/S7.cs index dce8a82..f774c2d 100644 --- a/Sharp7/S7.cs +++ b/Sharp7/S7.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Text; -#pragma warning disable 618 namespace Sharp7 { @@ -26,16 +25,16 @@ namespace Sharp7 { switch (wordLength) { - case S7Consts.S7WLBit: return 1; // S7 sends 1 byte per bit - case S7Consts.S7WLByte: return 1; - case S7Consts.S7WLChar: return 1; - case S7Consts.S7WLWord: return 2; - case S7Consts.S7WLDWord: return 4; - case S7Consts.S7WLInt: return 2; - case S7Consts.S7WLDInt: return 4; - case S7Consts.S7WLReal: return 4; - case S7Consts.S7WLCounter: return 2; - case S7Consts.S7WLTimer: return 2; + case (int)S7WordLength.Bit: return 1; // S7 sends 1 byte per bit + case (int)S7WordLength.Byte: return 1; + case (int)S7WordLength.Char: return 1; + case (int)S7WordLength.Word: return 2; + case (int)S7WordLength.DWord: return 4; + case (int)S7WordLength.Int: return 2; + case (int)S7WordLength.DInt: return 4; + case (int)S7WordLength.Real: return 4; + case (int)S7WordLength.Counter: return 2; + case (int)S7WordLength.Timer: return 2; default: return 0; } } diff --git a/Sharp7/S7MultiVar.cs b/Sharp7/S7MultiVar.cs index 717ee61..a7623bf 100644 --- a/Sharp7/S7MultiVar.cs +++ b/Sharp7/S7MultiVar.cs @@ -1,7 +1,6 @@ using System; using System.Linq; using System.Runtime.InteropServices; -#pragma warning disable 618 namespace Sharp7 { @@ -23,20 +22,20 @@ namespace Sharp7 if (WordSize == 0) return false; - if (Area == S7Consts.S7AreaCT) - WordLen = S7Consts.S7WLCounter; - if (Area == S7Consts.S7AreaTM) - WordLen = S7Consts.S7WLTimer; + if (Area == (int)S7Area.CT) + WordLen = (int)S7WordLength.Counter; + if (Area == (int)S7Area.TM) + WordLen = (int)S7WordLength.Timer; - if (WordLen == S7Consts.S7WLBit) + if (WordLen == (int)S7WordLength.Bit) Amount = 1; // Only 1 bit can be transferred at time else { - if ((WordLen != S7Consts.S7WLCounter) && (WordLen != S7Consts.S7WLTimer)) + if ((WordLen != (int)S7WordLength.Counter) && (WordLen != (int)S7WordLength.Timer)) { Amount = Amount * WordSize; Start = Start * 8; - WordLen = S7Consts.S7WLByte; + WordLen = (int)S7WordLength.Byte; } } return true;