Skip to main content

Modbus emulator

The Modbus Emulator software simulates running a machine communicating over the Modbus protocol

Abstract

The Modbus Emulator software simulates running a machine communicating over the Modbus protocol

FairCom Edge uses Modbus server and client emulators for learning and testing Modbus on both Windows and Linux computers.

The diagslave Modbus server emulator is a free, open-source command-line utility for Windows or Linux computers. When running, it listens for and responds to Modbus read and write requests. It is useful when you do not have a Modbus device, and you want to play with the Modbus protocol.

The modpoll Modbus client emulator is a free, open-source command-line utility for Windows or Linux computers. It reads or writes data to any Modbus device, including the Modbus server emulator.

FairCom provides three scripts to simplify using these command-line utilities:

  • run - runs the Modbus server emulator. This allows you to test the FairCom Modbus connector when you do not have a Modbus device.

  • read - runs the modpoll Modbus client emulator to read a data register between 1 and 65536 from the diagslave Modbus server emulator. The emulator interprets a register value as a two-byte signed integer from -32768 and 32767. The FairCom Modbus driver can convert two-byte values to unsigned integers from 0 to 65535, signed integers from -32768 to 32767, and floating-point values.

  • write - modifies a register value in the emulator. You can write to any register from 1 to 65535 with an integer value between -32768 and 32767.

Caution

You can run two Modbus emulators on the same port, but the second one reports no activity because the previously running instance handles reads and writes.

Prerequisites

  1. Install FairCom Edge

  2. Open a DOS command prompt:

    1. Press WINDOWS + R

    2. Type cmd

    3. Press ENTER

  3. Change to the <faircom>\server\modbus folder, such as cd C:\FairCom\FairCom-Edge.windows.64bit.v4.2.0.173\server\modbus

Run the Modbus emulator

Type run at the command prompt and press ENTER, or double-click on the file in Windows Explorer.

  • If successful, it displays:

    Opened Modbus emulator on Ethernet Port 502 in a new command window. 
    Updated Modbus Emulator register 4004 with value 23

    It automatically opens another command window and changes focus to it. This window shows status changes as the Modbus Emulator processes requests:

    diagslave 3.4 - FieldTalk(tm) Modbus(R) Diagnostic Slave Simulator
    Copyright (c) 2002-2021 proconX Pty Ltd
    Visit https://www.modbusdriver.com for Modbus libraries and tools.
    
    Protocol configuration: MODBUS/TCP
    Slave configuration: address = -1, master activity t/o = 3.00s
    IP configuration: port = 502, connection t/o = 60.00s
    
    Server started up successfully.
    Listening to network (Ctrl-C to stop)
    
    validateMasterIpAddr: accepting connection from 127.0.0.1
    Slave   1: writeHoldingRegisters from 4004, 1 references
    ......
  • If it fails, it displays:

    ERROR: Failed to run the Modbus emulator.
    ERROR: Failed to update Modbus emulator.

Write a register value

Write to a register in the Modbus emulator by running the write.bat script:

  1. Type write and press ENTER, or double-click on the file in Windows Explorer.

  2. Type in a register number and press ENTER.

    Pressing ENTER without typing a register number will set the register number to 4004 or the last register you entered by default.

  3. Type in a numeric value to assign to the register and press ENTER.

    Pressing ENTER without typing a number will set the value to 23 or the last value you entered by default.

    If successful, it displays the following message:

    Updated Modbus Emulator register 4004 with value 24

Read a register value

  1. Read a register value in the Modbus emulator using the read.bat script.

  2. Type read and press ENTER, or double-click on the file in Windows Explorer.

  3. Type in a register number and press ENTER.

    Pressing ENTER without typing a register number will set the register number to 4004 or the last register you entered by default.

  4. If successful, the script displays the register and its value below the heading, -- Polling slave...

    It displays the following message: successfully read Modbus emulator register XXXX.

    modpoll 3.10 - FieldTalk(tm) Modbus(R) Master Simulator
    Copyright (c) 2002-2021 proconX Pty Ltd
    Visit https://www.modbusdriver.com for Modbus libraries and tools.
    
    Protocol configuration: MODBUS/TCP, FC3
    Slave configuration...: address = 1, start reference = 4004, count = 1
    Communication.........: localhost, port 502, t/o 1.00 s, poll rate 1000 ms
    Data type.............: 16-bit register, output (holding) register table
    
    -- Polling slave...
    [4004]: 24
    
    Successfully read Modbus emulator register 4004

