mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2025-12-18 01:16:50 +08:00
Updated Home (markdown)
44
Home.md
44
Home.md
@@ -50,11 +50,11 @@ plc.Open();
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Disconnecting from the PLC
|
## Disconnecting from the PLC
|
||||||
```
|
```csharp
|
||||||
public void Close()
|
public void Close()
|
||||||
```
|
```
|
||||||
For example this closes the connection:
|
For example this closes the connection:
|
||||||
```
|
```csharp
|
||||||
plc.Close();
|
plc.Close();
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ plc.Close();
|
|||||||
|
|
||||||
The Open() method returns an ErrorCode to check if the operation was successful. You should always check that it returns ErrorCode.NoError.
|
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:
|
These are the types of errors:
|
||||||
```
|
```csharp
|
||||||
public enum ErrorCode
|
public enum ErrorCode
|
||||||
{
|
{
|
||||||
NoError = 0,
|
NoError = 0,
|
||||||
@@ -80,11 +80,11 @@ public enum ErrorCode
|
|||||||
## Global error handling
|
## Global error handling
|
||||||
|
|
||||||
Not all methods returns an error. To check that a read or a write operation was successful, you can use:
|
Not all methods returns an error. To check that a read or a write operation was successful, you can use:
|
||||||
```
|
```csharp
|
||||||
public ErrorCode LastErrorCode
|
public ErrorCode LastErrorCode
|
||||||
```
|
```
|
||||||
and
|
and
|
||||||
```
|
```csharp
|
||||||
public string LastErrorString
|
public string LastErrorString
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -92,7 +92,7 @@ public string LastErrorString
|
|||||||
## Check PLC availability
|
## Check PLC availability
|
||||||
|
|
||||||
To check if the plc is available (the IP address is reachable with a ping) you can use the property
|
To check if the plc is available (the IP address is reachable with a ping) you can use the property
|
||||||
```
|
```csharp
|
||||||
public bool IsAvailable
|
public bool IsAvailable
|
||||||
```
|
```
|
||||||
When you check this property, the driver will try to connect to the plc and returns true if it can connect, false otherwise.
|
When you check this property, the driver will try to connect to the plc and returns true if it can connect, false otherwise.
|
||||||
@@ -101,7 +101,7 @@ When you check this property, the driver will try to connect to the plc and retu
|
|||||||
|
|
||||||
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 at the other side of the socket.
|
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 at the other side of the socket.
|
||||||
The property that you have to check in this case is:
|
The property that you have to check in this case is:
|
||||||
```
|
```csharp
|
||||||
public bool IsConnected
|
public bool IsConnected
|
||||||
```
|
```
|
||||||
This property can be checked after you called the method Open() and the result was a success, to check if the connection is still alive.
|
This property can be checked after you called the method Open() and the result was a success, to check if the connection is still alive.
|
||||||
@@ -110,7 +110,7 @@ This property can be checked after you called the method Open() and the result w
|
|||||||
## Read bytes / Write bytes
|
## Read bytes / Write bytes
|
||||||
|
|
||||||
The library offers several methods to read variables. The basic one and the most used is ReadBytes.
|
The library offers several methods to read variables. The basic one and the most used is ReadBytes.
|
||||||
```
|
```csharp
|
||||||
public byte[] ReadBytes(DataType dataType, int db, int startByteAdr, int count)
|
public byte[] ReadBytes(DataType dataType, int db, int startByteAdr, int count)
|
||||||
|
|
||||||
public ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value)
|
public ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value)
|
||||||
@@ -118,7 +118,7 @@ public ErrorCode WriteBytes(DataType dataType, int db, int startByteAdr, byte[]
|
|||||||
This reads all the bytes you specify from a given memory location. This method handles multiple requests automatically in case the number of bytes exceeds the maximum bytes that can be transferred in a single request.
|
This reads all the bytes you specify from a given memory location. This method handles multiple requests automatically in case the number of bytes exceeds the maximum bytes that can be transferred in a single request.
|
||||||
|
|
||||||
* dataType: you have to specify the memory location with the enum DataType
|
* dataType: you have to specify the memory location with the enum DataType
|
||||||
```
|
```csharp
|
||||||
public enum DataType
|
public enum DataType
|
||||||
{
|
{
|
||||||
Input = 129,
|
Input = 129,
|
||||||
@@ -136,20 +136,20 @@ public enum DataType
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
This method reads the first 200 bytes of DB1:
|
This method reads the first 200 bytes of DB1:
|
||||||
```
|
```csharp
|
||||||
var bytes = plc.ReadBytes(DataType.DataBlock, 1,0,200);
|
var bytes = plc.ReadBytes(DataType.DataBlock, 1,0,200);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Read and decode / Write decoded
|
## Read and decode / Write decoded
|
||||||
|
|
||||||
This method permits to read and receive an already decoded result based on the varType provided. This is useful if you read several fields of the same type (for example 20 consecutive DBW). If you specify VarType.Byte, it has the same functionality as ReadBytes.
|
This method permits to read and receive an already decoded result based on the varType provided. This is useful if you read several fields of the same type (for example 20 consecutive DBW). If you specify VarType.Byte, it has the same functionality as ReadBytes.
|
||||||
```
|
```csharp
|
||||||
public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount)
|
public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount)
|
||||||
|
|
||||||
public ErrorCode Write(DataType dataType, int db, int startByteAdr, object value)
|
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
|
||||||
```
|
```csharp
|
||||||
public enum DataType
|
public enum DataType
|
||||||
{
|
{
|
||||||
Input = 129,
|
Input = 129,
|
||||||
@@ -163,7 +163,7 @@ public enum DataType
|
|||||||
* db: 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.
|
* db: 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: the address of the first byte that you want to read, for example if you want to read DB1.DBW200, this is 200.
|
* startByteAdr: 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.
|
* varType: specify the data that you want to get your bytes converted.
|
||||||
```
|
```csharp
|
||||||
public enum VarType
|
public enum VarType
|
||||||
{
|
{
|
||||||
Bit,
|
Bit,
|
||||||
@@ -183,14 +183,14 @@ public enum VarType
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
This method reads the first 20 DWords of DB1:
|
This method reads the first 20 DWords of DB1:
|
||||||
```
|
```csharp
|
||||||
var dwords = plc.Read(DataType.DataBlock, 1,0, VarType.DWord, 20);
|
var dwords = plc.Read(DataType.DataBlock, 1,0, VarType.DWord, 20);
|
||||||
|
|
||||||
```
|
```
|
||||||
## Read a single variable / Write a single variable
|
## Read a single variable / Write a single variable
|
||||||
|
|
||||||
This method reads a single variable from the plc, by parsing the string and returning the correct result. While this is the easiest method to get started, is very inefficient because the driver sends a TCP request for every variable.
|
This method reads a single variable from the plc, by parsing the string and returning the correct result. While this is the easiest method to get started, is very inefficient because the driver sends a TCP request for every variable.
|
||||||
```
|
```csharp
|
||||||
public object Read(string variable)
|
public object Read(string variable)
|
||||||
|
|
||||||
public ErrorCode Write(string variable, object value)
|
public ErrorCode Write(string variable, object value)
|
||||||
@@ -200,14 +200,14 @@ public ErrorCode Write(string variable, object value)
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
This reads the variable DB1.DBW0. The result must be cast to ushort to get the correct 16-bit format in C#.
|
This reads the variable DB1.DBW0. The result must be cast to ushort to get the correct 16-bit format in C#.
|
||||||
```
|
```csharp
|
||||||
ushort result = (ushort)plc.Read("DB1.DBW0");
|
ushort result = (ushort)plc.Read("DB1.DBW0");
|
||||||
```
|
```
|
||||||
|
|
||||||
## Read a struct / Write a struct
|
## Read a struct / Write a struct
|
||||||
|
|
||||||
This method reads all the bytes from a specified DB needed to fill a struct in C#, and returns the struct that contains the values.
|
This method reads all the bytes from a specified DB needed to fill a struct in C#, and returns the struct that contains the values.
|
||||||
```
|
```csharp
|
||||||
public object ReadStruct(Type structType, int db, int startByteAdr = 0)
|
public object ReadStruct(Type structType, int db, int startByteAdr = 0)
|
||||||
|
|
||||||
public ErrorCode WriteStruct(object structValue, int db, int startByteAdr = 0)
|
public ErrorCode WriteStruct(object structValue, int db, int startByteAdr = 0)
|
||||||
@@ -222,7 +222,7 @@ Define a DataBlock in the plc like:
|
|||||||

|

|
||||||
|
|
||||||
Then add a struct into your .Net application that is similiar to the DB in the plc:
|
Then add a struct into your .Net application that is similiar to the DB in the plc:
|
||||||
```
|
```csharp
|
||||||
public struct testStruct
|
public struct testStruct
|
||||||
{
|
{
|
||||||
public bool varBool0;
|
public bool varBool0;
|
||||||
@@ -243,14 +243,14 @@ public struct testStruct
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
then add the code to read or write the complete struct
|
then add the code to read or write the complete struct
|
||||||
```
|
```csharp
|
||||||
// reads a struct from DataBlock 1
|
// reads a struct from DataBlock 1
|
||||||
testStruct test = (testStruct)plc.ReadStruct(typeof(testStruct), 1);
|
testStruct test = (testStruct)plc.ReadStruct(typeof(testStruct), 1);
|
||||||
```
|
```
|
||||||
## Read a class / Write a class
|
## Read a class / Write a class
|
||||||
|
|
||||||
This method reads all the bytes from a specified DB needed to fill a class in C#. The class is passed as reference and values are assigned by using reflection.
|
This method reads all the bytes from a specified DB needed to fill a class in C#. The class is passed as reference and values are assigned by using reflection.
|
||||||
```
|
```csharp
|
||||||
public void ReadClass(object sourceClass, int db, int startByteAdr = 0)
|
public void ReadClass(object sourceClass, int db, int startByteAdr = 0)
|
||||||
|
|
||||||
public ErrorCode WriteClass(object classValue, int db, int startByteAdr = 0)
|
public ErrorCode WriteClass(object classValue, int db, int startByteAdr = 0)
|
||||||
@@ -264,7 +264,7 @@ Define a DataBlock in the plc like:
|
|||||||

|

|
||||||
|
|
||||||
Then add a struct into your .Net application that is similiar to the DB in the plc:
|
Then add a struct into your .Net application that is similiar to the DB in the plc:
|
||||||
```
|
```csharp
|
||||||
public class TestClass
|
public class TestClass
|
||||||
{
|
{
|
||||||
public bool varBool0 { get; set;}
|
public bool varBool0 { get; set;}
|
||||||
@@ -289,7 +289,7 @@ public class TestClass
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
then add the code to read or write the complete struct
|
then add the code to read or write the complete struct
|
||||||
```
|
```csharp
|
||||||
// reads a struct from DataBlock 1
|
// reads a struct from DataBlock 1
|
||||||
TestClass testClass = new TestClass();
|
TestClass testClass = new TestClass();
|
||||||
plc.ReadClass(testClass, 1);
|
plc.ReadClass(testClass, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user