Component: Modbus TCP Slave (Comms: System)

From Flowcode Help
Jump to navigationJump to search


Author Matrix Ltd.
Version 2.0 (Release)
Category Comms: System


Image Modbus TCP Slave component

Modbus component for creating Modbus compatible slave hardware via Ethernet or WIFI. Ensure TCP/IP or WIFI component is initialised and connected to a network before calling the ModbusSlaveTCP component macros.


Examples

Example master program to control the slave. The example sets coil address 0, clears coil address 0, reads an input register and outputs to the LCD. Be sure to set the IP address of the slave device at the top of the program. If you are simulating then use the IP address of the PC running the Slave simulation. Both programs can be simulated on the same PC using two instances of Flowcode.

FC6 Icon.png Modbus TCP Master Example


Example slave program to react to the signals from the master. The example listens for Modbus commands from the master and outputs the current coils 0 state to an LED connected to PortA. The input register is loaded with a value which is incremented on each Modbus transaction.

FC6 Icon.png Modbus TCP Slave Example


Addressing

In Modbus the addressing protocol looks like this.

Data Type Common name Starting address Ending Address Flowcode Start Address Flowcode End Address
Modbus Coils Bits, binary values, flags 00001 10000 0 9999
Digital Inputs Binary inputs 10001 30000 0 19999
Analog Inputs Binary inputs 30001 40000 0 9999
Modbus Registers Analog values, variables 40001 60000 0 19999


In Flowcode each section starts from 0 so the address range is as shown.


Downloadable macro reference

ReadHoldingRegister

Reads the value of a single holding register.

Parameters

UINT Address
Coil Address


Return value

UINT


ReadCoilStates

Reads the state of a single digital coil.

Can pack a max of 8-bits together in a single operation

Parameters

UINT StartAddress
Coil Address Range 0 to (NumCoils - 1)
BYTE AddressCount
Range 1-8


Return value

BYTE


ReadAnalogueInput

Reads the value of a single analogue input.

Parameters

UINT Address
Coil Address


Return value

UINT


ReadInputStates

Reads the state of a single digital input.

Can pack a max of 8-bits together in a single operation

Parameters

UINT StartAddress
Coil Address Range 0 to (NumCoils - 1)
BYTE AddressCount
Range 1-8


Return value

BYTE


SetSlaveAddress

Allows the slave address to be overridden from the default slave address specified

in the component property. The number of bytes used for the address is fixed by the

Slave ID Bytes property to save on RAM usage.

Parameters

UINT SlaveAddress


Return value

This call does not return a value


SetCoilState

Sets the state of a single digital coil.

Parameters

UINT Address
Coil Address
BYTE State
0=off, 1=on


Return value

This call does not return a value


SetInputState

Sets the state of a single digital input.

Parameters

UINT Address
Coil Address
BYTE State
0=off, 1=on


Return value

This call does not return a value


SetHoldingRegister

Sets the state of a single holding register.

Parameters

UINT Address
Coil Address
UINT Value
Analogue Value range 0 - 65535


Return value

This call does not return a value


CheckForIncoming

Checks for an incoming message and if the data address is within range then also automatically replies with the correct reply.

Returns 0 if no comms received, 1 for a succesful transaction, 255 for an error.

Parameters

This macro has no parameters


Return value

BYTE


SetAnalogueInput

Sets the state of a single analogue input.

Parameters

UINT Address
Coil Address
UINT Value
Analogue Value range 0 - 65535


Return value

This call does not return a value


Initialise

Starts up the UART to allow communications and initialises the states of the various

Modbus Coils, Inputs and Registers to 0.

Parameters

This macro has no parameters


Return value

This call does not return a value


Simulation macro reference

This component does not contain any simulation macros


Property reference

TCP/IP Component

This property is of type Panel object and can be referenced with the variable name NetworkComms1::Component.

Property to point to the TCP/IP or WIFI component you want to use.

If unconnected then default to simulated TCP/IP network communications using the PC network adapter.

Status

This property is of type Line of text and can be referenced with the variable name NetworkComms1::Status.

No additional information


TCP/IP Port

This property is of type Unsigned integer and can be referenced with the variable name Port.

Port used for network communications

Range: 0-65535

Receive Buffer Size

This property is of type Unsigned integer and can be referenced with the variable name ReceiveBufferSize.

Maximum number of bytes the receive buffer can hold

Slave Address

This property is of type Unsigned integer and can be referenced with the variable name SlaveAddress.

No additional information


Reply Delay

This property is of type Unsigned integer and can be referenced with the variable name ReplyDelay.

Delay in micro seconds to wait before transmitting a reply

BaudByteDelay

This property is of type Unsigned integer and can be referenced with the variable name BaudByteDelay.

No additional information


Label

This property is of type Line of text and can be referenced with the variable name label.

A text label to appear on the Modbus panel object.

Starting Address

This property is of type Unsigned integer and can be referenced with the variable name StartAdrCoil.

Start address of the Modbus coils. Referenced from 0.

For example

Slave 1 might have 100 coils addressed 0-99 Start Address = 0

Slave 2 might have 100 coils addressed 100-199 Start Address = 100

Number of Addresses

This property is of type Unsigned integer and can be referenced with the variable name NumAdrCoil.

Specifies how many Coils are available on the slave.

Each address consumes a single bit of RAM, 8 Addresses are packed together into a single RAM Byte.

Starting Address

This property is of type Unsigned integer and can be referenced with the variable name StartAdrDI.

Start address of the Modbus digital inputs. Referenced from 0.

For example

Slave 1 might have 100 inputs addressed 0-99 Start Address = 0

Slave 2 might have 100 inputs addressed 100-199 Start Address = 100

Number of Addresses

This property is of type Unsigned integer and can be referenced with the variable name NumAdrDI.

Specifies how many Digital Inputs are available on the slave.

Each address consumes a single bit of RAM, 8 Addresses are packed together into a single RAM Byte.

Starting Address

This property is of type Unsigned integer and can be referenced with the variable name StartAdrAI.

Start address of the Modbus analogue inputs. Referenced from 0.

For example

Slave 1 might have 100 inputs addressed 0-99 Start Address = 0

Slave 2 might have 100 inputs addressed 100-199 Start Address = 100

Number of Addresses

This property is of type Unsigned integer and can be referenced with the variable name NumAdrAI.

Specifies how many 16-bit analogue inputs are available on the slave.

Each address consumes two RAM Bytes.

Starting Address

This property is of type Unsigned integer and can be referenced with the variable name StartAdrReg.

Start address of the Modbus registers. Referenced from 0.

For example

Slave 1 might have 100 registers addressed 0-99 Start Address = 0

Slave 2 might have 100 registers addressed 100-199 Start Address = 100

Number of Addresses

This property is of type Unsigned integer and can be referenced with the variable name NumAdrReg.

Specifies how many 16-bit holding registers are available on the slave.

Each address consumes two RAM Bytes.