mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2025-12-18 01:16:50 +08:00
Updated Home (markdown)
109
Home.md
109
Home.md
@@ -18,7 +18,8 @@ To get started with S7.Net you have to download and include the S7.Net.dll in yo
|
||||
To create an instance of the driver you need to use this constructor:
|
||||
public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot)
|
||||
|
||||
• Cpu: this specify what CPU you are connecting to. The supported CPU are:
|
||||
* Cpu: this specify what CPU you are connecting to. The supported CPU are:
|
||||
|
||||
public enum CpuType {
|
||||
S7200 = 0,
|
||||
S7300 = 10,
|
||||
@@ -26,9 +27,10 @@ public enum CpuType {
|
||||
S71200 = 30,
|
||||
S71500 = 40,
|
||||
}
|
||||
• Ip: this contains the IP address of the CPU of external Ethernet card
|
||||
• Rack: this contains the rack of the plc, that you can find in hardware configuration in Step7
|
||||
• Slot: this is the slot of the CPU, that you can find in hardware configuration in Step7
|
||||
|
||||
* Ip: this contains the IP address of the CPU of external Ethernet card
|
||||
* Rack: this contains the rack of the plc, that you can find in hardware configuration in Step7
|
||||
* Slot: this is the slot of the CPU, that you can find in hardware configuration in Step7
|
||||
|
||||
Example:
|
||||
This code creates a Plc object for a S7-300 plc at the IP address 127.0.0.1, that it’s localhost, for a plc that it’s in rack 0 and a cpu that it’s in slot 2 of the hardware configuration:
|
||||
@@ -37,7 +39,9 @@ Plc plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);
|
||||
## Connecting to the PLC
|
||||
|
||||
public ErrorCode Open()
|
||||
|
||||
For example this line of code open the connection:
|
||||
|
||||
plc.Open();
|
||||
|
||||
## Disconnecting from the PLC
|
||||
@@ -45,12 +49,14 @@ plc.Open();
|
||||
public void Close()
|
||||
|
||||
For example this closes the connection:
|
||||
|
||||
plc.Close();
|
||||
|
||||
## Error handling
|
||||
|
||||
The Open() method returns an ErrorCode to check if the operation was successful. You should always check that it returns ErrorCode.NoError.
|
||||
These are the types of errors:
|
||||
|
||||
public enum ErrorCode
|
||||
{
|
||||
NoError = 0,
|
||||
@@ -67,20 +73,30 @@ public enum ErrorCode
|
||||
## Global error handling
|
||||
|
||||
Not all methods returns an error. You can check for
|
||||
|
||||
public ErrorCode LastErrorCode
|
||||
|
||||
and
|
||||
|
||||
public string LastErrorString
|
||||
|
||||
on every methods that you execute, in order to catch errors while running the driver.
|
||||
|
||||
## Check PLC availability
|
||||
|
||||
To check if the plc is available you can use the property
|
||||
|
||||
public bool IsAvailable
|
||||
|
||||
When you check this property, the driver will send a ping to the plc and returns true if the plc responds to the ping, false otherwise.
|
||||
Check PLC connection
|
||||
|
||||
## Check PLC connection
|
||||
|
||||
Checking the plc connection is trivial, because you have to check if the PC socket is connected but also if the PLC is still connected.
|
||||
The property that you have to check in this case is this:
|
||||
|
||||
public bool IsConnected
|
||||
|
||||
This property can be checked after you called the method Open(), to check if the connection is still alive.
|
||||
|
||||
|
||||
@@ -93,7 +109,8 @@ public byte[] ReadBytes(DataType dataType, int db, int startByteAdr, int count)
|
||||
public ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value)
|
||||
|
||||
This reads up to 200 bytes (actual limit of the protocol) from a memory location that you determine.
|
||||
• dataType: you have to specify the memory location with the enum DataType
|
||||
|
||||
* dataType: you have to specify the memory location with the enum DataType
|
||||
public enum DataType
|
||||
{
|
||||
Input = 129,
|
||||
@@ -103,16 +120,17 @@ public enum DataType
|
||||
Timer = 29,
|
||||
Counter = 28
|
||||
}
|
||||
• db: this is the address of the dataType, for example if you want to read DB1, this field is “1”; if you want to read T45, this field is 45.
|
||||
• startByteAdr: this is the address of the first byte that you want to read, for example if you want to read DB1.DBW200, this is 200.
|
||||
• count: this contains how many bytes you want to read. It’s limited to 200 bytes and if you need more, you must use recursion.
|
||||
• Value[]: array of bytes to be written to the plc.
|
||||
* db: this is the address of the dataType, for example if you want to read DB1, this field is “1”; if you want to read T45, this field is 45.
|
||||
* startByteAdr: this is the address of the first byte that you want to read, for example if you want to read DB1.DBW200, this is 200.
|
||||
* count: this contains how many bytes you want to read. It’s limited to 200 bytes and if you need more, you must use recursion.
|
||||
* Value[]: array of bytes to be written to the plc.
|
||||
|
||||
Example:
|
||||
This method reads the first 200 bytes of DB1:
|
||||
var bytes = plc.ReadBytes(DataType.DataBlock, 1,0,200);
|
||||
|
||||
Example with recursion:
|
||||
|
||||
private List<byte> ReadMultipleBytes(int numBytes, int db, int startByteAdr = 0)
|
||||
{
|
||||
List<byte> resultBytes = new List<byte>();
|
||||
@@ -138,7 +156,8 @@ public object Read(DataType dataType, int db, int startByteAdr, VarType varType,
|
||||
|
||||
public ErrorCode Write(DataType dataType, int db, int startByteAdr, object value)
|
||||
|
||||
• dataType: you have to specify the memory location with the enum DataType
|
||||
* dataType: you have to specify the memory location with the enum DataType
|
||||
|
||||
public enum DataType
|
||||
{
|
||||
Input = 129,
|
||||
@@ -148,24 +167,27 @@ public enum DataType
|
||||
Timer = 29,
|
||||
Counter = 28
|
||||
}
|
||||
• db: this is the address of the dataType, for example if you want to read DB1, this field is “1”; if you want to read T45, this field is 45.
|
||||
• startByteAdr: this is the address of the first byte that you want to read, for example if you want to read DB1.DBW200, this is 200.
|
||||
• varType: specify the data that you want to get your bytes converted.
|
||||
|
||||
* db: this is the address of the dataType, for example if you want to read DB1, this field is “1”; if you want to read T45, this field is 45.
|
||||
* startByteAdr: this is the address of the first byte that you want to read, for example if you want to read DB1.DBW200, this is 200.
|
||||
* varType: specify the data that you want to get your bytes converted.
|
||||
|
||||
public enum VarType
|
||||
{
|
||||
Bit,
|
||||
Byte,
|
||||
Word,
|
||||
DWord,
|
||||
Int,
|
||||
DInt,
|
||||
Real,
|
||||
String,
|
||||
Timer,
|
||||
Counter
|
||||
Bit,
|
||||
Byte,
|
||||
Word,
|
||||
DWord,
|
||||
Int,
|
||||
DInt,
|
||||
Real,
|
||||
String,
|
||||
Timer,
|
||||
Counter
|
||||
}
|
||||
• count: this contains how many variables you want to read. It’s limited to 200 bytes and if you need more, you must use recursion.
|
||||
• Value: array of values to be written to the plc. It can be a single value or an array, the important is that the type is unique, for example array of double, array of int, array of shorts, etc..
|
||||
|
||||
* count: this contains how many variables you want to read. It’s limited to 200 bytes and if you need more, you must use recursion.
|
||||
* Value: array of values to be written to the plc. It can be a single value or an array, the important is that the type is unique, for example array of double, array of int, array of shorts, etc..
|
||||
|
||||
Example:
|
||||
This method reads the first 20 DWords of DB1:
|
||||
@@ -179,7 +201,7 @@ public object Read(string variable)
|
||||
public ErrorCode Write(string variable, object value)
|
||||
|
||||
|
||||
• variable: specify the variable to read by using strings like “DB1.DBW20”, “T45”, “C21”, “DB1.DBD400”, etc.
|
||||
* variable: specify the variable to read by using strings like “DB1.DBW20”, “T45”, “C21”, “DB1.DBD400”, etc.
|
||||
|
||||
Example:
|
||||
This reads the variable DB1.DBW0. The result must be cast to ushort to get the correct 16-bit format in C#.
|
||||
@@ -234,15 +256,16 @@ public void ReadClass(object sourceClass, int db, int startByteAdr = 0)
|
||||
|
||||
public ErrorCode WriteClass(object classValue, int db, int startByteAdr = 0)
|
||||
|
||||
• sourceClass: instance of the class that you want to assign the values
|
||||
• db: index of the DB to read
|
||||
• startByteAdr: specified the first address of the byte to read (the default is zero).
|
||||
* sourceClass: instance of the class that you want to assign the values
|
||||
* db: index of the DB to read
|
||||
* startByteAdr: specified the first address of the byte to read (the default is zero).
|
||||
Example:
|
||||
Define a DataBlock in the plc like:
|
||||
|
||||

|
||||
|
||||
Then add a struct into your .Net application that is similiar to the DB in the plc:
|
||||
|
||||
public class TestClass
|
||||
{
|
||||
public bool varBool0 { get; set;}
|
||||
@@ -267,49 +290,51 @@ public class TestClass
|
||||
}
|
||||
|
||||
then you add the code to read or write the complete struct
|
||||
|
||||
// reads a struct from DataBlock 1
|
||||
TestClass testClass = new TestClass();
|
||||
plc.ReadClass(testClass, 1);
|
||||
|
||||
|
||||
Value conversion between C# and S7 plc
|
||||
|
||||
|
||||
• Read S7 Word:
|
||||
* Read S7 Word:
|
||||
ushort result = (ushort)plc.Read("DB1.DBW0");
|
||||
|
||||
• Write S7 Word:
|
||||
* Write S7 Word:
|
||||
ushort val = 40000;
|
||||
plc.Write("DB1.DBW0", val);
|
||||
|
||||
• Read S7 Int / Dec, you need to use the method ConvertToShort():
|
||||
* Read S7 Int / Dec, you need to use the method ConvertToShort():
|
||||
short result = ((ushort)plc.Read("DB1.DBW0")).ConvertToShort();
|
||||
|
||||
• Write S7 Int / Dec, you need to use the method ConvertToUshort():
|
||||
* Write S7 Int / Dec, you need to use the method ConvertToUshort():
|
||||
short value = -100;
|
||||
plc.Write("DB1.DBW0", value.ConvertToUshort());
|
||||
|
||||
• Read S7 DWord:
|
||||
* Read S7 DWord:
|
||||
uint result = (uint)plc.Read("DB1.DBD40");
|
||||
|
||||
• Write S7 DWord:
|
||||
* Write S7 DWord:
|
||||
uint val = 1000;
|
||||
plc.Write("DB1.DBD40", val);
|
||||
|
||||
• Read S7 Dint, you need to use ConvertToInt():
|
||||
* Read S7 Dint, you need to use ConvertToInt():
|
||||
int result2 = ((uint)plc.Read("DB1.DBD60")).ConvertToInt();
|
||||
|
||||
• Write S7 Dint:
|
||||
* Write S7 Dint:
|
||||
int value = -60000;
|
||||
plc.Write("DB1.DBD60", value);
|
||||
• Read S7 Real, you need to use ConvertToDouble():
|
||||
|
||||
* Read S7 Real, you need to use ConvertToDouble():
|
||||
double result = ((uint)plc.Read("DB1.DBD40")).ConvertToDouble();
|
||||
|
||||
• Write S7 Real, you need to use ConvertToInt():
|
||||
* Write S7 Real, you need to use ConvertToInt():
|
||||
double val = 35.687;
|
||||
plc.Write("DB1.DBD40", val.ConvertToUInt());
|
||||
|
||||
• Read bool from byte
|
||||
* Read bool from byte
|
||||
|
||||
byte myByte = 5; // 0000 0101
|
||||
myByte.SelectBit(0) // true
|
||||
myByte.SelectBit(1) // false
|
||||
Reference in New Issue
Block a user