Merge pull request #13 from fbarresi/develop

Methods refactors and constants deprecations
This commit is contained in:
Federico Barresi
2020-06-11 15:31:43 +02:00
committed by GitHub
12 changed files with 259 additions and 288 deletions

View File

@@ -15,16 +15,40 @@ or read the [Wiki](https://github.com/fbarresi/Sharp7/wiki).
# How to install # How to install
## Package Manager ## Package Manager or dotnet CLI
``` ```
PM> Install-Package Sharp7 PM> Install-Package Sharp7
``` ```
or
## .NET CLI
``` ```
> dotnet add package Sharp7 > dotnet add package Sharp7
``` ```
## Do you need more power? # Do you need more power?
Try [Sharp7Reactive](https://github.com/evopro-ag/Sharp7Reactive) 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)

View File

@@ -177,10 +177,10 @@ namespace Sharp7.Tests
var multivar = new S7MultiVar(Client); var multivar = new S7MultiVar(Client);
multivar.ShouldNotBeNull(); 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.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); multivar.Write().ShouldBe(0);

View File

@@ -14,7 +14,7 @@ namespace Sharp7.Tests
client = new S7Client(); client = new S7Client();
} }
public new void Dispose() public void Dispose()
{ {
client.Disconnect(); client.Disconnect();
} }

View File

@@ -345,10 +345,7 @@ namespace Sharp7.Tests
else else
return 0; return 0;
} }
set set => Srv_SetMask(server, S7Server.MkLog, value);
{
Srv_SetMask(server, S7Server.MkLog, value);
}
} }
// Property EventMask R/W // Property EventMask R/W
@@ -365,10 +362,7 @@ namespace Sharp7.Tests
else else
return 0; return 0;
} }
set set => Srv_SetMask(server, S7Server.MkEvent, value);
{
Srv_SetMask(server, S7Server.MkEvent, value);
}
} }
@@ -395,10 +389,7 @@ namespace Sharp7.Tests
else else
return -1; return -1;
} }
set set => Srv_SetCpuStatus(server, value);
{
Srv_SetCpuStatus(server, value);
}
} }
// Property Server Status Read Only // Property Server Status Read Only

View File

@@ -160,48 +160,23 @@ namespace Sharp7
return LastError; return LastError;
} }
public bool Connected public bool Connected => (TCPSocket != null) && (TCPSocket.Connected);
{
get
{
return (TCPSocket != null) && (TCPSocket.Connected);
}
}
public int ReadTimeout public int ReadTimeout
{ {
get get => _ReadTimeout;
{ set => _ReadTimeout = value;
return _ReadTimeout;
}
set
{
_ReadTimeout = value;
}
} }
public int WriteTimeout public int WriteTimeout
{ {
get get => _WriteTimeout;
{ set => _WriteTimeout = value;
return _WriteTimeout;
}
set
{
_WriteTimeout = value;
}
} }
public int ConnectTimeout public int ConnectTimeout
{ {
get get => _ConnectTimeout;
{ set => _ConnectTimeout = value;
return _ConnectTimeout;
}
set
{
_ConnectTimeout = value;
}
} }
} }
} }

View File

