C# Class ENDA.PLCNetLib.PLC

Represents a PLC device.

Please note that all of the functions except the ones beginning with Begin* and End* are blocking and calling these functions will block until a response is received from the PLC device. If there's a communication problem your application will block (freeze) until a timeout occur.

You can either use blocking methods in a separate thread or use advanced asynchronous methods for a non blocking usage.

Datei anzeigen Open project: engina/PLCNetLib Class Usage Examples

Public Properties

Property Type Description
IP ENDA.PLCNetLib.Accessors.BoolArrayAccessor
MB ENDA.PLCNetLib.Accessors.BoolArrayAccessor
MF ENDA.PLCNetLib.Accessors.FloatArrayAccessor
MI ENDA.PLCNetLib.Accessors.Int32ArrayAccessor
MW ENDA.PLCNetLib.Accessors.UInt16ArrayAccessor
QP ENDA.PLCNetLib.Accessors.BoolArrayAccessor

Private Properties

Property Type Description
IP2Int int
Int2IP System.Net.IPAddress
ReadUntil Response
ReadUntil Response
ReadUntil Response

Public Methods

Method Description
BeginCmd ( byte buf, AsyncCallback cb, object state ) : IAsyncResult

Sends buf byte array command to the PLC asynchronously.

This method returns immediately. When the response is received cb will be invoked.

BeginConnect ( AsyncCallback cb, object state ) : IAsyncResult
BeginRead ( int offset, int len, AsyncCallback cb, object state ) : IAsyncResult

Starts an async reading.

BeginReadMulti ( ushort offsets, AsyncCallback cb, object state ) : IAsyncResult

Begins an asynchronous multiple read.

BeginWriteRaw ( int offset, byte data, AsyncCallback cb, object state ) : IAsyncResult

Writes raw data to PLC memory in the given offset asynchronously.

When the write operation is complete cb will be invoked (from another thread).

Cmd ( byte buf ) : Response

Sends a byte stream as a telnet command.

Cmd ( string cmd ) : Response

Sends a string as a telnet command.

ConfigureNetwork ( IPAddress ip, IPAddress nm, IPAddress gw, IPAddress dns ) : void

Configures the networking configuration of the PLC. If either ip or dns is 0.0.0.0 or 255.255.255.255 the device puts itself in DHCP mode and tries to acquire IP automatically. If your configuration changed any of ip, nm or gw all connections to the device will be closed. After sending this command, this object will update its IP configuration regardless of any response and will try to reconnect during next data send. Note that reconnections will block. Also, note that, if a not working DNS configuration is provided, WMI feature of the PLC won't be usable.

Connect ( ) : void

Attempts to connect to the PLC. This method will be automatically called if a data send attempt is made prior to connecting, partly because of your convenience and partly for making the library handle disconnections better.

It is a good practice to initally call this method first to check if the password is correct.

Disconnect ( ) : void
EndCmd ( IAsyncResult ar ) : Response

Ends a BeginCmd asynchronous call.

EndConnect ( IAsyncResult ar ) : void
EndRead ( IAsyncResult ar ) : BinaryReader
EndReadMulti ( IAsyncResult ar ) : BinaryReader
EndWrite ( IAsyncResult ar ) : void

Ends a asynchronous BeginWriteRaw call.

PLC ( IPAddress ip, string password ) : System

Connects to said PLC with given IP and password on default port 23.

PLC ( IPEndPoint ip, string password ) : System

Initiates a PLC object. Does not automatically connect until said. Finder can be used to find IP addresses of PLC devices from PLC serial numbers (MAC addresses).

Read ( int offset, int len ) : BinaryReader

Reads len bytes of data from offset sequentially.

ReadMulti ( ushort offsets ) : BinaryReader

Reads 4-byte values from PLC memory at arbitrary offsets given as offsets. Usefull for reading non sequential data from PLC memory. Unlike alternatives, it does all the reading in a single network packet roundtrip, hence it is higher performance, yet harder to use.

Reboot ( ) : void

Sends a reboot command to the PLC. Your connection will be lost.

Rewrite ( ) : void

Rewrites last command

Run ( ) : bool

Sends run command to the PLC.

Send ( byte data ) : void
Send ( string str ) : void
Stop ( ) : bool

Sends stop command to the PLC.

ToString ( ) : string
UpdateFirmware ( string path ) : void

Updates the firmware.

UpdateNetwork ( ) : void
Write ( int offset, byte data ) : void

Writes whole data to PLC memory at offset, sequentially.

Write ( int offset, float value ) : void

Writes a floating point value to the given offset.

Write ( int offset, int value ) : void

Writes an Int32 value to the given offset.

Write ( int offset, ushort value ) : void

Writes a UInt16 value to the given offset.

