mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
Minor beautification tweaks and additional README info.
This commit is contained in:
33
README.md
33
README.md
@@ -13,3 +13,36 @@ to my request for committing code, I decided to pick up where he left off here o
|
||||
|
||||
+ Compatible S7 PLC (S7-200, S7-300, S7-400, S7-1200)
|
||||
+ .NET Framework 3.5 or higher
|
||||
|
||||
## Basic Usage
|
||||
|
||||
```C#
|
||||
string deviceIpAddress = "172.25.116.87";
|
||||
int rackNumber = 0;
|
||||
int slotNumber = 2;
|
||||
using (var plc = new PLC(CPU_Type.S7300, deviceIpAddress, rackNumber, slotNumber))
|
||||
{
|
||||
//Ensure IP is responding
|
||||
if (plc.IsAvailable)
|
||||
{
|
||||
ErrorCode connectionResult = plc.Open();
|
||||
|
||||
//Verify that connection was successful
|
||||
if (connectionResult.Equals(ErrorCode.NoError))
|
||||
{
|
||||
//Get data
|
||||
object data = plc.Read("MB59");
|
||||
|
||||
Console.WriteLine("SUCCESS: Read result of MB59 is {0}", data);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR: Device is available but connection was unsuccessful!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("ERROR: Device is not available!");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
{
|
||||
S7200 = 0,
|
||||
S7300 = 10,
|
||||
S7400 = 20
|
||||
S7400 = 20,
|
||||
S71200 = 30,
|
||||
}
|
||||
|
||||
public enum ErrorCode
|
||||
|
||||
@@ -7,6 +7,8 @@ namespace S7
|
||||
{
|
||||
public class Plc : IPlc
|
||||
{
|
||||
private Socket _mSocket; //TCP connection to device
|
||||
|
||||
public string IP
|
||||
{ get; set; }
|
||||
|
||||
@@ -40,8 +42,6 @@ namespace S7
|
||||
public string LastErrorString { get; private set; }
|
||||
public ErrorCode LastErrorCode { get; private set; }
|
||||
|
||||
private Socket _mSocket;
|
||||
|
||||
public Plc() : this(CpuType.S7400, "localhost", 0, 2) { }
|
||||
|
||||
public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot, string name = "", object tag = null)
|
||||
@@ -91,9 +91,8 @@ namespace S7
|
||||
}
|
||||
|
||||
try {
|
||||
byte[] bSend1 = { 3, 0, 0, 22, 17, 224, 0, 0, 0, 46,
|
||||
0, 193, 2, 1, 0, 194, 2, 3, 0, 192,
|
||||
1, 9 };
|
||||
byte[] bSend1 = { 3, 0, 0, 22, 17, 224, 0, 0, 0, 46, 0, 193, 2, 1, 0, 194, 2, 3, 0, 192, 1, 9 };
|
||||
|
||||
switch (CPU) {
|
||||
case CpuType.S7200:
|
||||
//S7200: Chr(193) & Chr(2) & Chr(16) & Chr(0) 'Eigener Tsap
|
||||
@@ -107,6 +106,7 @@ namespace S7
|
||||
bSend1[17] = 16;
|
||||
bSend1[18] = 0;
|
||||
break;
|
||||
case CpuType.S71200:
|
||||
case CpuType.S7300:
|
||||
//S7300: Chr(193) & Chr(2) & Chr(1) & Chr(0) 'Eigener Tsap
|
||||
bSend1[11] = 193;
|
||||
@@ -136,14 +136,19 @@ namespace S7
|
||||
}
|
||||
_mSocket.Send(bSend1, 22, SocketFlags.None);
|
||||
|
||||
if (_mSocket.Receive(bReceive, 22, SocketFlags.None) != 22) throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString());
|
||||
if (_mSocket.Receive(bReceive, 22, SocketFlags.None) != 22)
|
||||
{
|
||||
throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString());
|
||||
}
|
||||
|
||||
byte[] bsend2 = { 3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 255, 255, 0, 8, 0, 0, 240, 0, 0, 3, 0, 3, 1, 0 };
|
||||
|
||||
byte[] bsend2 = { 3, 0, 0, 25, 2, 240, 128, 50, 1, 0,
|
||||
0, 255, 255, 0, 8, 0, 0, 240, 0, 0,
|
||||
3, 0, 3, 1, 0 };
|
||||
_mSocket.Send(bsend2, 25, SocketFlags.None);
|
||||
|
||||
if (_mSocket.Receive(bReceive, 27, SocketFlags.None) != 27) throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString());
|
||||
if (_mSocket.Receive(bReceive, 27, SocketFlags.None) != 27)
|
||||
{
|
||||
throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString());
|
||||
}
|
||||
IsConnected = true;
|
||||
}
|
||||
catch
|
||||
@@ -155,7 +160,6 @@ namespace S7
|
||||
}
|
||||
|
||||
return ErrorCode.NoError;
|
||||
// ok
|
||||
}
|
||||
|
||||
public void Close()
|
||||
@@ -178,9 +182,7 @@ namespace S7
|
||||
|
||||
package.Add(new byte[] { 0x03, 0x00, 0x00 });
|
||||
package.Add((byte)packageSize);
|
||||
package.Add(new byte[] { 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12,
|
||||
0x0a, 0x10});
|
||||
package.Add(new byte[] { 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12, 0x0a, 0x10});
|
||||
// package.Add(0x02); // datenart
|
||||
switch (DataType)
|
||||
{
|
||||
|
||||
5
S7.sln
5
S7.sln
@@ -5,6 +5,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S7.Net", "S7.Net\S7.Net.csp
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S7.Net.Test", "S7.Net.Test\S7.Net.Test.csproj", "{D34F7AF6-19DA-4AE6-9BB0-6666970F6838}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7A8252C3-E6AE-435A-809D-4413C06E0711}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
README.md = README.md
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
||||
Reference in New Issue
Block a user