C# Class 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.
Afficher le fichier Open project: rj128x/MODBUS Class Usage Examples

Méthodes publiques

Méthode Description
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.

Private Methods

Méthode Description
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[]

Method Details

Dispose() public méthode

Destroy master instance
public Dispose ( ) : void
Résultat void

Master() public méthode

Create master instance without parameters.
public Master ( ) : System
Résultat System

Master() public méthode

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.
Résultat System

ReadCoils() public méthode

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.
Résultat void

ReadCoils() public méthode

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.
Résultat void

ReadDiscreteInputs() public méthode

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.
Résultat void

ReadDiscreteInputs() public méthode

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.
Résultat void

ReadHoldingRegister() public méthode

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.
Résultat void

ReadHoldingRegister() public méthode

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.
Résultat void

ReadInputRegister() public méthode

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.
Résultat void

ReadInputRegister() public méthode

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.
Résultat void

ReadWriteMultipleRegister() public méthode

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.
Résultat void

ReadWriteMultipleRegister() public méthode

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.
Résultat void

WriteMultipleCoils() public méthode

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.
Résultat void

WriteMultipleCoils() public méthode

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.
Résultat void

WriteMultipleRegister() public méthode

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.
Résultat void

WriteMultipleRegister() public méthode

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.
Résultat void

WriteSingleCoils() public méthode

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.
Résultat void

WriteSingleCoils() public méthode

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.
Résultat void

WriteSingleRegister() public méthode

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.
Résultat void

WriteSingleRegister() public méthode

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.
Résultat void

connect() public méthode

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.
Résultat void

disconnect() public méthode

Stop connection to slave.
public disconnect ( ) : void
Résultat void