Prerequisites

  1. Install FairCom Edge

  2. Press CTRL + ALT + T to open a terminal

  3. Change to the <faircom>/server/modbus folder, such as cd /FairCom/FairCom-Edge.linux.x64.64bit.v4.2.0.163/server/modbus

Run the Modbus emulator

Enter sudo ./run.sh in the terminal.

sudo is required for access to the network interface.

  • If successful, it displays:

    Started Modbus emulator on Ethernet Port 502
    Updated Modbus Emulator register 4004 with value 23
    The Modbus emulator is run in the background with the output redirected to modbus.log
  • If it fails, it displays:

    ERROR: Failed to start the Modbus emulator
    ERROR: Failed to update Modbus emulator

Write a register value

Write to a register in the Modbus emulator using sudo ./write.sh.

sudo is required for access to the network interface.

  1. Type in a register number and press ENTER.

    Pressing ENTER without typing a register number will set the register number to 4004 or the last register you entered by default.

  2. Type in a numeric value to assign to the register and press ENTER.

    Pressing ENTER without typing a number will set the value to 23 or the last value you entered by default.

    If successful, it displays the following message:

    Written 1 reference. 
    Updated Modbus emulator register XXXX with value XX. 

Read a register value

  1. Read a register value in the Modbus emulator using ./read.sh.

  2. Type in a register number and press ENTER.

    Pressing ENTER without typing a register number will set the register number to 4004 or the last register you entered by default.

    If successful, the script displays the register and its value below the heading, --Polling slave....

    It displays the following message:

    Successfully read Modbus emulator register XXXX.

Instead of using the Faircom run, read, and write scripts, you can directly run the diagslave and modpoll command-line utilities.

Directly run diagslave

You can directly run the diagslave command-line utility. It launches a Modbus Server Emulator on a specific Ethernet port.

  1. In the terminal, move to the <faircom>\server\modbus\test\Windows\diagslave-3.4\win folder.

  2. Enter diagslave -h to see its command-line options.

Directly run modpoll

You can directly run the modpoll command-line utility to read and update values on a Modbus device, including the Modbus Server Emulator.

  1. In the terminal, move to the <faircom>\server\modbus\test\Windows\modpoll-3.10\win folder.

  2. Enter modpoll -h to see its command-line options.

Run examples

  • Run Modbus emulator on Ethernet TPC Port 502: diagslave -m tcp -p 502

Read examples

  1. Read 1 value starting at register 4004 on a Modbus device on TCP Port 502 on the local computer. modpoll -m tcp -p 502 -a 1 -r 4004 -c 1 -1 localhost

  2. Read 5 values starting at register 4004 on a Modbus device on TCP Port 502 on the local computer. modpoll -m tcp -p 502 -a 1 -r 4004 -c 5 -1 localhost

  3. Continuously read 5 values starting at register 4004 on a Modbus device on TPC Port 502 on the local computer. modpoll -m tcp -p 502 -a 1 -r 4004 -c 5 localhost

Write examples

  1. Write 23 to register 4004 on a Modbus device on TCP Pot 502 on the local computer. modpoll -m tcp -p 502 -a 1 -r 4004 -c 1 localhost 23

  2. Write 1 in hexadecimal notation to register 4004 on a Modbus device on TCP Port 502 on the local computer. modpoll -m tcp -p 502 -a 1 -r 4004 -c 1 localhost 0x0001

  3. Write 1 to register 4004 using a zero-based register address on a Modbus device on TCP Port 502 on the local computer. modpoll -m tcp -p 502 -a 1 -r 4004 -c 1 -0 localhost 0x0001

  4. Write 5 values starting at register 4004 on a Modbus device on TCP Port 502 on the local computer. modpoll -m tcp -p 502 -a 1 -r 4004 -c 5 localhost 23 24 25 26 27