Private Methods

Method Description
IP2Int ( IPAddress addr ) : int
Int2IP ( int addr ) : IPAddress
ReadUntil ( ) : Response

Reads until command prompt.

ReadUntil ( String untils ) : Response

Waits until any of the strings in the untils array matched.

ReadUntil ( string str ) : Response

Reads until a str matched

Method Details

BeginCmd() public method

Sends buf byte array command to the PLC asynchronously.

This method returns immediately. When the response is received cb will be invoked.

public BeginCmd ( byte buf, AsyncCallback cb, object state ) : IAsyncResult
buf byte ///
cb AsyncCallback Callback to be called when the operation is complete.
state object Any state to be passed to when invoked, it's a good idea to use the PLC instance as state.
return IAsyncResult

BeginConnect() public method

public BeginConnect ( AsyncCallback cb, object state ) : IAsyncResult
cb AsyncCallback
state object
return IAsyncResult

BeginRead() public method

Starts an async reading.
public BeginRead ( int offset, int len, AsyncCallback cb, object state ) : IAsyncResult
offset int
len int
cb AsyncCallback
state object
return IAsyncResult

BeginReadMulti() public method

Begins an asynchronous multiple read.
public BeginReadMulti ( ushort offsets, AsyncCallback cb, object state ) : IAsyncResult
offsets ushort
cb AsyncCallback
state object
return IAsyncResult

BeginWriteRaw() public method

Writes raw data to PLC memory in the given offset asynchronously.

When the write operation is complete cb will be invoked (from another thread).

public BeginWriteRaw ( int offset, byte data, AsyncCallback cb, object state ) : IAsyncResult
offset int Offset of the memory location in the whole PLC memory in bytes.
data byte Byte array of the data you want to write. This can contain sequential 4-byte integers, floats or 2-byte unsigned hosrts or 1-byte data.
cb AsyncCallback Callback to be called when the operation is complete.
state object Any state to be passed to when invoked, it's a good idea to use the PLC instance as state.
return IAsyncResult

Cmd() public method

Sends a byte stream as a telnet command.
public Cmd ( byte buf ) : Response
buf byte telnet command as byte array
return Response

Cmd() public method

Sends a string as a telnet command.
public Cmd ( string cmd ) : Response
cmd string
return Response

ConfigureNetwork() public method

Configures the networking configuration of the PLC. If either ip or dns is 0.0.0.0 or 255.255.255.255 the device puts itself in DHCP mode and tries to acquire IP automatically. If your configuration changed any of ip, nm or gw all connections to the device will be closed. After sending this command, this object will update its IP configuration regardless of any response and will try to reconnect during next data send. Note that reconnections will block. Also, note that, if a not working DNS configuration is provided, WMI feature of the PLC won't be usable.
public ConfigureNetwork ( IPAddress ip, IPAddress nm, IPAddress gw, IPAddress dns ) : void
ip System.Net.IPAddress IP Address of the PLC
nm System.Net.IPAddress Netmask
gw System.Net.IPAddress Gateway IP
dns System.Net.IPAddress DNS IP
return void

Connect() public method

Attempts to connect to the PLC. This method will be automatically called if a data send attempt is made prior to connecting, partly because of your convenience and partly for making the library handle disconnections better.

It is a good practice to initally call this method first to check if the password is correct.

Thrown if the password is wrong.
public Connect ( ) : void
return void

Disconnect() public method

public Disconnect ( ) : void
return void

EndCmd() public method

Ends a BeginCmd asynchronous call.
public EndCmd ( IAsyncResult ar ) : Response
ar IAsyncResult
return Response

EndConnect() public method

public EndConnect ( IAsyncResult ar ) : void
ar IAsyncResult
return void

EndRead() public method

public EndRead ( IAsyncResult ar ) : BinaryReader
ar IAsyncResult
return System.IO.BinaryReader

EndReadMulti() public method

public EndReadMulti ( IAsyncResult ar ) : BinaryReader
ar IAsyncResult
return System.IO.BinaryReader

EndWrite() public method

Ends a asynchronous BeginWriteRaw call.
public EndWrite ( IAsyncResult ar ) : void
ar IAsyncResult
return void

PLC() public method

Connects to said PLC with given IP and password on default port 23.
public PLC ( IPAddress ip, string password ) : System
ip System.Net.IPAddress
password string
return System

PLC() public method

Initiates a PLC object. Does not automatically connect until said. Finder can be used to find IP addresses of PLC devices from PLC serial numbers (MAC addresses).

public PLC ( IPEndPoint ip, string password ) : System
ip System.Net.IPEndPoint IP of the PLC
password string Password of the PLC
return System

Read() public method

