C# Класс ModbusLib.Master

Modbus TCP common driver class. This class implements a modbus TCP master driver. It supports the following commands: Read coils Read discrete inputs Write single coil Write multiple cooils Read holding register Read input register Write single register Write multiple register All commands can be sent in synchronous or asynchronous mode. If a value is accessed in synchronous mode the program will stop and wait for slave to response. If the slave didn't answer within a specified time a timeout exception is called. The class uses multi threading for both synchronous and asynchronous access. For the communication two lines are created. This is necessary because the synchronous thread has to wait for a previous command to finish.
Показать файл Открыть проект Примеры использования класса

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

Метод Описание
Dispose ( ) : void

Destroy master instance

Master ( ) : System

Create master instance without parameters.

Master ( string ip, ushort port ) : System

Create master instance with parameters.

ReadCoils ( ushort id, ushort startAddress, ushort numInputs ) : void

Read coils from slave asynchronous. The result is given in the response function.

ReadCoils ( ushort id, ushort startAddress, ushort numInputs, byte &values ) : void

Read coils from slave synchronous.

ReadDiscreteInputs ( ushort id, ushort startAddress, ushort numInputs ) : void

Read discrete inputs from slave asynchronous. The result is given in the response function.

ReadDiscreteInputs ( ushort id, ushort startAddress, ushort numInputs, byte &values ) : void

Read discrete inputs from slave synchronous.

ReadHoldingRegister ( ushort id, ushort startAddress, ushort numInputs ) : void

Read holding registers from slave asynchronous. The result is given in the response function.

ReadHoldingRegister ( ushort id, ushort startAddress, ushort numInputs, byte &values ) : void

Read holding registers from slave synchronous.

ReadInputRegister ( ushort id, ushort startAddress, ushort numInputs ) : void

Read input registers from slave asynchronous. The result is given in the response function.

ReadInputRegister ( ushort id, ushort startAddress, ushort numInputs, byte &values ) : void

Read input registers from slave synchronous.

ReadWriteMultipleRegister ( ushort id, ushort startReadAddress, ushort numInputs, ushort startWriteAddress, byte values ) : void

Read/Write multiple registers in slave asynchronous. The result is given in the response function.

ReadWriteMultipleRegister ( ushort id, ushort startReadAddress, ushort numInputs, ushort startWriteAddress, byte values, byte &result ) : void

Read/Write multiple registers in slave synchronous. The result is given in the response function.

WriteMultipleCoils ( ushort id, ushort startAddress, ushort numBits, byte values ) : void

Write multiple coils in slave asynchronous. The result is given in the response function.

WriteMultipleCoils ( ushort id, ushort startAddress, ushort numBits, byte values, byte &result ) : void

Write multiple coils in slave synchronous.

WriteMultipleRegister ( ushort id, ushort startAddress, byte values ) : void

Write multiple registers in slave asynchronous. The result is given in the response function.

WriteMultipleRegister ( ushort id, ushort startAddress, byte values, byte &result ) : void

Write multiple registers in slave synchronous.

WriteSingleCoils ( ushort id, ushort startAddress, bool OnOff ) : void

Write single coil in slave asynchronous. The result is given in the response function.

WriteSingleCoils ( ushort id, ushort startAddress, bool OnOff, byte &result ) : void

Write single coil in slave synchronous.

WriteSingleRegister ( ushort id, ushort startAddress, byte values ) : void

Write single register in slave asynchronous. The result is given in the response function.

WriteSingleRegister ( ushort id, ushort startAddress, byte values, byte &result ) : void

Write single register in slave synchronous.

connect ( string ip, ushort port ) : void

Start connection to slave.

disconnect ( ) : void

Stop connection to slave.

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

Метод Описание
CallException ( ushort id, byte function, byte exception ) : void
CreateReadHeader ( ushort id, ushort startAddress, ushort length, byte function ) : byte[]
CreateReadWriteHeader ( ushort id, ushort startReadAddress, ushort numRead, ushort startWriteAddress, ushort numWrite ) : byte[]
CreateWriteHeader ( ushort id, ushort startAddress, ushort numData, ushort numBytes, byte function ) : byte[]
OnReceive ( System result ) : void
OnSend ( System result ) : void
WriteAsyncData ( byte write_data, ushort id ) : void
WriteSyncData ( byte write_data, ushort id ) : byte[]

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

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

Destroy master instance
public Dispose ( ) : void
Результат void

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

Create master instance without parameters.
public Master ( ) : System
Результат System

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

