C# Class S7.Net.Plc

Creates an instance of S7.Net driver
Inheritance: IDisposable
ファイルを表示 Open project: killnine/s7netplus Class Usage Examples

Public Methods

Method Description
ClearLastError ( ) : void

Sets the LastErrorCode to NoError and LastErrorString to String.Empty

Close ( ) : void

Disonnects from the plc and close the socket

Dispose ( ) : void

Releases all resources, disonnects from the plc and closes the socket

Open ( ) : ErrorCode

Open a socket and connects to the plc, sending all the corrected package and returning if the connection was successful (ErroreCode.NoError) of it was wrong.

Plc ( CpuType cpu, string ip, Int16 rack, Int16 slot ) : System

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.

Read ( DataType dataType, int db, int startByteAdr, VarType varType, int varCount ) : object

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.

Read ( string variable ) : object

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.

ReadBytes ( DataType dataType, int db, int startByteAdr, int count ) : byte[]

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.

ReadClass ( object sourceClass, int db, int startByteAdr ) : void

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 ony properties, it doesn't read private variable or public variable without {get;set;} specified.

ReadMultipleVars ( List dataItems ) : void

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).

ReadStruct ( Type structType, int db, int startByteAdr ) : object

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.

Write ( DataType dataType, int db, int startByteAdr, object value ) : ErrorCode

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.

Write ( string variable, object value ) : ErrorCode

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 LastErrorCode or LastErrorString.

WriteBytes ( DataType dataType, int db, int startByteAdr, byte value ) : ErrorCode

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.

WriteClass ( object classValue, int db, int startByteAdr ) : ErrorCode

Writes a C# class to a DB in the plc

WriteStruct ( object structValue, int db, int startByteAdr ) : ErrorCode

Writes a C# struct to a DB in the plc

Private Methods

Method Description
CreateReadDataRequestPackage ( DataType dataType, int db, int startByteAdr, int count = 1 ) : ByteArray

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.

ParseBytes ( VarType varType, byte bytes, int varCount ) : object

Given a S7 variable type (Bool, Word, DWord, etc.), it converts the bytes in the appropriate C# format.

ReadBytesWithASingleRequest ( DataType dataType, int db, int startByteAdr, int count ) : byte[]
ReadHeaderPackage ( int amount = 1 ) : ByteArray

Creates the header to read bytes from the plc

VarTypeToByteLength ( VarType varType, int varCount = 1 ) : int

Given a S7 variable type (Bool, Word, DWord, etc.), it returns how many bytes to read.

WriteBytesWithASingleRequest ( DataType dataType, int db, int startByteAdr, byte value ) : ErrorCode

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.

Method Details

ClearLastError() public method

Sets the LastErrorCode to NoError and LastErrorString to String.Empty
public ClearLastError ( ) : void
return void

Close() public method

Disonnects from the plc and close the socket
public Close ( ) : void
return void

Dispose() public method

Releases all resources, disonnects from the plc and closes the socket
public Dispose ( ) : void
return void

Open() public method

Open a socket and connects to the plc, sending all the corrected package and returning if the connection was successful (ErroreCode.NoError) of it was wrong.
public Open ( ) : ErrorCode
return ErrorCode

Plc() public method

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.
public Plc ( CpuType cpu, string ip, Int16 rack, Int16 slot ) : System
cpu CpuType CpuType of the plc (select from the enum)
ip string Ip address of the plc
rack System.Int16 rack of the plc, usually it's 0, but check in the hardware configuration of Step7 or TIA portal
slot System.Int16 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.
return System

Read() public method

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.
public Read ( DataType dataType, int db, int startByteAdr, VarType varType, int varCount ) : object
dataType DataType Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
db int 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.
startByteAdr int Start byte address. If you want to read DB1.DBW200, this is 200.
varType VarType Type of the variable/s that you are reading
varCount int
return object

Read() public method

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.
public Read ( string variable ) : object
variable string Input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.
return object

ReadBytes() public method

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.
public ReadBytes ( DataType dataType, int db, int startByteAdr, int count ) : byte[]
dataType DataType Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
db int 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.
startByteAdr int Start byte address. If you want to read DB1.DBW200, this is 200.
count int Byte count, if you want to read 120 bytes, set this to 120.
return byte[]

ReadClass() public method

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 ony properties, it doesn't read private variable or public variable without {get;set;} specified.
public ReadClass ( object sourceClass, int db, int startByteAdr ) : void
sourceClass object Instance of the class that will store the values
db int Index of the DB; es.: 1 is for DB1
startByteAdr int Start byte address. If you want to read DB1.DBW200, this is 200.
return void

ReadMultipleVars() public method

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).
public ReadMultipleVars ( List dataItems ) : void
dataItems List List of dataitems that contains the list of variables that must be read. Maximum 20 dataitems are accepted.
return void

ReadStruct() public method

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.
public ReadStruct ( Type structType, int db, int startByteAdr ) : object
structType System.Type Type of the struct to be readed (es.: TypeOf(MyStruct)).
db int Address of the DB.
startByteAdr int Start byte address. If you want to read DB1.DBW200, this is 200.
return object

Write() public method

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.
public Write ( DataType dataType, int db, int startByteAdr, object value ) : ErrorCode
dataType DataType Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
db int 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.
startByteAdr int Start byte address. If you want to read DB1.DBW200, this is 200.
value object Bytes to write. The lenght of this parameter can't be higher than 200. If you need more, use recursion.
return ErrorCode

Write() public method

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 LastErrorCode or LastErrorString.
public Write ( string variable, object value ) : ErrorCode
variable string Input strings like "DB1.DBX0.0", "DB20.DBD200", "MB20", "T45", etc.
value object Value to be written to the plc
return ErrorCode

WriteBytes() public method

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.
public WriteBytes ( DataType dataType, int db, int startByteAdr, byte value ) : ErrorCode
dataType DataType Data type of the memory area, can be DB, Timer, Counter, Merker(Memory), Input, Output.
db int 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.
startByteAdr int Start byte address. If you want to write DB1.DBW200, this is 200.
value byte Bytes to write. If more than 200, multiple requests will be made.
return ErrorCode

WriteClass() public method

Writes a C# class to a DB in the plc
public WriteClass ( object classValue, int db, int startByteAdr ) : ErrorCode
classValue object The class to be written
db int Db address
startByteAdr int Start bytes on the plc
return ErrorCode

WriteStruct() public method

Writes a C# struct to a DB in the plc
public WriteStruct ( object structValue, int db, int startByteAdr ) : ErrorCode
structValue object The struct to be written
db int Db address
startByteAdr int Start bytes on the plc
return ErrorCode