Jump to content

Component: Modbus Slave USB (Comms: System): Difference between revisions

From Flowcode Help
m Text replacement - "class="wikitable" style="width:60%; background-color:#FFFFFF;"" to "class="mtx-class-macrotable wikitable""
m Text replacement - "width="10%" align="center" style="background-color:#D8C9D8;" align="center"" to "width="10%" align="center" class="mtx-class-macrohead""
Line 159: Line 159:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ChangeFrameType'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ChangeFrameType'''
|-
|-
Line 178: Line 178:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''CheckForIncoming'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''CheckForIncoming'''
|-
|-
Line 192: Line 192:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''GetLastIncoming'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''GetLastIncoming'''
|-
|-
Line 211: Line 211:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Initialise'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''Initialise'''
|-
|-
Line 225: Line 225:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReadAnalogInput'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReadAnalogInput'''
|-
|-
Line 244: Line 244:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReadCoils'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReadCoils'''
|-
|-
Line 268: Line 268:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReadDigitalInputs'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReadDigitalInputs'''
|-
|-
Line 292: Line 292:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReadHoldingRegister'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''ReadHoldingRegister'''
|-
|-
Line 311: Line 311:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetAnalogInput'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetAnalogInput'''
|-
|-
Line 335: Line 335:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetCoil'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetCoil'''
|-
|-
Line 359: Line 359:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetDigitalInput'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetDigitalInput'''
|-
|-
Line 383: Line 383:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetHoldingRegister'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetHoldingRegister'''
|-
|-
Line 407: Line 407:
{| class="mtx-class-macrotable wikitable"
{| class="mtx-class-macrotable wikitable"
|-
|-
| width="10%" align="center" style="background-color:#D8C9D8;" align="center" | [[File:Fc9-comp-macro.png]]
| width="10%" align="center" class="mtx-class-macrohead" | [[File:Fc9-comp-macro.png]]
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetSlaveAddress'''
| width="90%" style="background-color:#D8C9D8; color:#4B008D;" | '''SetSlaveAddress'''
|-
|-

Revision as of 14:17, 13 July 2026

Author Matrix Ltd.
Version 2.0
Category Comms: System


Modbus Slave USB component

Modbus component for creating Modbus compatible slave hardware via USB Serial.

Component Source Code

Please click here to download the component source project: FC_Comp_Source_ModbusSlaveUSB.fcfx

Please click here to view the component source code (Beta): FC_Comp_Source_ModbusSlaveUSB.fcfx

Detailed description

No detailed description exists yet for this component

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.

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.

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.







Macro reference

ChangeFrameType

ChangeFrameType
Default frame type is the type set in the component properties. 
- BYTE Type
0 = Modbus RTU / 1 = Modbus ASCII 
- VOID Return


CheckForIncoming

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. 
- BYTE Return


GetLastIncoming

GetLastIncoming
Gets a value from the last incoming command. Index 0 = 0 Read / 1 Write Index 1 = 0 Coils / 1 DigInput / 2 AnInput / 3 Register Index 2 = Address Index 3 = Number 
- BYTE Index
Range 0-3 
- UINT Return


Initialise

Initialise
Starts up the UART to allow communications and initialises the states of the various Modbus Coils, Inputs and Registers to 0. Returns 1 if USB is started ok. 
- BOOL Return


ReadAnalogInput

ReadAnalogInput
Reads the value of a single analogue input. 
- UINT Address
Coil Address 
- UINT Return


ReadCoils

ReadCoils
Reads the state of a single digital coil. Can pack a max of 8-bits together in a single operation 
- UINT StartAddress
Coil Address Range 0 to (NumCoils - 1) 
- BYTE AddressCount
Range 1-8 
- BYTE Return


ReadDigitalInputs

ReadDigitalInputs
Reads the state of a single digital input. Can pack a max of 8-bits together in a single operation 
- UINT StartAddress
Coil Address Range 0 to (NumCoils - 1) 
- BYTE AddressCount
Range 1-8 
- BYTE Return


ReadHoldingRegister

ReadHoldingRegister
Reads the value of a single holding register. 
- UINT Address
Coil Address 
- UINT Return


SetAnalogInput

SetAnalogInput
Sets the state of a single analogue input. 
- UINT Address
Coil Address 
- UINT Value
Analogue Value range 0 - 65535 
- VOID Return


SetCoil

SetCoil
Sets the state of a single digital coil. 
- UINT Address
Coil Address 
- BYTE State
0=off, 1=on 
- VOID Return


SetDigitalInput

SetDigitalInput
Sets the state of a single digital input. 
- UINT Address
Coil Address 
- BYTE State
0=off, 1=on 
- VOID Return


SetHoldingRegister

SetHoldingRegister
Sets the state of a single holding register. 
- UINT Address
Coil Address 
- UINT Value
Analogue Value range 0 - 65535 
- VOID Return


SetSlaveAddress

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. 
- UINT SlaveAddress
 
- VOID Return


Property reference

Properties
USB Properties
USB Properties
Vendor ID
USB VID 
Product ID
USB PID 
Device Name
USB Device Name 
Manufacturer
USB Manufacturer 
Major Version
USB Major Version Number 
Minor Version
USB Minor Version Number 
Return Type
Specifies the way the receive function indicates a timeout. 8 Bit mode - Timeout is represented by the value 255. 16 Bit mode - Timeout is represented by the value 512 allowing the value 255 to represent valid data 
Enumeration Timeout
Specifies if the enumeration (Initialise function) can timeout if taking too long. 
USB Driver
Driver Directory
Directory to save the generated device driver .inf file 
Driver Filename
Filename to give to the generated driver file. 
Generate Driver
Generates a driver file when set to Yes, Automatically jumps back to No when complete. 
Modbus Properties
Timeout
Timeout in milliseconds used to dictate the maximum time to wait in milliseconds between bytes in a packet. Range: 1 - 255 
Frame Type
Switches between RTU and ASCII forms of Modbus 
Receive Buffer Size
Maximum number of bytes the receive buffer can hold 
Slave ID Bytes
Number of bytes used when passing a slave ID, usually 1 but sometimes 2 
Slave Address
 
Reply Delay
Delay in micro seconds to wait before transmitting a reply 
Maintain Stats
Controls if the last command from the master is logged allowing for easier value updates. No - Do not log commands Yes - Commands are logged and available via the GetLastIncoming macro 
Simulation Properties
Label
A text label to appear on the Modbus panel object. 
COM Port
Hardware COM port to communicate with any connected USB serial devices or any other virtual COM ports. 
Modbus Coils - Single bit values
Starting Address
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
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. 
Digital Inputs - Single bit values
Starting Address
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
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. 
Analog Inputs - 16-bit values
Starting Address
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
Specifies how many 16-bit analogue inputs are available on the slave. Each address consumes two RAM Bytes. 
Holding Registers - 16-bit values
Starting Address
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
Specifies how many 16-bit holding registers are available on the slave. Each address consumes two RAM Bytes.