Reads len bytes of data from offset sequentially.
Thrown when offset and/or length is out of PLC memory boundaries.
public Read ( int offset, int len ) : BinaryReader
offset int Offset in bytes.
len int Length in bytes.
return System.IO.BinaryReader

ReadMulti() public method

Reads 4-byte values from PLC memory at arbitrary offsets given as offsets. Usefull for reading non sequential data from PLC memory. Unlike alternatives, it does all the reading in a single network packet roundtrip, hence it is higher performance, yet harder to use.
public ReadMulti ( ushort offsets ) : BinaryReader
offsets ushort List of offsets in bytes.
return System.IO.BinaryReader

Reboot() public method

Sends a reboot command to the PLC. Your connection will be lost.
public Reboot ( ) : void
return void

Rewrite() public method

Rewrites last command
public Rewrite ( ) : void
return void

Run() public method

Sends run command to the PLC.
public Run ( ) : bool
return bool

Send() public method

public Send ( byte data ) : void
data byte
return void

Send() public method

public Send ( string str ) : void
str string
return void

Stop() public method

Sends stop command to the PLC.
public Stop ( ) : bool
return bool

ToString() public method

public ToString ( ) : string
return string

UpdateFirmware() public method

Updates the firmware.
public UpdateFirmware ( string path ) : void
path string The path.
return void

UpdateNetwork() public method

public UpdateNetwork ( ) : void
return void

Write() public method

Writes whole data to PLC memory at offset, sequentially.
public Write ( int offset, byte data ) : void
offset int Offset in bytes.
data byte Data to be written
return void

Write() public method

Writes a floating point value to the given offset.
public Write ( int offset, float value ) : void
offset int Offset in bytes
value float Float value
return void

Write() public method

Writes an Int32 value to the given offset.
public Write ( int offset, int value ) : void
offset int Offset in bytes
value int Int32 value
return void

Write() public method

Writes a UInt16 value to the given offset.
public Write ( int offset, ushort value ) : void
offset int Offset in bytes
value ushort UInt16 value
return void

Property Details

IP public_oe property

MI registers of PLC. These are general purpose integer registers. Consists of 1024 4-byte integers. Each read and write attempt will result in a network transmission. plc.MI[5] = 10; Console.Out.WriteLine(plc.MI[5]);
Thrown when offset and/or length is out of PLC memory boundaries.
public BoolArrayAccessor,ENDA.PLCNetLib.Accessors IP
return ENDA.PLCNetLib.Accessors.BoolArrayAccessor

MB public_oe property

MI registers of PLC. These are general purpose integer registers. Consists of 1024 4-byte integers. Each read and write attempt will result in a network transmission. plc.MI[5] = 10; Console.Out.WriteLine(plc.MI[5]);
Thrown when offset and/or length is out of PLC memory boundaries.
public BoolArrayAccessor,ENDA.PLCNetLib.Accessors MB
return ENDA.PLCNetLib.Accessors.BoolArrayAccessor

MF public_oe property

MI registers of PLC. These are general purpose integer registers. Consists of 1024 4-byte integers. Each read and write attempt will result in a network transmission. plc.MI[5] = 10; Console.Out.WriteLine(plc.MI[5]);
Thrown when offset and/or length is out of PLC memory boundaries.
public FloatArrayAccessor,ENDA.PLCNetLib.Accessors MF
return ENDA.PLCNetLib.Accessors.FloatArrayAccessor

MI public_oe property

MI registers of PLC, consists of 1024 4-byte integers. Each read and write attempt will result in a network transmission. plc.MI[5] = 10; Console.Out.WriteLine(plc.MI[5]);
Thrown when offset and/or length is out of PLC memory boundaries.
public Int32ArrayAccessor,ENDA.PLCNetLib.Accessors MI
return ENDA.PLCNetLib.Accessors.Int32ArrayAccessor

MW public_oe property

MI registers of PLC. These are general purpose integer registers. Consists of 1024 4-byte integers. Each read and write attempt will result in a network transmission. plc.MI[5] = 10; Console.Out.WriteLine(plc.MI[5]);
Thrown when offset and/or length is out of PLC memory boundaries.
public UInt16ArrayAccessor,ENDA.PLCNetLib.Accessors MW
return ENDA.PLCNetLib.Accessors.UInt16ArrayAccessor

QP public_oe property

MI registers of PLC. These are general purpose integer registers. Consists of 1024 4-byte integers. Each read and write attempt will result in a network transmission. plc.MI[5] = 10; Console.Out.WriteLine(plc.MI[5]);
Thrown when offset and/or length is out of PLC memory boundaries.
public BoolArrayAccessor,ENDA.PLCNetLib.Accessors QP
return ENDA.PLCNetLib.Accessors.BoolArrayAccessor