|
ModbusTCPServer class |
Top |
|
ModbusTCPServer
Connection to Modbus TCP Server simulator Since R2019b
Description A ModbusTCPServer object represents one Modbus TCP Server node for communication with the Modbus TCP Master. After creating the object, use dot notation to call methods. We only support one Modbus TCP Master. However if you use port+1 as TCP Port, we can support another Modbus TCP Master. For example, default port=502, we can support 2 Modbus TCP masters with port =502 and 503.
Notes: For Modbus TCP Server, we can simulate only one server. We call one consecutive Modbus register addresses "Segment". For one kind of registers, we have no limit to segment numbers.
Creation
Syntax
m = ModbusTCPServer() m = ModbusTCPServer(port) m = ModbusTCPServer(port, Name, Value) m = ModbusTCPServer( Name, Value)
Description m=ModbusTCPServer() or m = ModbusTCPServer(port) object connects to the Modbus TCP network by USB or Bluetooth serial port specified by "COM" (default "COM1"). It will use default Modbus parameters for connecting. m = ModbusTCPServer(port, Name, Value) or m = ModbusTCPServer( Name, Value) connects to the Modbus TCP network by USB or Bluetooth serial port specified by "COM" (default "COM1"). And sets additional private properties using optional name-value pair arguments.
Input Arguments
port -- TCP port number. Default=502 Value is scalar double. We provide 2 ports for TCP Server. One is port, the other is port+1. Each port can only connect one Modbus TCP Master. So we can connect 2 Modbus TCP Masters
Name-Value Arguments Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after port or first string/char array if no port parameter. But the order of the pairs does not matter. Please, use commas to separate each name and value, and enclose Name in quotes. For example, m=ModbusRTUASCiiServer("COM", "COM3","ServerID",3,"SupportedHoldingRegsSegs",{[1 10]}, "SupportedInputRegsSegs", {[22 25]}); It will set Server ID =3. Its holding register range is address 1 to 10, and Input register range is address 22 to 25. You can use Name-Value pairs to set the following arguments:
Value is scalar double: 1 to 247. Default=1
Value is scalar string or char array. It denote the words order when register data type is "uint32/int32/single/uint64/int64/double". Valid value is "big-endian" or "little-endian". Default="big-endian"
Value is scalar string or char array. It denote our Serial Port which is used in Modbus Slave Simulator USB or Bluetooth. Only when correct hardware used, we can use Modbus TCP server.
Value is logical scalar. True means Server will respond to Master if IP address and port number are correct no matter what Server ID is. Default = false
Value is cell, cell element is 2 elements' row vector. It tells system how many continuous Holding register segments to support. Row vector denotes "Start address" and "End address". For example, {[1 10], [15 20]} denotes it supports 2 read-write holding register segments, and the 1st segment address is from 1 to 10, the 2nd segment address is from 15 to 20. Compared with Modbus RTU/ASCii Server, we have no limit for segment QTY. Default={[1 65536]}
Value is cell, cell element is 2 elements' row vector. It tells system how many continous read-only input regsiser segments to support. Row vector denotes "Start address" and "End address". For example, {[31 40], [45 120]} denotes it supports 2 read-only input register segments, and the 1st segment address is from 31 to 40, the 2nd segment address is from 45 to 120. Default={[1 65536]}
Value is logical true or false. true means we will use holding register to replace input registers for read-only registers. Default=false
Value is logical true or false. true means we support Discrete/Coil registers. Default=true
Value is cell, cell element is 2 elements' row vector. It tells system how many continuous coil register segments to support for Device 1 to 4. Row vector denotes "Start address" and "End address". For example, {[1 10], [15 20]} denotes it supports 2 read-write coil register segments, and the 1st segment address is from 1 to 10, the 2nd segment address is from 15 to 20. Default={[1 65536]}
Value is cell, cell element is 2 elements' row vector. It tells system how many continuous read-only discrete register segments to support. Row vector denotes "Start address" and "End address". For example, {[31 40], [45 120]} denotes it supports 2 read-only discrete register segments, and the 1st segment address is from 31 to 40, the 2nd segment address is from 45 to 120. Default={[1 65536]}
In general, you only need to set up USB or Bluetooth serial port, the default behaviour is Server ID=1. And the holding register address is from 1 to 65536, input register address is from 1 to 65536, coil register address is from 1 to 65536, discrete register address is from 1 to 65536, TCP port=502. The following statement is usually used in general project:
m=ModbusTCPServer("COM", "COM5");
In the following example, we support Modbus TCP Server with server ID=3, port=502, Read-write word register address ranges are 1 to 100 , 500 to 560 and 900 to 2048, Read-only word register address ranges is 220 to 250. We use holding register to replace read-only input register. It means that it does not support input word registers, instead, we use read-only holding regs to replace them. The coil bit register address ranges is 1 to 1500. The discrete bit register address ranges is 2 to 1000. We don't support watchdog.
m=ModbusTCPServer("COM", "COM5","ServerID",3,"SupportedHoldingRegsSegs",{[1 100], [500 560], [900 2048]}, "SupportedInputRegs1to4Segs", {[220 250]}, ... "SupportedCoils1to4Segs", {[1,1500]},"SupportedDiscreteRegs1to4Segs",{[2,1000]},"HoldingReplaceInputRegs1to4",true);
Properties
scalar string or char array. It denote the words order when register data type is "uint32/int32/single/uint64/int64/double". Valid value is "big-endian" or "little-endian". Default="big-endian". This is public Property, so you can change it in the run-time by assignment
Value is scalar double: 1 to 247. Default=1. This is public Property, so you can change it in the run-time by assignment
Object Functions (Or Methods)
Events
None
All methods details
read Server registers values Since R2019b
Syntax
[moddata,Error] = read(obj,target,address) [moddata,Error] = read(obj,target,address,count) [moddata,Error] = read(obj,target,address,count,precision)
Description
This function will get my Server register values from one of four target addressable areas: Coils, Inputs, Holding Registers, or Input Registers. Usually we only need to read Coils and Holding registers because Inputs (bits) and Input Registers (words) values are set by ours. We should know their values.
Input Arguments
'single' and 'double' conversions conform to the IEEE 754 floating point standard. For signed integers a two's complement conversion is performed. Note that 'precision' does not refer to the return type (always 'double'), it only specifies how to interpret the register data.
Precision is a string or char array when doing reads of the same data type. For reading multiple contiguous registers containing different data types, precision must be a cell array of strings or character vectors, or a string array of precisions. The number of precision values must match the number of count values.
Only for tagets: inputregs and holdingregs.
Output Arguments
Row vector for reading result in double number type. It will be [ ] if failed
Logical value. True means fail to read.
Examples
Firstly, we set up a Modbus TCP Server object with COM5 and Server ID is 3. They support Read-write holding registers addressing from 1 to 100. They support Read-only input registers addressing from 22 to 25. They support coil registers addressing from 1 to 15. They support discrete registers addressing from 2 to 8. Word order is big-endian.
m=ModbusTCPServer(502, "COM", "COM5","ServerID",3,"SupportedHoldingRegsSegs",{[1 100]}, "SupportedInputRegsSegs", {[22 25]}, ... "SupportedCoilsSegs", {[1,15]},"SupportedDiscreteRegsSegs",{[2,8]});
% Read 3 coil values starting at address 5
address = 5; [moddata,Error] = read(m,'coils',address,3);
% Read 2 holding registers whose data format is unsigned 16 bits integer and 4 holding registers % whose data format is double, in one read, at address 10. address = 10; precision = {'uint16', 'double'}; count = [2, 4]; [moddata, Error] = read(m, 'holdingregs', address, count, precision);
write Server read-only registers or discrete bit registers. Since R2019b
Syntax
Error=write(obj,target,address,values) Error=write(obj,target,address,values,precision)
Description
This function will perform a write operation to one of two simulated target addressable areas: inputs (Discrete) or input Registers (read-only word registers).
Input Arguments
The values passed in to be written will be converted to register values based on the specified precision. 'single' and 'double' conversions conform to the IEEE 754 floating point standard. For signed integers a 2's complement conversion is performed.
Output Arguments
Logical value. True means fail to write.
Examples Firstly, we set up a Modbus TCP Server object with COM5 and Server ID is 3. They support Read-write holding registers addressing from 1 to 100. They support Read-only input registers addressing from 22 to 25. They support coil registers addressing from 1 to 15. They support discrete registers addressing from 2 to 8. Word order is big-endian.
m=ModbusTCPServer("COM", "COM5","ServerID",3,"SupportedHoldingRegsSegs",{[1 100]}, "SupportedInputRegsSegs", {[22 25]}, ... "SupportedCoilsSegs", {[1,15]},"SupportedDiscreteRegsSegs",{[2,8]});
% set the input word registers at address 22 to the value 2000
Error = write(m,'inputregs',22,2000);
Clear all register values Device supports to zero. Since R2019b
Syntax
Error=resetDevice(obj)
Description
This function will put all registers simulator supports to zero.
Input Arguments
Output Arguments
Logical value. True means fail to reset.
Enable Device Modbus communication Since R2019b
Syntax
Error=enableDevice(obj, enabled)
Description
This function will enable/disable simulated server
Input Arguments
Output Arguments
Logical value. True means fail to enable/disable.
Note: If we never call this enableDevice method, all devices are enabled.
|