Create master instance with parameters.
public Master ( string ip, ushort port ) : System
ip string IP adress of modbus slave.
port ushort Port number of modbus slave. Usually port 502 is used.
Результат System

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

Read coils from slave asynchronous. The result is given in the response function.
public ReadCoils ( ushort id, ushort startAddress, ushort numInputs ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
Результат void

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

Read coils from slave synchronous.
public ReadCoils ( ushort id, ushort startAddress, ushort numInputs, byte &values ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
values byte Contains the result of function.
Результат void

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

Read discrete inputs from slave asynchronous. The result is given in the response function.
public ReadDiscreteInputs ( ushort id, ushort startAddress, ushort numInputs ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
Результат void

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

Read discrete inputs from slave synchronous.
public ReadDiscreteInputs ( ushort id, ushort startAddress, ushort numInputs, byte &values ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
values byte Contains the result of function.
Результат void

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

Read holding registers from slave asynchronous. The result is given in the response function.
public ReadHoldingRegister ( ushort id, ushort startAddress, ushort numInputs ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
Результат void

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

Read holding registers from slave synchronous.
public ReadHoldingRegister ( ushort id, ushort startAddress, ushort numInputs, byte &values ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
values byte Contains the result of function.
Результат void

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

Read input registers from slave asynchronous. The result is given in the response function.
public ReadInputRegister ( ushort id, ushort startAddress, ushort numInputs ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
Результат void

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

Read input registers from slave synchronous.
public ReadInputRegister ( ushort id, ushort startAddress, ushort numInputs, byte &values ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
values byte Contains the result of function.
Результат void

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

Read/Write multiple registers in slave asynchronous. The result is given in the response function.
public ReadWriteMultipleRegister ( ushort id, ushort startReadAddress, ushort numInputs, ushort startWriteAddress, byte values ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startReadAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
startWriteAddress ushort Address to where the data is written.
values byte Contains the register information.
Результат void

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

Read/Write multiple registers in slave synchronous. The result is given in the response function.
public ReadWriteMultipleRegister ( ushort id, ushort startReadAddress, ushort numInputs, ushort startWriteAddress, byte values, byte &result ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startReadAddress ushort Address from where the data read begins.
numInputs ushort Length of data.
startWriteAddress ushort Address to where the data is written.
values byte Contains the register information.
result byte Contains the result of the synchronous command.
Результат void

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

Write multiple coils in slave asynchronous. The result is given in the response function.
public WriteMultipleCoils ( ushort id, ushort startAddress, ushort numBits, byte values ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numBits ushort Specifys number of bits.
values byte Contains the bit information in byte format.
Результат void

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

Write multiple coils in slave synchronous.
public WriteMultipleCoils ( ushort id, ushort startAddress, ushort numBits, byte values, byte &result ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
numBits ushort Specifys number of bits.
values byte Contains the bit information in byte format.
result byte Contains the result of the synchronous write.
Результат void

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

Write multiple registers in slave asynchronous. The result is given in the response function.
public WriteMultipleRegister ( ushort id, ushort startAddress, byte values ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address to where the data is written.
values byte Contains the register information.
Результат void

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

Write multiple registers in slave synchronous.
public WriteMultipleRegister ( ushort id, ushort startAddress, byte values, byte &result ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address to where the data is written.
values byte Contains the register information.
result byte Contains the result of the synchronous write.
Результат void

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

Write single coil in slave asynchronous. The result is given in the response function.
public WriteSingleCoils ( ushort id, ushort startAddress, bool OnOff ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
OnOff bool Specifys if the coil should be switched on or off.
Результат void

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

Write single coil in slave synchronous.
public WriteSingleCoils ( ushort id, ushort startAddress, bool OnOff, byte &result ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address from where the data read begins.
OnOff bool Specifys if the coil should be switched on or off.
result byte Contains the result of the synchronous write.
Результат void

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

Write single register in slave asynchronous. The result is given in the response function.
public WriteSingleRegister ( ushort id, ushort startAddress, byte values ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address to where the data is written.
values byte Contains the register information.
Результат void

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

Write single register in slave synchronous.
public WriteSingleRegister ( ushort id, ushort startAddress, byte values, byte &result ) : void
id ushort Unique id that marks the transaction. In asynchonous mode this id is given to the callback function.
startAddress ushort Address to where the data is written.
values byte Contains the register information.
result byte Contains the result of the synchronous write.
Результат void

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

Start connection to slave.
public connect ( string ip, ushort port ) : void
ip string IP adress of modbus slave.
port ushort Port number of modbus slave. Usually port 502 is used.
Результат void

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

Stop connection to slave.
public disconnect ( ) : void
Результат void