diff --git a/Home.md b/Home.md index 463e20d..a81ca6b 100644 --- a/Home.md +++ b/Home.md @@ -29,13 +29,13 @@ public enum CpuType { S71500 = 40, } ``` -* Ip: this contains the IP address of the CPU of external Ethernet card +* Ip: specify the IP address of the CPU or of the 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: +This code creates a Plc object for a S7-300 plc at the IP address 127.0.0.1, for a plc in rack 0 with the cpu in slot 2: ``` Plc plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2); ``` @@ -60,7 +60,7 @@ 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. +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 @@ -79,7 +79,7 @@ public enum ErrorCode ## Global error handling -Not all methods returns an error. You can check for +Not all methods returns an error. To check that a read or a write operation was successful, you can use: ``` public ErrorCode LastErrorCode ``` @@ -87,24 +87,24 @@ 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 +To check if the plc is available (the IP address is reachable with a ping) 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. +When you check this property, the driver will try to connect to the plc and returns true if it can connect, false otherwise. ## 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: +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: ``` public bool IsConnected ``` -This property can be checked after you called the method Open(), 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.   ## Read bytes / Write bytes @@ -115,7 +115,7 @@ 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. +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 ``` @@ -129,9 +129,9 @@ public enum DataType 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. +* 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. +* count: contains how many bytes you want to read. * Value[]: array of bytes to be written to the plc. Example: @@ -139,29 +139,10 @@ This method reads the first 200 bytes of DB1: ``` var bytes = plc.ReadBytes(DataType.DataBlock, 1,0,200); ``` -Example with recursion: -``` -private List ReadMultipleBytes(int numBytes, int db, int startByteAdr = 0) -{ - List resultBytes = new List(); - int index = startByteAdr; - while (numBytes > 0) - { - var maxToRead = (int)Math.Min(numBytes, 200); - byte[] bytes = ReadBytes (DataType.DataBlock, db, index, (int)maxToRead); - if (bytes == null) - return new List(); - resultBytes.AddRange(bytes); - numBytes -= maxToRead; - index += maxToRead; - } - return resultBytes; -} -``` ## 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). This is also limited to maximum 200 bytes. 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. ``` public object Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount) @@ -179,8 +160,8 @@ public enum DataType 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. +* 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. ``` public enum VarType @@ -197,16 +178,18 @@ public enum VarType 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: contains how many variables you want to read. +* Value: array of values to be written to the plc. It can be a single value or an array, just remember 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: +``` 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, this 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. ``` public object Read(string variable) @@ -259,7 +242,7 @@ public struct testStruct public UInt32 varDWord; } ``` -then you add the code to read or write the complete struct +then add the code to read or write the complete struct ``` // reads a struct from DataBlock 1 testStruct test = (testStruct)plc.ReadStruct(typeof(testStruct), 1); @@ -305,7 +288,7 @@ public class TestClass public UInt32 varDWord { get; set;} } ``` -then you add the code to read or write the complete struct +then add the code to read or write the complete struct ``` // reads a struct from DataBlock 1 TestClass testClass = new TestClass();