Updated Home (markdown)

Michele Cattafesta
2017-08-31 22:29:06 +01:00
parent 1be9f7bbfa
commit 23905d1bf8

44
Home.md

@@ -50,11 +50,11 @@ plc.Open();
```
## Disconnecting from the PLC
```
```csharp
public void Close()
```
For example this closes the connection:
```
```csharp
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.
These are the types of errors:
```
```csharp
public enum ErrorCode
{
NoError = 0,
@@ -80,11 +80,11 @@ public enum ErrorCode
## Global error handling
Not all methods returns an error. To check that a read or a write operation was successful, you can use:
```
```csharp
public ErrorCode LastErrorCode
```
and
```
```csharp
public string LastErrorString
```
@@ -92,7 +92,7 @@ public string LastErrorString
## Check PLC availability
To check if the plc is available (the IP address is reachable with a ping) you can use the property
```
```csharp
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.
@@ -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.
The property that you have to check in this case is:
```
```csharp
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.
@@ -110,7 +110,7 @@ This property can be checked after you called the method Open() and the result w
## Read bytes / Write bytes
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 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.
* dataType: you have to specify the memory location with the enum DataType
```
```csharp
public enum DataType
{
Input = 129,
@@ -136,20 +136,20 @@ public enum DataType
Example:
This method reads the first 200 bytes of DB1:
```
```csharp
var bytes = plc.ReadBytes(DataType.DataBlock, 1,0,200);
```
## 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.
```
```csharp
public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount)
public ErrorCode Write(DataType dataType, int db, int startByteAdr, object value)
```
* dataType: you have to specify the memory location with the enum DataType
```
```csharp
public enum DataType
{
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.
* 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.
```
```csharp
public enum VarType
{
Bit,
@@ -183,14 +183,14 @@ public enum VarType
Example:
This method reads the first 20 DWords of DB1:
```
```csharp
var dwords = plc.Read(DataType.DataBlock, 1,0, VarType.DWord, 20);
```
## 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.
```
```csharp
public object Read(string variable)
public ErrorCode Write(string variable, object value)
@@ -200,14 +200,14 @@ public ErrorCode Write(string variable, object value)
Example:
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");
```
## 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.
```
```csharp
public object ReadStruct(Type structType, 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:
![struct](https://github.com/killnine/s7netplus/blob/master/Documentation/struct.png)
Then add a struct into your .Net application that is similiar to the DB in the plc:
```
```csharp
public struct testStruct
{
public bool varBool0;
@@ -243,14 +243,14 @@ public struct testStruct
}
```
then add the code to read or write the complete struct
```
```csharp
// reads a struct from DataBlock 1
testStruct test = (testStruct)plc.ReadStruct(typeof(testStruct), 1);
```
## 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.
```
```csharp
public void ReadClass(object sourceClass, 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:
![struct](https://github.com/killnine/s7netplus/blob/master/Documentation/struct.png)
Then add a struct into your .Net application that is similiar to the DB in the plc:
```
```csharp
public class TestClass
{
public bool varBool0 { get; set;}
@@ -289,7 +289,7 @@ public class TestClass
}
```
then add the code to read or write the complete struct
```
```csharp
// reads a struct from DataBlock 1
TestClass testClass = new TestClass();
plc.ReadClass(testClass, 1);