S7.Net
Conversion methods to convert from Siemens numeric format to C# and back
Converts a binary string to Int32 value
Converts a binary string to a byte. Can return null.
Converts the value to a binary string
Helper to get a bit value given a byte and the bit index.
Example: DB1.DBX0.5 -> var bytes = ReadBytes(DB1.DBW0); bool bit = bytes[0].SelectBit(5);
Converts from ushort value to short value; it's used to retrieve negative values from words
Converts from short value to ushort value; it's used to pass negative values to DWs
Converts from UInt32 value to Int32 value; it's used to retrieve negative values from DBDs
Converts from Int32 value to UInt32 value; it's used to pass negative values to DBDs
Converts from double to DWord (DBD)
Converts from DWord (DBD) to double
COTP Protocol functions and types
Describes a COTP TPDU (Transport protocol data unit)
Reads COTP TPDU (Transport protocol data unit) from the network stream
See: https://tools.ietf.org/html/rfc905
The socket to read from
COTP DPDU instance
Describes a COTP TSDU (Transport service data unit). One TSDU consist of 1 ore more TPDUs
Reads the full COTP TSDU (Transport service data unit)
See: https://tools.ietf.org/html/rfc905
Data in TSDU
Types of S7 cpu supported by the library
S7 200 cpu type
S7 300 cpu type
S7 400 cpu type
S7 1200 cpu type
S7 1500 cpu type
Types of error code that can be set after a function is called
The function has been executed correctly
Wrong type of CPU error
Connection error
Ip address not available
Wrong format of the variable
Wrong number of received bytes
Error on send data
Error on read data
Error on write data
Types of memory area that can be read
Input area memory
Output area memory
Merkers area memory (M0, M0.0, ...)
DB area memory (DB1, DB2, ...)
Timer area memory(T1, T2, ...)
Counter area memory (C1, C2, ...)
Types
S7 Bit variable type (bool)
S7 Byte variable type (8 bits)
S7 Word variable type (16 bits, 2 bytes)
S7 DWord variable type (32 bits, 4 bytes)
S7 Int variable type (16 bits, 2 bytes)
DInt variable type (32 bits, 4 bytes)
Real variable type (32 bits, 4 bytes)
String variable type (variable)
String variable type (variable)
Timer variable type
Counter variable type
Creates an instance of S7.Net driver
IP address of the PLC
CPU type of the PLC
Rack of the PLC
Slot of the CPU of the PLC
Max PDU size this cpu supports
Returns true if a connection to the PLC can be established
Checks if the socket is connected and polls the other peer (the PLC) to see if it's connected.
This is the variable that you should continously check to see if the communication is working
See also: http://stackoverflow.com/questions/2661764/how-to-check-if-a-socket-is-connected-disconnected-in-c
Contains the last error registered when executing a function
Contains the last error code registered when executing a function
Creates a PLC object with all the parameters needed for connections.
For S7-1200 and S7-1500, the default is rack = 0 and slot = 0.
You need slot > 0 if you are connecting to external ethernet card (CP).
For S7-300 and S7-400 the default is rack = 0 and slot = 2.
CpuType of the PLC (select from the enum)
Ip address of the PLC
rack of the PLC, usually it's 0, but check in the hardware configuration of Step7 or TIA portal
slot of the CPU of the PLC, usually it's 2 for S7300-S7400, 0 for S7-1200 and S7-1500.
If you use an external ethernet card, this must be set accordingly.
Open a and connects to the PLC, sending all the corrected package
and returning if the connection was successful () of it was wrong.
Returns ErrorCode.NoError if the connection was successful, otherwise check the ErrorCode
Disonnects from the PLC and close the socket
Reads multiple vars in a single request.
You have to create and pass a list of DataItems and you obtain in response the same list with the values.
Values are stored in the property "Value" of the dataItem and are already converted.
If you don't want the conversion, just create a dataItem of bytes.
DataItems must not be more than 20 (protocol restriction) and bytes must not be more than 200 + 22 of header (protocol restriction).
List of dataitems that contains the list of variables that must be read. Maximum 20 dataitems are accepted.
Reads a number of bytes from a DB starting from a specified index. This handles more than 200 bytes with multiple requests.
If the read was not successful, check LastErrorCode or LastErrorString.
Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.
Start byte address. If you want to read DB1.DBW200, this is 200.
Byte count, if you want to read 120 bytes, set this to 120.
Returns the bytes in an array
Read and decode a certain number of bytes of the "VarType" provided.
This can be used to read multiple consecutive variables of the same type (Word, DWord, Int, etc).
If the read was not successful, check LastErrorCode or LastErrorString.
Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.
Start byte address. If you want to read DB1.DBW200, this is 200.
Type of the variable/s that you are reading
Address of bit. If you want to read DB1.DBX200.6, set 6 to this parameter.
Reads a single variable from the PLC, takes in input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.
If the read was not successful, check LastErrorCode or LastErrorString.
Input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.
Returns an object that contains the value. This object must be cast accordingly.
Reads all the bytes needed to fill a struct in C#, starting from a certain address, and return an object that can be casted to the struct.
Type of the struct to be readed (es.: TypeOf(MyStruct)).
Address of the DB.
Start byte address. If you want to read DB1.DBW200, this is 200.
Returns a struct that must be cast.
Reads all the bytes needed to fill a struct in C#, starting from a certain address, and returns the struct or null if nothing was read.
The struct type
Address of the DB.
Start byte address. If you want to read DB1.DBW200, this is 200.
Returns a nulable struct. If nothing was read null will be returned.
Reads all the bytes needed to fill a class in C#, starting from a certain address, and set all the properties values to the value that are read from the PLC.
This reads only properties, it doesn't read private variable or public variable without {get;set;} specified.
Instance of the class that will store the values
Index of the DB; es.: 1 is for DB1
Start byte address. If you want to read DB1.DBW200, this is 200.
The number of read bytes
Reads all the bytes needed to fill a class in C#, starting from a certain address, and set all the properties values to the value that are read from the PLC.
This reads only properties, it doesn't read private variable or public variable without {get;set;} specified. To instantiate the class defined by the generic
type, the class needs a default constructor.
The class that will be instantiated. Requires a default constructor
Index of the DB; es.: 1 is for DB1
Start byte address. If you want to read DB1.DBW200, this is 200.
An instance of the class with the values read from the PLC. If no data has been read, null will be returned
Reads all the bytes needed to fill a class in C#, starting from a certain address, and set all the properties values to the value that are read from the PLC.
This reads only properties, it doesn't read private variable or public variable without {get;set;} specified.
The class that will be instantiated
Function to instantiate the class
Index of the DB; es.: 1 is for DB1
Start byte address. If you want to read DB1.DBW200, this is 200.
An instance of the class with the values read from the PLC. If no data has been read, null will be returned
Write a number of bytes from a DB starting from a specified index. This handles more than 200 bytes with multiple requests.
If the write was not successful, check LastErrorCode or LastErrorString.
Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.
Start byte address. If you want to write DB1.DBW200, this is 200.
Bytes to write. If more than 200, multiple requests will be made.
NoError if it was successful, or the error is specified
Write a single bit from a DB with the specified index.
Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.
Start byte address. If you want to write DB1.DBW200, this is 200.
The address of the bit. (0-7)
Bytes to write. If more than 200, multiple requests will be made.
NoError if it was successful, or the error is specified
Write a single bit from a DB with the specified index.
Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.
Start byte address. If you want to write DB1.DBW200, this is 200.
The address of the bit. (0-7)
Bytes to write. If more than 200, multiple requests will be made.
NoError if it was successful, or the error is specified
Takes in input an object and tries to parse it to an array of values. This can be used to write many data, all of the same type.
You must specify the memory area type, memory are address, byte start address and bytes count.
If the read was not successful, check LastErrorCode or LastErrorString.
Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.
Start byte address. If you want to read DB1.DBW200, this is 200.
Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion.
The address of the bit. (0-7)
NoError if it was successful, or the error is specified
Writes a single variable from the PLC, takes in input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.
If the write was not successful, check or .
Input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.
Value to be written to the PLC
NoError if it was successful, or the error is specified
Writes a C# struct to a DB in the PLC
The struct to be written
Db address
Start bytes on the PLC
NoError if it was successful, or the error is specified
Writes a C# class to a DB in the PLC
The class to be written
Db address
Start bytes on the PLC
NoError if it was successful, or the error is specified
Sets the to and to .
Creates the header to read bytes from the PLC
Create the bytes-package to request data from the PLC. You have to specify the memory type (dataType),
the address of the memory, the address of the byte and the bytes count.
MemoryType (DB, Timer, Counter, etc.)
Address of the memory to be read
Start address of the byte
Number of bytes to be read
Writes up to 200 bytes to the PLC and returns NoError if successful. You must specify the memory area type, memory are address, byte start address and bytes count.
If the write was not successful, check LastErrorCode or LastErrorString.
Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
Address of the memory area (if you want to read DB1, this is set to 1). This must be set also for other memory area types: counters, timers,etc.
Start byte address. If you want to read DB1.DBW200, this is 200.
Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion.
NoError if it was successful, or the error is specified
Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format.
Given a S7 (Bool, Word, DWord, etc.), it returns how many bytes to read.
Byte lenght of variable
Releases all resources, disonnects from the PLC and closes the
Releases all resources, disonnects from the PLC and closes the
Describes a TPKT Packet
Reds a TPKT from the socket
The socket to read from
TPKT Instace
Contains the conversion methods to convert Bit from S7 plc to C#.
Converts a Bit to bool
Converts an array of bytes to a BitArray
Contains the methods to read, set and reset bits inside bytes
Returns the value of a bit in a bit, given the address of the bit
Sets the value of a bit to 1 (true), given the address of the bit
Resets the value of a bit to 0 (false), given the address of the bit
Contains the methods to convert from bytes to byte arrays
Converts a byte to byte array
Converts a byte array to byte
Contains the methods to convert a C# class to S7 data types
Gets the size of the class in bytes.
An instance of the class
the number of bytes
Sets the object's values with the given array of bytes
The object to fill in the given array of bytes
The array of bytes
Creates a byte array depending on the struct type.
The struct object
A byte array or null if fails.
Contains the conversion methods to convert Counter from S7 plc to C# ushort (UInt16).
Converts a Counter (2 bytes) to ushort (UInt16)
Converts a ushort (UInt16) to word (2 bytes)
Converts an array of ushort (UInt16) to an array of bytes
Converts an array of bytes to an array of ushort
Create an instance of a memory block that can be read by using ReadMultipleVars
Memory area to read
Type of data to be read (default is bytes)
Address of memory area to read (example: for DB1 this value is 1, for T45 this value is 45)
Address of the first byte to read
Addess of bit to read from StartByteAdr
Number of variables to read
Contains the value of the memory area after the read has been executed
Create an instance of DataItem
Contains the conversion methods to convert DInt from S7 plc to C# int (Int32).
Converts a S7 DInt (4 bytes) to int (Int32)
Converts a int (Int32) to S7 DInt (4 bytes)
Converts an array of int (Int32) to an array of bytes
Converts an array of S7 DInt to an array of int (Int32)
Contains the conversion methods to convert Real from S7 plc to C# double.
Converts a S7 Real (4 bytes) to double
Converts a S7 DInt to double
Converts a S7 DWord to double
Converts a double to S7 Real (4 bytes)
Converts an array of double to an array of bytes
Converts an array of S7 Real to an array of double
Contains the conversion methods to convert DWord from S7 plc to C#.
Converts a S7 DWord (4 bytes) to uint (UInt32)
Converts 4 bytes to DWord (UInt32)
Converts a uint (UInt32) to S7 DWord (4 bytes)
Converts an array of uint (UInt32) to an array of S7 DWord (4 bytes)
Converts an array of S7 DWord to an array of uint (UInt32)
Contains the conversion methods to convert Int from S7 plc to C#.
Converts a S7 Int (2 bytes) to short (Int16)
Converts a short (Int16) to a S7 Int byte array (2 bytes)
Converts an array of short (Int16) to a S7 Int byte array (2 bytes)
Converts an array of S7 Int to an array of short (Int16)
Converts a C# int value to a C# short value, to be used as word.
Contains the methods to convert from S7 strings to C# strings
there are two kinds how strings a send. This one is with a pre of two bytes
they contain the length of the string
Converts S7 bytes to a string
Contains the methods to convert from S7 strings to C# strings
Converts a string to S7 bytes
Converts S7 bytes to a string
Contains the method to convert a C# struct to S7 data types
Gets the size of the struct in bytes.
the type of the struct
the number of bytes
Creates a struct of a specified type by an array of bytes.
The struct type
The array of bytes
The object depending on the struct type or null if fails(array-length != struct-length
Creates a byte array depending on the struct type.
The struct object
A byte array or null if fails.
Converts the Timer data type to C# data type
Converts the timer bytes to a double
Converts a ushort (UInt16) to an array of bytes formatted as time
Converts an array of ushorts (Uint16) to an array of bytes formatted as time
Converts an array of bytes formatted as time to an array of doubles
Contains the conversion methods to convert Words from S7 plc to C#.
Converts a word (2 bytes) to ushort (UInt16)
Converts 2 bytes to ushort (UInt16)
Converts a ushort (UInt16) to word (2 bytes)
Converts an array of ushort (UInt16) to an array of bytes
Converts an array of bytes to an array of ushort