C# Класс 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.

Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
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

Свойство Тип Описание
IP2Int int
Int2IP System.Net.IPAddress
ReadUntil Response
ReadUntil Response
ReadUntil Response

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
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

Описание методов

BeginCmd() публичный Метод

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.
Результат IAsyncResult

BeginConnect() публичный Метод

public BeginConnect ( AsyncCallback cb, object state ) : IAsyncResult
cb AsyncCallback
state object
Результат IAsyncResult

BeginRead() публичный Метод

Starts an async reading.
public BeginRead ( int offset, int len, AsyncCallback cb, object state ) : IAsyncResult
offset int
len int
cb AsyncCallback
state object
Результат IAsyncResult

BeginReadMulti() публичный Метод

Begins an asynchronous multiple read.
public BeginReadMulti ( ushort offsets, AsyncCallback cb, object state ) : IAsyncResult
offsets ushort
cb AsyncCallback
state object
Результат IAsyncResult

BeginWriteRaw() публичный Метод

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.
Результат IAsyncResult

Cmd() публичный Метод

Sends a byte stream as a telnet command.
public Cmd ( byte buf ) : Response
buf byte telnet command as byte array
Результат Response

Cmd() публичный Метод

Sends a string as a telnet command.
public Cmd ( string cmd ) : Response
cmd string
Результат Response

ConfigureNetwork() публичный Метод

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
Результат void

Connect() публичный Метод

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
Результат void

Disconnect() публичный Метод

public Disconnect ( ) : void
Результат void

EndCmd() публичный Метод

Ends a BeginCmd asynchronous call.
public EndCmd ( IAsyncResult ar ) : Response
ar IAsyncResult
Результат Response

EndConnect() публичный Метод

public EndConnect ( IAsyncResult ar ) : void
ar IAsyncResult
Результат void

EndRead() публичный Метод

public EndRead ( IAsyncResult ar ) : BinaryReader
ar IAsyncResult
Результат System.IO.BinaryReader

EndReadMulti() публичный Метод

public EndReadMulti ( IAsyncResult ar ) : BinaryReader
ar IAsyncResult
Результат System.IO.BinaryReader

EndWrite() публичный Метод

Ends a asynchronous BeginWriteRaw call.
public EndWrite ( IAsyncResult ar ) : void
ar IAsyncResult
Результат void

PLC() публичный Метод

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
Результат System

PLC() публичный Метод

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
Результат System

Read() публичный Метод

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.
Результат System.IO.BinaryReader

ReadMulti() публичный Метод

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.
Результат System.IO.BinaryReader

Reboot() публичный Метод

Sends a reboot command to the PLC. Your connection will be lost.
public Reboot ( ) : void
Результат void

Rewrite() публичный Метод

Rewrites last command
public Rewrite ( ) : void
Результат void

Run() публичный Метод

Sends run command to the PLC.
public Run ( ) : bool
Результат bool

Send() публичный Метод

public Send ( byte data ) : void
data byte
Результат void

Send() публичный Метод

public Send ( string str ) : void
str string
Результат void

Stop() публичный Метод

Sends stop command to the PLC.
public Stop ( ) : bool
Результат bool

ToString() публичный Метод

public ToString ( ) : string
Результат string

UpdateFirmware() публичный Метод

Updates the firmware.
public UpdateFirmware ( string path ) : void
path string The path.
Результат void

UpdateNetwork() публичный Метод

public UpdateNetwork ( ) : void
Результат void

Write() публичный Метод

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
Результат void

Write() публичный Метод

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
Результат void

Write() публичный Метод

Writes an Int32 value to the given offset.
public Write ( int offset, int value ) : void
offset int Offset in bytes
value int Int32 value
Результат void

Write() публичный Метод

Writes a UInt16 value to the given offset.
public Write ( int offset, ushort value ) : void
offset int Offset in bytes
value ushort UInt16 value
Результат void

Описание свойств

IP публичное свойство

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
Результат ENDA.PLCNetLib.Accessors.BoolArrayAccessor

MB публичное свойство

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
Результат ENDA.PLCNetLib.Accessors.BoolArrayAccessor

MF публичное свойство

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
Результат ENDA.PLCNetLib.Accessors.FloatArrayAccessor

MI публичное свойство

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
Результат ENDA.PLCNetLib.Accessors.Int32ArrayAccessor

MW публичное свойство

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
Результат ENDA.PLCNetLib.Accessors.UInt16ArrayAccessor

QP публичное свойство

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
Результат ENDA.PLCNetLib.Accessors.BoolArrayAccessor