You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Modbus projects are comprised of a library, a console application, a test project, and two web applications.
ModbusLib
The Modbus library provides a set of helper classes to facilitate data access to a Modbus slave.
This library is using the NModbus library (found at GitHub (https://github.com/NModbus/NModbus) and has been recompiled as a .NET Standard 2.0 library.
NModbus
NModbus is a fork of NModbus4 which itself is a fork of the original NModbus (https://code.google.com/p/nmodbus) and supports serial ASCII, serial RTU, TCP, and UDP protocols.
NModbus is a C# implementation of the Modbus protocol.
Here only the Modbus TCP master is used to communicate with the Modbus TCP slaves.
The APIs used are:
Function
Function Code
Read Discrete Inputs
(fc 2)
Read Coils
(fc 1)
Write Single Coil
(fc 5)
Write Multiple Coils
(fc 15)
Read Input Registers
(fc 4)
Read Multiple Registers
(fc 3)
Write Single Register
(fc 6)
Write Multiple Registers
(fc 16)
Modbus Conversion
The additional datatypes supported are:
BitArray
Short
UShort
Int32
UInt32
Float
Double
Long
ULong
ASCII
Hex
Conversion routines from and to Modbus registers are provided.
NModbus Extensions
Additonal data types (using read/write coils and holding registers) are supported:
Function
Description
ReadString
Reads an ASCII string (multiple holding registers)
ReadHexString
Reads an HEX string (multiple holding registers)
ReadBool
Reads a boolean value (single coil)
ReadBits
Reads a 16 bit array (single holding register)
ReadShort
Reads a 16 bit integer (single holding register)
ReadUShort
Reads an unsigned 16 bit integer (single holding register)
ReadInt32
Reads a 32 bit integer (two holding registers)
ReadUInt32
Reads an unsigned 32 bit integer (two holding registers)
ReadFloat
Reads a 32 bit IEEE floating point number (two holding registers)
ReadDouble
Reads a 64 bit IEEE floating point number (four holding registers)
ReadLong
Reads a 64 bit integer (four holding registers)
ReadULong
Reads an unsigned 64 bit integer (four holding registers)
ReadBoolArray
Reads an array of boolean values (multiple coils)
ReadBytes
Reads 8 bit values (multiple holding register)
ReadShortArray
Reads an array of 16 bit integers (multiple holding register)
ReadUShortArray
Reads an array of unsigned 16 bit integer (multiple holding register)
ReadInt32Array
Reads an array of 32 bit integers (multiple holding registers)
ReadUInt32Array
Reads an array of unsigned 32 bit integers (multiple holding registers)
ReadFloatArray
Reads an array of 32 bit IEEE floating point numbers (multiple holding registers)
ReadDoubleArray
Reads an array of 64 bit IEEE floating point numbers (multiple holding registers)
ReadLongArray
Reads an array of 64 bit integers (multiple holding registers)
ReadULongArray
Reads an array of unsigned 64 bit integers (multiple holding registers)
WriteString
Writes an ASCII string (multiple holding registers)
WriteHexString
Writes an HEX string (multiple holding registers)
WriteBool
Writes a boolean value (single coil)
WriteBits
Writes a 16 bit array (single holding register)
WriteShort
Writes a 16 bit integer (single holding register)
WriteUShort
Writes an unsigned 16 bit integer (single holding register)
WriteInt32
Writes a 32 bit integer (two holding registers)
WriteUInt32
Writes an unsigned 32 bit integer (two holding registers)
WriteFloat
Writes a 32 bit IEEE floating point number (two holding registers)
WriteDouble
Writes a 64 bit IEEE floating point number (four holding registers)
WriteLong
Writes a 64 bit integer (four holding registers)
WriteULong
Writes an unsigned 64 bit integer (four holding registers)
WriteBoolArray
Writes an array of boolean values (multiple coils)
WriteBytes
Writes 8 bit values (multiple holding register)
WriteShortArray
Writes an array of 16 bit integers (multiple holding register)
WriteUShortArray
Writes an array of unsigned 16 bit integer (multiple holding register)
WriteInt32Array
Writes an array of 32 bit integers (multiple holding registers)
WriteUInt32Array
Writes an array of unsigned 32 bit integers (multiple holding registers)
WriteFloatArray
Writes an array of 32 bit IEEE floating point numbers (multiple holding registers)
WriteDoubleArray
Writes an array of 64 bit IEEE floating point numbers (multiple holding registers)
WriteLongArray
Writes an array of 64 bit integers (multiple holding registers)
WriteULongArray
Writes an array of unsigned 64 bit integers (multiple holding registers)
Additonal data types (read discrete inputs and input registers) are supported:
Function
Description
ReadOnlyString
Reads an ASCII string (multiple input registers)
ReadOnlyHexString
Reads an HEX string (multiple input registers)
ReadOnlyBool
Reads a boolean value (single discrete input)
ReadOnlyBits
Reads a 16 bit array (single holding register)
ReadOnlyShort
Reads a 16 bit integer (single holding register)
ReadOnlyUShort
Reads an unsigned 16 bit integer (single input register)
ReadOnlyInt32
Reads a 32 bit integer (two input registers)
ReadOnlyUInt32
Reads an unsigned 32 bit integer (two input registers)
ReadOnlyFloat
Reads a 32 bit IEEE floating point number (two input registers)
ReadOnlyDouble
Reads a 64 bit IEEE floating point number (four input registers)
ReadOnlyLong
Reads a 64 bit integer (four input registers)
ReadOnlyULong
Reads an unsigned 64 bit integer (four input registers)
ReadOnlyBoolArray
Reads an array of boolean values (multiple discrete inputs)
ReadOnlyBytes
Reads 8 bit values (multiple input register)
ReadOnlyShortArray
Reads an array of 16 bit integers (multiple input register)
ReadOnlyUShortArray
Reads an array of unsigned 16 bit integer (multiple input register)
ReadOnlyInt32Array
Reads an array of 32 bit integers (multiple input registers)
ReadOnlyUInt32Array
Reads an array of unsigned 32 bit integers (multiple input registers)
ReadOnlyFloatArray
Reads an array of 32 bit IEEE floating point numbers (multiple input registers)
ReadOnlyDoubleArray
Reads an array of 64 bit IEEE floating point numbers (multiple input registers)
ReadOnlyLongArray
Reads an array of 64 bit integers (multiple input registers)
ReadOnlyULongArray
Reads an array of unsigned 64 bit integers (multiple input registers)
Modbus Attribute
This attribute allows to mark a property with a Modbus specific offset, length, and access mode.
Those attributes can be used to access the actual modbus registers (at the specified offset, using the specified numbers).
ModbusApp
The NModbus console application is used to access Modbus slave devices over ethernet (TCP) or serial lines (RTU).
Several commandline options are provided to facilitate the device access and optionally to override settings from the JSON configuration file ("appsettings.json").
All commands have -? and --help options to display usage information.
Root Command
Option
Description
--verbose
Shows the current settings (typically from "appsettings.json")
--config
Shows the all configuration items (environment, settings, etc.)
The root command option --verbose can also be used in all sub commands.
Command
Description
rtu
Supporting standard Modbus RTU operations
tcp
Supporting standard Modbus TCP operations
rtu Command
Option
Description
--com <string>
Sets the Modbus master COM port
--baudrate <number>
Sets the Modbus COM port baud rate
--parity <string>
Sets the Modbus COM port parity
--databits <number>
Sets the Modbus COM port data bits
--stopbits <string>
Sets the Modbus COM port stop bits
--slaveid <number>
Sets the Modbus slave ID
--read-timeout <number>
Sets the timeout in msec (hidden)
--write-timeout <number>
Sets the timeout in msec (hidden)
The rtu command options can also be used in the following sub commands.
Command
Description
read
Supporting standard Modbus RTU read operations
write
Supporting standard Modbus RTU write operations
monitor
Supporting standard Modbus RTU monitor operations
read Command
Option
Description
-c
--coil
Reads coil(s)
-d
--discrete
Reads discrete input(s)
-h
--holding
Reads holding register(s)
-i
--input
Reads input register(s)
-x
--hex
Displays the register values in HEX
-n
--number <number>
The number of items to read
-o
--offset <number>
The offset of the first item to read
-t
--type <string>
Reads the specified data type
write Command
Option
Description
-c
--coil <json>
Writes coil(s)
-h
--holding <json>
Writes holding register(s)
-x
--hex
Writes the HEX values (string)
-o
--offset <number>
The offset of the first item to write
-t
--type <string>
Writes the specified data type
monitor Command
Option
Description
-c
--coil
Reads coil(s)
-d
--discrete
Reads discrete input(s)
-h
--holding
Reads holding register(s)
-i
--input
Reads input register(s)
-x
--hex
Displays the register values in HEX
-n
--number <number>
The number of items to read
-o
--offset <number>
The offset of the first item to read
-t
--type <string>
Reads the specified data type
-r
--repeat <number>
The number of times to read
-s
--seconds <number>>
The number of seconds between reads
The monitor command runs for the number of times specified and can be terminated using Control-C.
tcp Command
Option
Description
--address <string>
Sets the Modbus slave IP address
--port <number>
Sets the Modbus slave port number
--slaveid <number>
Sets the Modbus slave ID
--receive-timeout <number>
Sets the timeout in msec (hidden)
--send-timeout <number>
Sets the timeout in msec (hidden)
The tcp command options can also be used in the following sub commands.
Command
Description
read
Supporting standard Modbus TCP read operations
write
Supporting standard Modbus TCP write operations
monitor
Supporting standard Modbus TCP monitor operations
read Command
Option
Description
-c
--coil
Reads coil(s)
-d
--discrete
Reads discrete input(s)
-h
--holding
Reads holding register(s)
-i
--input
Reads input register(s)
-x
--hex
Displays the register values in HEX
-n
--number <number>
The number of items to read
-o
--offset <number>
The offset of the first item to read
-t
--type <string>
Reads the specified data type
write Command
Option
Description
-c
--coil <json>
Writes coil(s)
-h
--holding <json>
Writes holding register(s)
-x
--hex
Writes the HEX values (string)
-o
--offset <number>
The offset of the first item to write
-t
--type <string>
Writes the specified data type
monitor Command
Option
Description
-c
--coil
Reads coil(s)
-d
--discrete
Reads discrete input(s)
-h
--holding
Reads holding register(s)
-i
--input
Reads input register(s)
-x
--hex
Displays the register values in HEX
-n
--number <number>
The number of items to read
-o
--offset <number>
The offset of the first item to read
-t
--type <string>
Reads the specified data type
-r
--repeat <number>
The number of times to read
-s
--seconds <number>>
The number of seconds between reads
The monitor command runs for the number of times specified and can be terminated using Control-C.
ModbusRTU
The ModbusRTU web application is used to access Modbus RTU slave devices providing a REST based web service.
The Web service can act as a HTTP gateway to Modbus slave devices providing a common well known protocol and the use of JSON for the data exchange.
The Web API page provides a common Web interface for the REST web API using Swagger.
The configuration is stored in the appsettings.json file.
appsettings.json
The typical configuration file contains application settings, the database connection string and the Serilog logger settings.
The TcpSlave settings allow to specifiy the Modbus TCP slave device.
The ModbusTCP web application is used to access Modbus TCP slave devices providing a REST based web service.
The Web service can act as a HTTP gateway to Modbus slave devices providing a common well known protocol and the use of JSON for the data exchange.
The Web API page provides a common Web interface for the REST web API using Swagger.
The configuration is stored in the appsettings.json file.
appsettings.json
The typical configuration file contains application settings, the database connection string and the Serilog logger settings.
The RtuMaster and RtuSlave settings allow to specifiy the Modbus RTU master and slave device.