@@ -25,16 +25,16 @@ namespace Sharp7
{ {
switch (wordLength) switch (wordLength)
{ {
case S7Consts.S7WLBit: return 1; // S7 sends 1 byte per bit case (int)S7WordLength.Bit: return 1; // S7 sends 1 byte per bit
case S7Consts.S7WLByte: return 1; case (int)S7WordLength.Byte: return 1;
case S7Consts.S7WLChar: return 1; case (int)S7WordLength.Char: return 1;
case S7Consts.S7WLWord: return 2; case (int)S7WordLength.Word: return 2;
case S7Consts.S7WLDWord: return 4; case (int)S7WordLength.DWord: return 4;
case S7Consts.S7WLInt: return 2; case (int)S7WordLength.Int: return 2;
case S7Consts.S7WLDInt: return 4; case (int)S7WordLength.DInt: return 4;
case S7Consts.S7WLReal: return 4; case (int)S7WordLength.Real: return 4;
case S7Consts.S7WLCounter: return 2; case (int)S7WordLength.Counter: return 2;
case S7Consts.S7WLTimer: return 2; case (int)S7WordLength.Timer: return 2;
default: return 0; default: return 0;
} }
} }
@@ -434,7 +434,7 @@ namespace Sharp7
{ {
try try
{ {
return new DateTime(0).AddMilliseconds(S7.GetDIntAt(buffer, pos)); return new DateTime(0).AddMilliseconds(buffer.GetDIntAt(pos));
} }
catch (System.ArgumentOutOfRangeException) catch (System.ArgumentOutOfRangeException)
{ {

12
Sharp7/S7Area.cs Normal file
View File

@@ -0,0 +1,12 @@
namespace Sharp7
{
public enum S7Area
{
PE = 0x81,
PA = 0x82,
MK = 0x83,
DB = 0x84,
CT = 0x1C,
TM = 0x1D,
}
}

View File

@@ -240,7 +240,7 @@ namespace Sharp7
0x12, // Var spec. 0x12, // Var spec.
0x0a, // Length of remaining bytes 0x0a, // Length of remaining bytes
0x10, // Syntax ID 0x10, // Syntax ID
(byte)S7Consts.S7WLByte, // Transport Size idx=22 (byte)S7WordLength.Byte, // Transport Size idx=22
0x00,0x00, // Num Elements 0x00,0x00, // Num Elements
0x00,0x00, // DB Number (if any, else 0) 0x00,0x00, // DB Number (if any, else 0)
0x84, // Area Type 0x84, // Area Type
@@ -273,7 +273,7 @@ namespace Sharp7
0x12, // Var spec. 0x12, // Var spec.
0x0a, // Length of remaining bytes 0x0a, // Length of remaining bytes
0x10, // Syntax ID 0x10, // Syntax ID
(byte)S7Consts.S7WLByte, // Transport Size idx=3 (byte)S7WordLength.Byte, // Transport Size idx=3
0x00,0x00, // Num Elements 0x00,0x00, // Num Elements
0x00,0x00, // DB Number (if any, else 0) 0x00,0x00, // DB Number (if any, else 0)
0x84, // Area Type 0x84, // Area Type
@@ -300,7 +300,7 @@ namespace Sharp7
0x12, // Var spec. 0x12, // Var spec.
0x0a, // Length of remaining bytes 0x0a, // Length of remaining bytes
0x10, // Syntax ID 0x10, // Syntax ID
(byte)S7Consts.S7WLByte, // Transport Size idx=3 (byte)S7WordLength.Byte, // Transport Size idx=3
0x00,0x00, // Num Elements 0x00,0x00, // Num Elements
0x00,0x00, // DB Number (if any, else 0) 0x00,0x00, // DB Number (if any, else 0)
0x84, // Area Type 0x84, // Area Type
@@ -551,7 +551,7 @@ namespace Sharp7
RecvPacket(PDU, 0, 4); RecvPacket(PDU, 0, 4);
if (_LastError == 0) if (_LastError == 0)
{ {
Size = S7.GetWordAt(PDU, 2); Size = PDU.GetWordAt(2);
// Check 0 bytes Data Packet (only TPKT+COTP = 7 bytes) // Check 0 bytes Data Packet (only TPKT+COTP = 7 bytes)
if (Size == IsoHSize) if (Size == IsoHSize)
RecvPacket(PDU, 4, 3); // Skip remaining 3 bytes and Done is still false RecvPacket(PDU, 4, 3); // Skip remaining 3 bytes and Done is still false
@@ -612,7 +612,7 @@ namespace Sharp7
{ {
int Length; int Length;
// Set PDU Size Requested // Set PDU Size Requested
S7.SetWordAt(S7_PN, 23, (ushort)_PduSizeRequested); S7_PN.SetWordAt(23, (ushort)_PduSizeRequested);
// Sends the connection request telegram // Sends the connection request telegram
SendPacket(S7_PN); SendPacket(S7_PN);
if (_LastError == 0) if (_LastError == 0)
@@ -624,7 +624,7 @@ namespace Sharp7
if ((Length == 27) && (PDU[17] == 0) && (PDU[18] == 0)) // 20 = size of Negotiate Answer if ((Length == 27) && (PDU[17] == 0) && (PDU[18] == 0)) // 20 = size of Negotiate Answer
{ {
// Get PDU Size Negotiated // Get PDU Size Negotiated
_PDULength = S7.GetWordAt(PDU, 25); _PDULength = PDU.GetWordAt(25);
if (_PDULength <= 0) if (_PDULength <= 0)
_LastError = S7Consts.errCliNegotiatingPDU; _LastError = S7Consts.errCliNegotiatingPDU;
} }
@@ -723,7 +723,7 @@ namespace Sharp7
public int Disconnect() public int Disconnect()
{ {
Socket.Close(); Socket?.Close();
return 0; return 0;
} }
@@ -816,6 +816,15 @@ namespace Sharp7
#region [Data I/O main functions] #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) public int ReadArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer)
{ {
int BytesRead = 0; int BytesRead = 0;
@@ -837,25 +846,25 @@ namespace Sharp7
Time_ms = 0; Time_ms = 0;
int Elapsed = Environment.TickCount; int Elapsed = Environment.TickCount;
// Some adjustment // Some adjustment
if (Area == S7Consts.S7AreaCT) if (Area == (int)S7Area.CT)
WordLen = S7Consts.S7WLCounter; WordLen = (int)S7WordLength.Counter;
if (Area == S7Consts.S7AreaTM) if (Area == (int)S7Area.TM)
WordLen = S7Consts.S7WLTimer; WordLen = (int)S7WordLength.Timer;
// Calc Word size // Calc Word size
WordSize = S7.DataSizeByte(WordLen); WordSize = WordLen.DataSizeByte();
if (WordSize == 0) if (WordSize == 0)
return S7Consts.errCliInvalidWordLen; return S7Consts.errCliInvalidWordLen;
if (WordLen == S7Consts.S7WLBit) if (WordLen == (int)S7WordLength.Bit)
Amount = 1; // Only 1 bit can be transferred at time Amount = 1; // Only 1 bit can be transferred at time
else else
{ {
if ((WordLen != S7Consts.S7WLCounter) && (WordLen != S7Consts.S7WLTimer)) if ((WordLen != (int)S7WordLength.Counter) && (WordLen != (int)S7WordLength.Timer))
{ {
Amount = Amount * WordSize; Amount = Amount * WordSize;
WordSize = 1; WordSize = 1;
WordLen = S7Consts.S7WLByte; WordLen = (int)S7WordLength.Byte;
} }
} }
@@ -875,11 +884,11 @@ namespace Sharp7
// Set DB Number // Set DB Number
PDU[27] = (byte)Area; PDU[27] = (byte)Area;
// Set Area // Set Area
if (Area == S7Consts.S7AreaDB) if (Area == (int)S7Area.DB)
S7.SetWordAt(PDU, 25, (ushort)DBNumber); PDU.SetWordAt(25, (ushort)DBNumber);
// Adjusts Start and word length // 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; Address = Start;
PDU[22] = (byte)WordLen; PDU[22] = (byte)WordLen;
@@ -888,7 +897,7 @@ namespace Sharp7
Address = Start << 3; Address = Start << 3;
// Num elements // Num elements
S7.SetWordAt(PDU, 23, (ushort)NumElements); PDU.SetWordAt(23, (ushort)NumElements);
// Address into the PLC (only 3 bytes) // Address into the PLC (only 3 bytes)
PDU[30] = (byte)(Address & 0x0FF); PDU[30] = (byte)(Address & 0x0FF);
@@ -931,6 +940,16 @@ namespace Sharp7
return _LastError; 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) public int WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, byte[] Buffer)
{ {
int BytesWritten = 0; int BytesWritten = 0;
@@ -953,25 +972,25 @@ namespace Sharp7
Time_ms = 0; Time_ms = 0;
int Elapsed = Environment.TickCount; int Elapsed = Environment.TickCount;
// Some adjustment // Some adjustment
if (Area == S7Consts.S7AreaCT) if (Area == (int)S7Area.CT)
WordLen = S7Consts.S7WLCounter; WordLen = (int)S7WordLength.Counter;
if (Area == S7Consts.S7AreaTM) if (Area == (int)S7Area.TM)
WordLen = S7Consts.S7WLTimer; WordLen = (int)S7WordLength.Timer;
// Calc Word size // Calc Word size
WordSize = S7.DataSizeByte(WordLen); WordSize = WordLen.DataSizeByte();
if (WordSize == 0) if (WordSize == 0)
return S7Consts.errCliInvalidWordLen; 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; Amount = 1;
else else
{ {
if ((WordLen != S7Consts.S7WLCounter) && (WordLen != S7Consts.S7WLTimer)) if ((WordLen != (int)S7WordLength.Counter) && (WordLen != (int)S7WordLength.Timer))
{ {
Amount = Amount * WordSize; Amount = Amount * WordSize;
WordSize = 1; WordSize = 1;
WordLen = S7Consts.S7WLByte; WordLen = (int)S7WordLength.Byte;
} }
} }
@@ -990,20 +1009,20 @@ namespace Sharp7
// Setup the telegram // Setup the telegram
Array.Copy(S7_RW, 0, PDU, 0, Size_WR); Array.Copy(S7_RW, 0, PDU, 0, Size_WR);
// Whole telegram Size // Whole telegram Size
S7.SetWordAt(PDU, 2, (ushort)IsoSize); PDU.SetWordAt(2, (ushort)IsoSize);
// Data Length // Data Length
Length = DataSize + 4; Length = DataSize + 4;
S7.SetWordAt(PDU, 15, (ushort)Length); PDU.SetWordAt(15, (ushort)Length);
// Function // Function
PDU[17] = (byte)0x05; PDU[17] = (byte)0x05;
// Set DB Number // Set DB Number
PDU[27] = (byte)Area; PDU[27] = (byte)Area;
if (Area == S7Consts.S7AreaDB) if (Area == (int)S7Area.DB)
S7.SetWordAt(PDU, 25, (ushort)DBNumber); PDU.SetWordAt(25, (ushort)DBNumber);
// Adjusts Start and word length // 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; Address = Start;
Length = DataSize; Length = DataSize;
@@ -1016,7 +1035,7 @@ namespace Sharp7
} }
// Num elements // Num elements
S7.SetWordAt(PDU, 23, (ushort)NumElements); PDU.SetWordAt(23, (ushort)NumElements);
// Address into the PLC // Address into the PLC
PDU[30] = (byte)(Address & 0x0FF); PDU[30] = (byte)(Address & 0x0FF);
Address = Address >> 8; Address = Address >> 8;
@@ -1027,11 +1046,11 @@ namespace Sharp7
// Transport Size // Transport Size
switch (WordLen) switch (WordLen)
{ {
case S7Consts.S7WLBit: case (int)S7WordLength.Bit:
PDU[32] = TS_ResBit; PDU[32] = TS_ResBit;
break; break;
case S7Consts.S7WLCounter: case (int)S7WordLength.Counter:
case S7Consts.S7WLTimer: case (int)S7WordLength.Timer:
PDU[32] = TS_ResOctet; PDU[32] = TS_ResOctet;
break; break;
default: default:
@@ -1039,7 +1058,7 @@ namespace Sharp7
break; break;
}; };
// Length // Length
S7.SetWordAt(PDU, 33, (ushort)Length); PDU.SetWordAt(33, (ushort)Length);
// Copies the Data // Copies the Data
Array.Copy(Buffer, Offset, PDU, 35, DataSize); Array.Copy(Buffer, Offset, PDU, 35, DataSize);
@@ -1093,7 +1112,7 @@ namespace Sharp7
// Fills Header // Fills Header
Array.Copy(S7_MRD_HEADER, 0, PDU, 0, S7_MRD_HEADER.Length); 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; PDU[18] = (byte)ItemsCount;
// Fills the Items // Fills the Items
Offset = 19; Offset = 19;
@@ -1101,9 +1120,9 @@ namespace Sharp7
{ {
Array.Copy(S7_MRD_ITEM, S7Item, S7Item.Length); Array.Copy(S7_MRD_ITEM, S7Item, S7Item.Length);
S7Item[3] = (byte)Items[c].WordLen; 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) if (Items[c].Area == (int)S7Area.DB)
S7.SetWordAt(S7Item, 6, (ushort)Items[c].DBNumber); S7Item.SetWordAt(6, (ushort)Items[c].DBNumber);
S7Item[8] = (byte)Items[c].Area; S7Item[8] = (byte)Items[c].Area;
// Address into the PLC // Address into the PLC
@@ -1121,7 +1140,7 @@ namespace Sharp7
if (Offset > _PDULength) if (Offset > _PDULength)
return S7Consts.errCliSizeOverPDU; return S7Consts.errCliSizeOverPDU;
S7.SetWordAt(PDU, 2, (ushort)Offset); // Whole size PDU.SetWordAt(2, (ushort)Offset); // Whole size
SendPacket(PDU, Offset); SendPacket(PDU, Offset);
if (_LastError != 0) if (_LastError != 0)
@@ -1137,11 +1156,11 @@ namespace Sharp7
return _LastError; return _LastError;
} }
// Check Global Operation Result // Check Global Operation Result
_LastError = CpuError(S7.GetWordAt(PDU, 17)); _LastError = CpuError(PDU.GetWordAt(17));
if (_LastError != 0) if (_LastError != 0)
return _LastError; return _LastError;
// Get true ItemsCount // Get true ItemsCount
int ItemsRead = S7.GetByteAt(PDU, 20); int ItemsRead = PDU.GetByteAt(20);
if ((ItemsRead != ItemsCount) || (ItemsRead>MaxVars)) if ((ItemsRead != ItemsCount) || (ItemsRead>MaxVars))
{ {
_LastError = S7Consts.errCliInvalidPlcAnswer; _LastError = S7Consts.errCliInvalidPlcAnswer;
@@ -1155,7 +1174,7 @@ namespace Sharp7
Array.Copy(PDU, Offset, S7ItemRead, 0, Length-Offset); Array.Copy(PDU, Offset, S7ItemRead, 0, Length-Offset);
if (S7ItemRead[0] == 0xff) 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)) if ((S7ItemRead[1] != TS_ResOctet) && (S7ItemRead[1] != TS_ResReal) && (S7ItemRead[1] != TS_ResBit))
ItemSize = ItemSize >> 3; ItemSize = ItemSize >> 3;
Marshal.Copy(S7ItemRead, 4, Items[c].pData, ItemSize); Marshal.Copy(S7ItemRead, 4, Items[c].pData, ItemSize);
@@ -1193,7 +1212,7 @@ namespace Sharp7
// Fills Header // Fills Header
Array.Copy(S7_MWR_HEADER, 0, PDU, 0, S7_MWR_HEADER.Length); Array.Copy(S7_MWR_HEADER, 0, PDU, 0, S7_MWR_HEADER.Length);
ParLength = ItemsCount * S7_MWR_PARAM.Length + 2; ParLength = ItemsCount * S7_MWR_PARAM.Length + 2;
S7.SetWordAt(PDU, 13, (ushort)ParLength); PDU.SetWordAt(13, (ushort)ParLength);
PDU[18] = (byte)ItemsCount; PDU[18] = (byte)ItemsCount;
// Fills Params // Fills Params
Offset = S7_MWR_HEADER.Length; Offset = S7_MWR_HEADER.Length;
@@ -1202,8 +1221,8 @@ namespace Sharp7
Array.Copy(S7_MWR_PARAM, 0, S7ParItem, 0, S7_MWR_PARAM.Length); Array.Copy(S7_MWR_PARAM, 0, S7ParItem, 0, S7_MWR_PARAM.Length);
S7ParItem[3] = (byte)Items[c].WordLen; S7ParItem[3] = (byte)Items[c].WordLen;
S7ParItem[8] = (byte)Items[c].Area; S7ParItem[8] = (byte)Items[c].Area;
S7.SetWordAt(S7ParItem, 4, (ushort)Items[c].Amount); S7ParItem.SetWordAt(4, (ushort)Items[c].Amount);
S7.SetWordAt(S7ParItem, 6, (ushort)Items[c].DBNumber); S7ParItem.SetWordAt(6, (ushort)Items[c].DBNumber);
// Address into the PLC // Address into the PLC
int Address = Items[c].Start; int Address = Items[c].Start;
S7ParItem[11] = (byte)(Address & 0x0FF); S7ParItem[11] = (byte)(Address & 0x0FF);
@@ -1221,26 +1240,26 @@ namespace Sharp7
S7DataItem[0] = 0x00; S7DataItem[0] = 0x00;
switch (Items[c].WordLen) switch (Items[c].WordLen)
{ {
case S7Consts.S7WLBit: case (int)S7WordLength.Bit:
S7DataItem[1] = TS_ResBit; S7DataItem[1] = TS_ResBit;
break; break;
case S7Consts.S7WLCounter: case (int)S7WordLength.Counter:
case S7Consts.S7WLTimer: case (int)S7WordLength.Timer:
S7DataItem[1] = TS_ResOctet; S7DataItem[1] = TS_ResOctet;
break; break;
default: default:
S7DataItem[1] = TS_ResByte; // byte/word/dword etc. S7DataItem[1] = TS_ResByte; // byte/word/dword etc.
break; 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; ItemDataSize = Items[c].Amount * 2;
else else
ItemDataSize = Items[c].Amount; ItemDataSize = Items[c].Amount;
if ((S7DataItem[1] != TS_ResOctet) && (S7DataItem[1] != TS_ResBit)) if ((S7DataItem[1] != TS_ResOctet) && (S7DataItem[1] != TS_ResBit))
S7.SetWordAt(S7DataItem, 2, (ushort)(ItemDataSize*8)); S7DataItem.SetWordAt(2, (ushort)(ItemDataSize*8));
else else
S7.SetWordAt(S7DataItem, 2, (ushort)ItemDataSize); S7DataItem.SetWordAt(2, (ushort)ItemDataSize);
Marshal.Copy(Items[c].pData, S7DataItem, 4, ItemDataSize); Marshal.Copy(Items[c].pData, S7DataItem, 4, ItemDataSize);
if (ItemDataSize % 2 != 0) if (ItemDataSize % 2 != 0)
@@ -1257,19 +1276,19 @@ namespace Sharp7
if (Offset > _PDULength) if (Offset > _PDULength)
return S7Consts.errCliSizeOverPDU; return S7Consts.errCliSizeOverPDU;
S7.SetWordAt(PDU, 2, (ushort)Offset); // Whole size PDU.SetWordAt(2, (ushort)Offset); // Whole size
S7.SetWordAt(PDU, 15, (ushort)DataLength); // Whole size PDU.SetWordAt(15, (ushort)DataLength); // Whole size
SendPacket(PDU, Offset); SendPacket(PDU, Offset);
RecvIsoPacket(); RecvIsoPacket();
if (_LastError==0) if (_LastError==0)
{ {
// Check Global Operation Result // Check Global Operation Result
_LastError = CpuError(S7.GetWordAt(PDU, 17)); _LastError = CpuError(PDU.GetWordAt(17));
if (_LastError != 0) if (_LastError != 0)
return _LastError; return _LastError;
// Get true ItemsCount // Get true ItemsCount
int ItemsWritten = S7.GetByteAt(PDU, 20); int ItemsWritten = PDU.GetByteAt(20);
if ((ItemsWritten != ItemsCount) || (ItemsWritten > MaxVars)) if ((ItemsWritten != ItemsCount) || (ItemsWritten > MaxVars))
{ {
_LastError = S7Consts.errCliInvalidPlcAnswer; _LastError = S7Consts.errCliInvalidPlcAnswer;
@@ -1294,48 +1313,48 @@ namespace Sharp7
public int DBRead(int DBNumber, int Start, int Size, byte[] Buffer) 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) 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) 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) 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) 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) 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) 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) 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) public int TMRead(int Start, int Amount, ushort[] Buffer)
{ {
byte[] sBuffer = new byte[Amount * 2]; 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) if (Result == 0)
{ {
for (int c = 0; c < Amount; c++) for (int c = 0; c < Amount; c++)
@@ -1354,13 +1373,13 @@ namespace Sharp7
sBuffer[c * 2 + 1] = (byte)((Buffer[c] & 0xFF00) >> 8); sBuffer[c * 2 + 1] = (byte)((Buffer[c] & 0xFF00) >> 8);
sBuffer[c * 2] = (byte)(Buffer[c] & 0x00FF); 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) public int CTRead(int Start, int Amount, ushort[] Buffer)
{ {
byte[] sBuffer = new byte[Amount * 2]; 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) if (Result==0)
{ {
for (int c=0; c<Amount; c++) for (int c=0; c<Amount; c++)
@@ -1379,7 +1398,7 @@ namespace Sharp7
sBuffer[c * 2 + 1] = (byte)((Buffer[c] & 0xFF00)>>8); sBuffer[c * 2 + 1] = (byte)((Buffer[c] & 0xFF00)>>8);
sBuffer[c * 2]= (byte)(Buffer[c] & 0x00FF); 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 #endregion
@@ -1426,24 +1445,24 @@ namespace Sharp7
int Length = RecvIsoPacket(); int Length = RecvIsoPacket();
if (Length > 32) // the minimum expected if (Length > 32) // the minimum expected
{ {
ushort Result = S7.GetWordAt(PDU, 27); ushort Result = PDU.GetWordAt(27);
if (Result == 0) if (Result == 0)
{ {
Info.BlkFlags= PDU[42]; Info.BlkFlags= PDU[42];
Info.BlkLang = PDU[43]; Info.BlkLang = PDU[43];
Info.BlkType = PDU[44]; Info.BlkType = PDU[44];
Info.BlkNumber = S7.GetWordAt(PDU, 45); Info.BlkNumber = PDU.GetWordAt(45);
Info.LoadSize = S7.GetDIntAt(PDU, 47); Info.LoadSize = PDU.GetDIntAt(47);
Info.CodeDate = SiemensTimestamp(S7.GetWordAt(PDU, 59)); Info.CodeDate = SiemensTimestamp(PDU.GetWordAt(59));
Info.IntfDate = SiemensTimestamp(S7.GetWordAt(PDU, 65)); Info.IntfDate = SiemensTimestamp(PDU.GetWordAt(65));
Info.SBBLength = S7.GetWordAt(PDU, 67); Info.SBBLength = PDU.GetWordAt(67);
Info.LocalData = S7.GetWordAt(PDU, 71); Info.LocalData = PDU.GetWordAt(71);
Info.MC7Size = S7.GetWordAt(PDU, 73); Info.MC7Size = PDU.GetWordAt(73);
Info.Author = S7.GetCharsAt(PDU, 75, 8).Trim(new char[]{(char)0}); Info.Author = PDU.GetCharsAt(75, 8).Trim(new char[]{(char)0});
Info.Family = S7.GetCharsAt(PDU, 83, 8).Trim(new char[]{(char)0}); Info.Family = PDU.GetCharsAt(83, 8).Trim(new char[]{(char)0});
Info.Header = S7.GetCharsAt(PDU, 91, 8).Trim(new char[]{(char)0}); Info.Header = PDU.GetCharsAt(91, 8).Trim(new char[]{(char)0});
Info.Version = PDU[99]; Info.Version = PDU[99];
Info.CheckSum = S7.GetWordAt(PDU, 101); Info.CheckSum = PDU.GetWordAt(101);
} }
else else
_LastError = CpuError(Result); _LastError = CpuError(Result);
@@ -1555,9 +1574,9 @@ namespace Sharp7
Length = RecvIsoPacket(); Length = RecvIsoPacket();
if (Length > 30) // the minimum expected 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 else
_LastError = S7Consts.errCliInvalidPlcAnswer; _LastError = S7Consts.errCliInvalidPlcAnswer;
@@ -1579,14 +1598,14 @@ namespace Sharp7
Time_ms = 0; Time_ms = 0;
int Elapsed = Environment.TickCount; int Elapsed = Environment.TickCount;
S7.SetDateTimeAt(S7_SET_DT, 31, DT); S7_SET_DT.SetDateTimeAt(31, DT);
SendPacket(S7_SET_DT); SendPacket(S7_SET_DT);
if (_LastError == 0) if (_LastError == 0)
{ {
Length = RecvIsoPacket(); Length = RecvIsoPacket();
if (Length > 30) // the minimum expected if (Length > 30) // the minimum expected
{ {
if (S7.GetWordAt(PDU, 27) != 0) if (PDU.GetWordAt(27) != 0)
_LastError = S7Consts.errCliInvalidPlcAnswer; _LastError = S7Consts.errCliInvalidPlcAnswer;
} }
else else
@@ -1616,7 +1635,7 @@ namespace Sharp7
_LastError = ReadSZL(0x0011, 0x000, ref SZL, ref Size); _LastError = ReadSZL(0x0011, 0x000, ref SZL, ref Size);
if (_LastError == 0) 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.V1 = SZL.Data[Size - 3];
Info.V2 = SZL.Data[Size - 2]; Info.V2 = SZL.Data[Size - 2];
Info.V3 = SZL.Data[Size - 1]; Info.V3 = SZL.Data[Size - 1];
@@ -1635,11 +1654,11 @@ namespace Sharp7
_LastError = ReadSZL(0x001C, 0x000, ref SZL, ref Size); _LastError = ReadSZL(0x001C, 0x000, ref SZL, ref Size);
if (_LastError == 0) if (_LastError == 0)
{ {
Info.ModuleTypeName = S7.GetCharsAt(SZL.Data, 172, 32); Info.ModuleTypeName = SZL.Data.GetCharsAt(172, 32);
Info.SerialNumber = S7.GetCharsAt(SZL.Data, 138, 24); Info.SerialNumber = SZL.Data.GetCharsAt(138, 24);
Info.ASName = S7.GetCharsAt(SZL.Data, 2, 24); Info.ASName = SZL.Data.GetCharsAt(2, 24);
Info.Copyright = S7.GetCharsAt(SZL.Data, 104, 26); Info.Copyright = SZL.Data.GetCharsAt(104, 26);
Info.ModuleName = S7.GetCharsAt(SZL.Data, 36, 24); Info.ModuleName = SZL.Data.GetCharsAt(36, 24);
} }
if (_LastError == 0) if (_LastError == 0)
Time_ms = Environment.TickCount - Elapsed; Time_ms = Environment.TickCount - Elapsed;
@@ -1655,10 +1674,10 @@ namespace Sharp7
_LastError = ReadSZL(0x0131, 0x001, ref SZL, ref Size); _LastError = ReadSZL(0x0131, 0x001, ref SZL, ref Size);
if (_LastError == 0) if (_LastError == 0)
{ {
Info.MaxPduLength = S7.GetIntAt(PDU, 2); Info.MaxPduLength = PDU.GetIntAt(2);
Info.MaxConnections = S7.GetIntAt(PDU, 4); Info.MaxConnections = PDU.GetIntAt(4);
Info.MaxMpiRate = S7.GetDIntAt(PDU, 6); Info.MaxMpiRate = PDU.GetDIntAt(6);
Info.MaxBusRate = S7.GetDIntAt(PDU, 10); Info.MaxBusRate = PDU.GetDIntAt(10);
} }
if (_LastError == 0) if (_LastError == 0)
Time_ms = Environment.TickCount - Elapsed; Time_ms = Environment.TickCount - Elapsed;
@@ -1684,14 +1703,14 @@ namespace Sharp7
{ {
if (First) if (First)
{ {
S7.SetWordAt(S7_SZL_FIRST, 11, ++Seq_out); S7_SZL_FIRST.SetWordAt(11, ++Seq_out);
S7.SetWordAt(S7_SZL_FIRST, 29, (ushort)ID); S7_SZL_FIRST.SetWordAt(29, (ushort)ID);
S7.SetWordAt(S7_SZL_FIRST, 31, (ushort)Index); S7_SZL_FIRST.SetWordAt(31, (ushort)Index);
SendPacket(S7_SZL_FIRST); SendPacket(S7_SZL_FIRST);
} }
else else
{ {
S7.SetWordAt(S7_SZL_NEXT, 11, ++Seq_out); S7_SZL_NEXT.SetWordAt(11, ++Seq_out);
PDU[24] = (byte)Seq_in; PDU[24] = (byte)Seq_in;
SendPacket(S7_SZL_NEXT); SendPacket(S7_SZL_NEXT);
} }
@@ -1705,14 +1724,14 @@ namespace Sharp7
{ {
if (Length > 32) // the minimum expected 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 // 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; Done = PDU[26] == 0x00;
Seq_in = (byte)PDU[24]; // Slice sequence Seq_in = (byte)PDU[24]; // Slice sequence
SZL.Header.LENTHDR = S7.GetWordAt(PDU, 37); SZL.Header.LENTHDR = PDU.GetWordAt(37);
SZL.Header.N_DR = S7.GetWordAt(PDU, 39); SZL.Header.N_DR = PDU.GetWordAt(39);
Array.Copy(PDU, 41, SZL.Data, Offset, DataSZL); Array.Copy(PDU, 41, SZL.Data, Offset, DataSZL);
// SZL.Copy(PDU, 41, Offset, DataSZL); // SZL.Copy(PDU, 41, Offset, DataSZL);
Offset += DataSZL; Offset += DataSZL;
@@ -1728,10 +1747,10 @@ namespace Sharp7
{ {
if (Length > 32) // the minimum expected 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 // Gets Amount of this slice
DataSZL = S7.GetWordAt(PDU, 31); DataSZL = PDU.GetWordAt(31);
Done = PDU[26] == 0x00; Done = PDU[26] == 0x00;
Seq_in = (byte)PDU[24]; // Slice sequence Seq_in = (byte)PDU[24]; // Slice sequence
Array.Copy(PDU, 37, SZL.Data, Offset, DataSZL); Array.Copy(PDU, 37, SZL.Data, Offset, DataSZL);
@@ -1873,7 +1892,7 @@ namespace Sharp7
int Length = RecvIsoPacket(); int Length = RecvIsoPacket();
if (Length > 30) // the minimum expected if (Length > 30) // the minimum expected
{ {
ushort Result = S7.GetWordAt(PDU, 27); ushort Result = PDU.GetWordAt(27);
if (Result == 0) if (Result == 0)
{ {
switch (PDU[44]) switch (PDU[44])
@@ -1915,7 +1934,7 @@ namespace Sharp7
_LastError = 0; _LastError = 0;
int Elapsed = Environment.TickCount; int Elapsed = Environment.TickCount;
// Encodes the Password // Encodes the Password
S7.SetCharsAt(pwd, 0, Password); pwd.SetCharsAt(0, Password);
pwd[0] = (byte)(pwd[0] ^ 0x55); pwd[0] = (byte)(pwd[0] ^ 0x55);
pwd[1] = (byte)(pwd[1] ^ 0x55); pwd[1] = (byte)(pwd[1] ^ 0x55);
for (int c = 2; c < 8; c++) for (int c = 2; c < 8; c++)
@@ -1930,7 +1949,7 @@ namespace Sharp7
Length = RecvIsoPacket(); Length = RecvIsoPacket();
if (Length > 32) // the minimum expected if (Length > 32) // the minimum expected
{ {
ushort Result = S7.GetWordAt(PDU, 27); ushort Result = PDU.GetWordAt(27);
if (Result != 0) if (Result != 0)
_LastError = CpuError(Result); _LastError = CpuError(Result);
} }
@@ -1953,7 +1972,7 @@ namespace Sharp7
Length = RecvIsoPacket(); Length = RecvIsoPacket();
if (Length > 30) // the minimum expected if (Length > 30) // the minimum expected
{ {
ushort Result = S7.GetWordAt(PDU, 27); ushort Result = PDU.GetWordAt(27);
if (Result != 0) if (Result != 0)
_LastError = CpuError(Result); _LastError = CpuError(Result);
} }
@@ -1971,11 +1990,11 @@ namespace Sharp7
_LastError = ReadSZL(0x0232, 0x0004, ref SZL, ref Size); _LastError = ReadSZL(0x0232, 0x0004, ref SZL, ref Size);
if (_LastError == 0) if (_LastError == 0)
{ {
Protection.sch_schal = S7.GetWordAt(SZL.Data, 2); Protection.sch_schal = SZL.Data.GetWordAt(2);
Protection.sch_par = S7.GetWordAt(SZL.Data, 4); Protection.sch_par = SZL.Data.GetWordAt(4);
Protection.sch_rel = S7.GetWordAt(SZL.Data, 6); Protection.sch_rel = SZL.Data.GetWordAt(6);
Protection.bart_sch = S7.GetWordAt(SZL.Data, 8); Protection.bart_sch = SZL.Data.GetWordAt(8);
Protection.anl_sch = S7.GetWordAt(SZL.Data, 10); Protection.anl_sch = SZL.Data.GetWordAt(10);
} }
return _LastError; return _LastError;
} }
@@ -1989,7 +2008,7 @@ namespace Sharp7
Time_ms = 0; Time_ms = 0;
int Elapsed = Environment.TickCount; int Elapsed = Environment.TickCount;
Array.Copy(TPKT_ISO, 0, PDU, 0, TPKT_ISO.Length); 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 try
{ {
Array.Copy(Buffer, 0, PDU, TPKT_ISO.Length, Size); Array.Copy(Buffer, 0, PDU, TPKT_ISO.Length, Size);
@@ -2232,28 +2251,13 @@ namespace Sharp7
return Time_ms; return Time_ms;
} }
public int ExecutionTime public int ExecutionTime => Time_ms;
{
get
{
return Time_ms;
}
}
public int PduSizeNegotiated public int PduSizeNegotiated => _PDULength;
{
get
{
return _PDULength;
}
}
public int PduSizeRequested public int PduSizeRequested
{ {
get get => _PduSizeRequested;
{
return _PduSizeRequested;
}
set set
{ {
if (value < MinPduSizeToRequest) if (value < MinPduSizeToRequest)
@@ -2266,60 +2270,29 @@ namespace Sharp7
public int PLCPort public int PLCPort
{ {
get get => _PLCPort;
{ set => _PLCPort = value;
return _PLCPort;
}
set
{
_PLCPort = value;
}
} }
public int ConnTimeout public int ConnTimeout
{ {
get get => Socket.ConnectTimeout;
{ set => Socket.ConnectTimeout = value;
return Socket.ConnectTimeout;
}
set
{
Socket.ConnectTimeout = value;
}
} }
public int RecvTimeout public int RecvTimeout
{ {
get get => Socket.ReadTimeout;
{ set => Socket.ReadTimeout = value;
return Socket.ReadTimeout;
}
set
{
Socket.ReadTimeout = value;
}
} }
public int SendTimeout public int SendTimeout
{ {
get get => Socket.WriteTimeout;
{ set => Socket.WriteTimeout = value;
return Socket.WriteTimeout;
}
set
{
Socket.WriteTimeout = value;
}
}
public bool Connected
{
get
{
return (Socket != null) && (Socket.Connected);
}
} }
public bool Connected => (Socket != null) && (Socket.Connected);
#endregion #endregion
} }

View File

@@ -81,23 +81,23 @@ namespace Sharp7
public const Int32 p_u32_RecoveryTime = 14; // Not applicable here public const Int32 p_u32_RecoveryTime = 14; // Not applicable here
public const Int32 p_u32_KeepAliveTime = 15; // Not applicable here public const Int32 p_u32_KeepAliveTime = 15; // Not applicable here
// Area ID // Area ID
public const byte S7AreaPE = 0x81; [Obsolete("Use enum S7Area.PE instead")]public const byte S7AreaPE = 0x81;
public const byte S7AreaPA = 0x82; [Obsolete("Use enum S7Area.PA instead")]public const byte S7AreaPA = 0x82;
public const byte S7AreaMK = 0x83; [Obsolete("Use enum S7Area.MK instead")]public const byte S7AreaMK = 0x83;
public const byte S7AreaDB = 0x84; [Obsolete("Use enum S7Area.DB instead")]public const byte S7AreaDB = 0x84;
public const byte S7AreaCT = 0x1C; [Obsolete("Use enum S7Area.CT instead")]public const byte S7AreaCT = 0x1C;
public const byte S7AreaTM = 0x1D; [Obsolete("Use enum S7Area.TM instead")]public const byte S7AreaTM = 0x1D;
// Word Length // Word Length
public const int S7WLBit = 0x01; [Obsolete("Use enum S7WordLength.Bit instead")]public const int S7WLBit = 0x01;
public const int S7WLByte = 0x02; [Obsolete("Use enum S7WordLength.Byte instead")]public const int S7WLByte = 0x02;
public const int S7WLChar = 0x03; [Obsolete("Use enum S7WordLength.Char instead")]public const int S7WLChar = 0x03;
public const int S7WLWord = 0x04; [Obsolete("Use enum S7WordLength.Word instead")]public const int S7WLWord = 0x04;
public const int S7WLInt = 0x05; [Obsolete("Use enum S7WordLength.Int instead")]public const int S7WLInt = 0x05;
public const int S7WLDWord = 0x06; [Obsolete("Use enum S7WordLength.DWord instead")]public const int S7WLDWord = 0x06;
public const int S7WLDInt = 0x07; [Obsolete("Use enum S7WordLength.DInt instead")]public const int S7WLDInt = 0x07;
public const int S7WLReal = 0x08; [Obsolete("Use enum S7WordLength.Real instead")]public const int S7WLReal = 0x08;
public const int S7WLCounter = 0x1C; [Obsolete("Use enum S7WordLength.Counter instead")]public const int S7WLCounter = 0x1C;
public const int S7WLTimer = 0x1D; [Obsolete("Use enum S7WordLength.Timer instead")]public const int S7WLTimer = 0x1D;
// PLC Status // PLC Status
public const int S7CpuStatusUnknown = 0x00; public const int S7CpuStatusUnknown = 0x00;
public const int S7CpuStatusRun = 0x08; public const int S7CpuStatusRun = 0x08;

View File

@@ -18,24 +18,24 @@ namespace Sharp7
private bool AdjustWordLength(int Area, ref int WordLen, ref int Amount, ref int Start) private bool AdjustWordLength(int Area, ref int WordLen, ref int Amount, ref int Start)
{ {
// Calc Word size // Calc Word size
int WordSize = S7.DataSizeByte(WordLen); int WordSize = WordLen.DataSizeByte();
if (WordSize == 0) if (WordSize == 0)
return false; return false;
if (Area == S7Consts.S7AreaCT) if (Area == (int)S7Area.CT)
WordLen = S7Consts.S7WLCounter; WordLen = (int)S7WordLength.Counter;
if (Area == S7Consts.S7AreaTM) if (Area == (int)S7Area.TM)
WordLen = S7Consts.S7WLTimer; WordLen = (int)S7WordLength.Timer;
if (WordLen == S7Consts.S7WLBit) if (WordLen == (int)S7WordLength.Bit)
Amount = 1; // Only 1 bit can be transferred at time Amount = 1; // Only 1 bit can be transferred at time
else else
{ {
if ((WordLen != S7Consts.S7WLCounter) && (WordLen != S7Consts.S7WLTimer)) if ((WordLen != (int)S7WordLength.Counter) && (WordLen != (int)S7WordLength.Timer))
{ {
Amount = Amount * WordSize; Amount = Amount * WordSize;
Start = Start * 8; Start = Start * 8;
WordLen = S7Consts.S7WLByte; WordLen = (int)S7WordLength.Byte;
} }
} }
return true; return true;

View File

@@ -51,34 +51,14 @@ namespace Sharp7
this.q = (buff[8] & 0x02) == 0x02; this.q = (buff[8] & 0x02) == 0x02;
} }
} }
public TimeSpan PT public TimeSpan PT => pt;
{
get public TimeSpan ET => et;
{
return pt; public bool IN => input;
}
} public bool Q => q;
public TimeSpan ET
{
get
{
return et;
}
}
public bool IN
{
get
{
return input;
}
}
public bool Q
{
get
{
return q;
}
}
#endregion #endregion
} }
} }

16
Sharp7/S7WordLength.cs Normal file
View File

@@ -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,
}
}