Configuration¶
A configuration file is YAML with three sections: simulation, listeners and devices. The demo is
configs/demo-site.yaml; sme-modbus-sim validate FILE checks a file without starting anything.
simulation: {tick_seconds: 1, time_scale: 1, history_size: 600, running: true}
listeners:
- {id: tcp1, type: tcp, host: 0.0.0.0, port: 5020, unit_id_mode: strict, unknown_unit: ignore}
- {id: gw1, type: rtu_over_tcp, host: 0.0.0.0, port: 5021}
devices:
- name: Meter-1
unit_id: 3
template: power_meter
word_order: little
points:
- {name: serial_number, value: "MTR00001"}
simulation¶
| Key | Meaning |
|---|---|
tick_seconds |
Behavior update interval in seconds (above 0.05, up to 60) |
time_scale |
Simulated seconds per real second (above 0, up to 3600) |
history_size |
Samples kept per point for the UI chart |
running |
Start running or paused |
listeners¶
| Key | Meaning |
|---|---|
id |
Name used by devices[].listeners |
type |
tcp (Modbus TCP, MBAP), udp (Modbus TCP / MBAP frames in UDP datagrams), tls (Modbus/TCP Security: MBAP over TLS), rtu_over_tcp (RTU frames with CRC-16 over a TCP socket) rtu (serial line, RTU) or ascii (serial line, Modbus ASCII) |
host, port |
tcp / rtu_over_tcp: bind address and TCP port (default 5020; ports below 1024 need privileges). udp: bind address and UDP port (1-65535, default 5020). tls: bind address and TCP port (default 8020; the registered port is 802). rtu / ascii: port is the serial device (COM5, /dev/ttyUSB0) or a pyserial URL (socket://host:port, rfc2217://...), or pty (see below); host is ignored |
unit_id_mode |
strict: only configured unit IDs answer. any: every unit ID is routed to default_unit |
unknown_unit |
In strict mode: ignore (no response, like a real bus) or exception (gateway exception) |
default_unit |
Unit ID used by any mode and for TCP / UDP unit IDs 0 / 255 |
baudrate, bytesize, parity, stopbits |
rtu / ascii only. Defaults 19200 (spec default), 8 (ascii: 7), E, 1. rtu needs 8 data bits; ascii accepts 7 or 8. parity: E / O / N; with N the default is 2 stop bits (Serial Line V1.02). N + stopbits: 1 is accepted as a common but non-conforming setting |
strict_timing |
rtu only (default false). true: a gap longer than t1.5 inside a frame drops it and exactly t3.5 ends a frame. false: no t1.5 check and frames end after max(t3.5, 20 ms) of silence, because USB-serial adapters deliver bytes in bursts |
char_timeout |
ascii only (default 1 s). An unfinished frame is dropped when no character arrives for this long |
link |
port: pty only. Path of a symlink to the device under test's end of the pty pair (e.g. /tmp/ttyV0) |
Modbus UDP (type: udp)¶
- Each datagram carries exactly one Modbus TCP (MBAP) ADU; the answer is sent to the source address of the datagram with the transaction id echoed. The simulator never retransmits.
- No Modbus specification defines Modbus over UDP; ModbusSim follows common practice (for example
modpoll -m udpand the pymodbus UDP client). - A datagram shorter than 8 bytes, whose MBAP length field does not match the datagram length, or whose protocol id is not 0 is dropped (shown in the trace as dropped, with the reason); the listener keeps serving.
- Unit routing (
unit_id_mode,unknown_unit,default_unit, unit IDs 0 / 255) is the same as fortcp. Serial-only keys are rejected as fortcp. - A
udpand atcplistener may use the samehost:port; twoudplisteners on the samehost:portare rejected. - UDP has no connections: the client count in the stats and the UI is the number of distinct source addresses seen in the last 60 s.
Modbus/TCP Security (type: tls)¶
listeners:
- id: secure
type: tls
port: 8020
certificate: pki/server.crt
private_key: pki/server.key
ca: pki/ca.crt
roles:
operator: all
viewer: read
tuner: [3, 6, 16]
- MBAP over TLS, following "MB-TCP-Security v36 (2021-07-30)". The default port is 8020 (the registered port is 802; 8020 needs no privileges, like 5020 for 502).
- Keys, valid for
type: tlsonly. Relative paths are relative to the configuration file.
| Key | Meaning |
|---|---|
certificate |
Server certificate (PEM, may contain the chain). Required |
private_key |
Private key of the server certificate (PEM) |
ca |
CA certificates (PEM) used to verify client certificates |
client_auth |
Default true: a client certificate is required (spec R-07 / R-08), so ca is required too. false: the client certificate is optional |
crl |
Optional CRL (PEM); it checks the leaf client certificate. Needs ca |
roles |
Map role -> all, read or a list of function codes (1-127). Needs ca |
- TLS 1.2 is the minimum, TLS 1.3 is allowed. TLS 1.2 cipher suites are limited to ECDHE with AES-GCM or ChaCha20 (this includes the mandatory
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); no compression. The client host name is not checked (the specification defines no check). - Role. The role is read from the client certificate extension with OID
1.3.6.1.4.1.50316.802.1(a UTF8String). The extension must be non-critical: OpenSSL rejects a certificate with an unknown critical extension, so the handshake fails (CERTIFICATE_VERIFY_FAILED). - Authorization. Without
rolesthere is no authorization. Withroles, a listed role gets its rights; an unlisted role, or a certificate without a role, may only read (FC 01, 02, 03, 04 and 2B/0E). A refused request is answered with exception 01 (Illegal Function, spec R-31) and traced as an rx event with the errornot authorized (role 'x')ornot authorized (no role); the connection stays open. Authorization works by function code only (the specification's inputs are role and function code), not by address range, and it is checked before fault injection. - A failed handshake is traced as the rx event "TLS handshake failed" with the reason, and the connection is closed. Faults apply as on
tcp. - Read-only client:
sme-modbus-sim scan/recordwith--transport tls --host H [--port 802] [--ca ca.crt] [--cert client.crt --key client.key]. The default port is 802; without--cathe server certificate is not verified. In the Web UI and the API the target keys areca,certificateandprivate_key(the three inputs are shown for typetlsonly).
Test PKI with openssl (a test CA, a server certificate and a client certificate with the role operator):
# For testing only. Needs openssl; on Windows run it in Git Bash (add: export MSYS_NO_PATHCONV=1).
mkdir pki && cd pki
cat > ext.cnf <<'EOF'
[server]
basicConstraints = CA:FALSE
subjectAltName = DNS:localhost,IP:127.0.0.1
extendedKeyUsage = serverAuth
[client]
basicConstraints = CA:FALSE
extendedKeyUsage = clientAuth
1.3.6.1.4.1.50316.802.1 = ASN1:UTF8String:operator
EOF
# test CA
openssl req -x509 -newkey rsa:2048 -nodes -keyout ca.key -out ca.crt -days 365 -subj "/CN=Test CA" \
-addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,keyCertSign,cRLSign"
# server certificate
openssl req -newkey rsa:2048 -nodes -keyout server.key -out server.csr -subj "/CN=localhost"
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365 \
-extfile ext.cnf -extensions server
# client certificate carrying the role "operator"
openssl req -newkey rsa:2048 -nodes -keyout client.key -out client.csr -subj "/CN=operator1"
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 365 \
-extfile ext.cnf -extensions client
Serial RTU (type: rtu)¶
- Frame end = silence of t3.5 (3.5 character times of 11 bits; fixed 1.75 ms above 19200 baud); requests of known length are answered without waiting for the silence.
- Unit IDs 1-247 answer as configured; 0 is broadcast (write function codes run on every device of the listener, never answered); 248-255 are ignored.
unknown_unit: exceptionhas no effect on a serial line (unknown units never answer).- If the port cannot be opened, the listener reports
status: errorin/api/stateand the other listeners keep running; a configuration reload retries.
Serial ASCII (type: ascii)¶
- Frame =
:, address, PDU and LRC as hex characters, then CR LF (Serial Line V1.02 section 2.5.2). Lower-case hex is accepted; responses use upper case. - Characters before
:are ignored; a new:discards an unfinished frame, as does a gap longer thanchar_timeout. A bad LRC, an odd number of hex characters or a frame over 513 characters gets no response. port: ptyworks for ASCII too. Addressing, broadcast and fault injection are as for RTU;corrupt_crcinverts the LRC.
Pseudo-terminal pair (port: pty)¶
On Linux, macOS and WSL2 ModbusSim can create the virtual serial port pair itself, with no driver and no root:
- ModbusSim keeps one end of the pair; the device under test opens
link(or the/dev/pts/Npath shown in the log, the UI and/api/stateasdevice) as its serial port. linkis created at startup and removed on exit. If the path already exists, the listener reportsstatus: errorand the existing file is left alone. Put it in a directory you can write to (/devneeds root).- The device under test may close and reopen the port at any time.
- Limits: a pty carries no RTS / CTS / DTR, ignores the baud rate and parity, and does not pace bytes, so t1.5 / t3.5 cannot be tested; keep
strict_timing: false. - The device under test must open the pty as 8N1: on Linux (tested on the WSL2 5.15 kernel), setting even/odd parity or 7 data bits on a pty fails with
EINVAL. The bytes are the same either way, so an ASCII or 8E1 master still works once its serial settings are 8N1. - Not available on Windows: configuration validation fails and suggests a signed virtual serial port driver, a pair of USB-RS485 adapters, or
rtu_over_tcp.
Function codes¶
01, 02, 03, 04, 05, 06, 0F, 10, 16 (mask write register), 17 (read/write multiple registers, write before read) and 2B / 0E (Read Device Identification: read codes 01 basic, 02 regular, 03 extended stream, 04 individual; long answers use More Follows). Serial listeners also answer 07, 08, 0B, 0C and 11 (see below). Writes through 16 / 17 pass the same checks as 06 / 10 (read-only points, partial multi-register values, min / max). Other function codes answer exception 01.
Serial diagnostics (FC 07 / 08 / 0B / 0C / 11)¶
Serial listeners (rtu, ascii) also answer the "Serial Line only" function codes. TCP, UDP and RTU over TCP answer them with exception 01.
| FC | Answer |
|---|---|
| 07 Read Exception Status | 00 |
| 08 Diagnostics | sub-functions 00 echo (data of any length), 01 restart communications (data 0000 keeps the event log, FF00 clears it), 02 diagnostic register (always 0), 03 change ASCII input delimiter, 04 force Listen Only Mode (no answer), 0A clear counters, 0B-12 counters, 14 clear overrun counter |
| 0B Get Comm Event Counter | status 0000 (never busy) and the event counter |
| 0C Get Comm Event Log | status, event counter, bus message count and up to 64 event bytes, newest first |
| 11 Report Server ID | the device's identification.product_code as Server ID, run indicator FF, no additional data |
Counters are 16 bit, wrap around and start at 0 when the listener starts.
- Per listener (the line): bus message (08/0B, every correct frame whatever its address), bus communication error (08/0C, CRC or LRC errors, frames too short, invalid characters, timeouts), character overrun (08/12, always 0: the simulator never loses characters).
- Per device on the listener: exception (08/0D, also errors in broadcasts, which are never answered), server message (08/0E, own and broadcast requests), no response (08/0F, broadcasts and requests left unanswered because of Listen Only Mode or a
no_response/disconnectfault), NAK (08/10, exception 07), busy (08/11, exception 06, for example from thebusyfault), the FC 0B event counter and the FC 0C event log.
Where the specifications are silent or disagree, ModbusSim does this:
- Sub-functions 05-09 answer exception 03; 13 and 15 or above answer exception 01.
- 01 and 0A also clear the bus counters of the listener. 0A does not clear the event log.
- Listen Only Mode: requests are still counted and logged but not executed or answered; only 08/01 is executed, still without an answer. The event byte for entering the mode is
04. Broadcast writes skip a device in Listen Only Mode. - A broadcast executes only write function codes; any other function code in a broadcast (diagnostics included) is not executed and counts as an exception.
- The event counter counts every request completed without an exception, broadcasts included, except FC 0B and 0C.
- 03 changes only the delimiter the listener expects at the end of ASCII requests (one setting per listener, shared by its devices); answers still end with CR LF. 01 restores LF. On an RTU listener 03 is echoed and has no effect.
- Fault injection: an
exceptionorbusyfault counts like the real exception;no_responsecounts as not answered.
devices¶
| Key | Meaning |
|---|---|
name, description |
Device name (unique) and free text |
unit_id |
1-247 |
listeners |
Listener IDs this device is attached to (default: all) |
template, params |
Generate points from a template, see Behaviors & templates |
byte_order, word_order |
Device defaults: big or little |
undefined_address |
exception (illegal data address, default) or zero for reads outside defined points |
identification |
vendor_name, product_code, revision, vendor_url, product_name, model_name, user_application_name (served by FC 2B / 0E; defaults SmartEdge, SME-MODBUS-SIM, program version) |
points |
Points of this device; entries with the name of a template point override it |
removed |
Template points deleted by the user |
points¶
| Key | Meaning |
|---|---|
name, description, units |
Identification |
table |
coil, discrete, input or holding |
address |
0-based protocol address; omitted addresses are allocated automatically |
datatype |
bool, bit, uint16, int16, uint32, int32, float32, uint64, int64, float64, string, enum |
count |
Registers of a string (two characters each) |
bit |
Bit number 0-15 for datatype: bit (one bit of a holding / input register) |
byte_order, word_order |
Override the device defaults |
scale, offset |
Register value = (engineering value - offset) / scale |
access |
r or rw; writes to r points return exception 02 |
value |
Initial value |
min, max |
Clamp the value |
labels |
enum mapping, raw value to label (quote labels such as "ON") |
behavior |
See Behaviors & templates |
Byte order and word order¶
For multi-register values, word_order decides which register comes first and byte_order the byte order inside
a register. A float32 of 1.0 (0x3F800000) is sent as:
| byte / word order | Registers | Name |
|---|---|---|
| big / big | 3F80 0000 |
ABCD (Modbus standard) |
| big / little | 0000 3F80 |
CDAB (common for meters) |
| little / big | 803F 0000 |
BADC |
| little / little | 0000 803F |
DCBA |
Coupled site example¶
configs/demo-coupled-site.yaml couples five devices on one TCP listener (port 5020) with generic templates and
expressions only, so an EMS can close the loop against it:
| Device (unit) | Point | Behavior |
|---|---|---|
PV-1 (1, custom) |
p_ac |
40 * max(0, sin(pi * (hour - 6) / 12)) * cloud_factor (daylight 06:00-18:00; cloud_factor is a random walk 0.7-1.0) |
Load-1 (2, custom) |
active_power |
20 + 15 * exp(-((hour - 8) ** 2) / 4) + 25 * exp(-((hour - 19) ** 2) / 6) + noise(0.5) |
PCS-1 (3, battery_pcs) |
p_actual |
Ramps to p_setpoint at 10 kW/s while run_command is on (blocked at the BMS SOC limits) |
BMS-1 (4, bms) |
soc |
Counter with rate -p_actual / capacity_kwh * 100 / 3600 (% per second), clamped 0-100 |
Meter-1 (5, power_meter) |
active_power |
pv('Load-1', 'active_power') - pv('PV-1', 'p_ac') - pv('PCS-1', 'p_actual') |
Signs: PCS power is + discharge / - charge; meter power is + import / - export, and its import / export energy
counters integrate it. Behaviors run device by device in file order, so the meter (listed last) uses values of the
same tick. Try: write holding 0 = 200 (20.0 kW) and coil 0 = 1 on unit 3, then watch the meter import fall by 20 kW
and BMS-1 soc decrease.