Skip to main content

VirtualPLC

A local Mitsubishi MC protocol PLC simulator for client tests, with sparse memory, CPU control and declarative faults. Python 3.10+, standard library only.

AI agents: run virtual-plc guide for a compact usage guide; use --json for data.

1E error codes and OutOfRange: BEHAVIOR and memory API semantics; compare_observations / observation_template: PHYSICAL_ORACLE.

30-second quickstart

Install with pip install virtual-plc, then choose a recipe:

virtual-plc --port 5000 --text-pad 0x20
virtual-plc --profile iQ-R --port 5001 --initial-state STOP
virtual-plc --init seed.json --fault-plan faults.json --dump-on-exit saved.json

The third recipe needs the JSON files shown below. From a checkout, install with python -m pip install -e .. python -m virtual_plc is the equivalent entry point. For more startup details, run virtual-plc guide run.

Install the tested upstream client with pip install pymcprotocol==0.3.0. This exact version is the repository's compatibility oracle; the separate client compatibility matrix distinguishes it from our unreleased greatgc-flow/pymcprotocol fork and unverified real-PLC behavior:

from pymcprotocol import Type3E

client = Type3E(plctype="Q")
client.connect("127.0.0.1", 5000)
try:
    client.batchwrite_wordunits("D0", [123, -45])
    assert client.batchread_wordunits("D0", 2) == [123, -45]
finally:
    client.close()

For the iQ-R recipe (virtual-plc --profile iQ-R --port 5001 --initial-state STOP):

from pymcprotocol import Type3E

client = Type3E(plctype="iQ-R")
try:
    client.connect("127.0.0.1", 5001)
    assert client.batchread_wordunits("D0", 1) == [0]
finally:
    client.close()

Safety

Local-only binding is enforced by default in the CLI and Python API. Passwords simulate PLC locking; there is no network authentication. Never connect to real equipment or production networks. If an isolated test network really needs a non-loopback listener, explicitly opt in and set an idle timeout:

virtual-plc --host 0.0.0.0 --allow-nonlocal --idle-timeout 30 --max-clients 16

The non-local override is not authentication; use only on a trusted, isolated test network. max_clients defaults to 64; excess connections are closed without allocating workers. Embedded servers use Server(host="0.0.0.0", allow_nonlocal=True, idle_timeout=30, max_clients=16). A non-loopback host without explicit permission (or without an idle timeout when permitted) raises a configuration error before listening.

Sample project: learn by running

New here? The bottling-line sample walks through five runnable steps with pymcprotocol and VirtualPLC: a hello round trip, a whole line with a simulated ladder program, fault injection, the CLI as a separate process, and pytest fixtures for your own client. It needs no hardware and ships with expected output.

Multiple TCP/UDP listeners

The legacy command virtual-plc --port 5000 still opens one TCP port. Repeat --listen to expose several ports on one shared virtual PLC:

virtual-plc --listen tcp:5000 --listen tcp:5001 --listen udp:5000

Each endpoint shares memory, CPU state, authentication-simulation lock and the request serialization lock. Different transports can reuse the same numeric port (TCP/5000 and UDP/5000). For a completely independent virtual PLC with different memory, start another process. --listen replaces the legacy --port option and may not be combined with it; --host applies to every endpoint.

The same topology is available to embedded Python programs:

from virtual_plc import Server

with Server(listeners=[("tcp", 0), ("udp", 0)]) as plc:
    print(plc.addresses)  # [("tcp", host, assigned_port), ("udp", host, assigned_port)]
    # Use a TCP client or an MC request datagram to either bound address.

For UDP, send exactly one complete 3E/4E binary or ASCII MC request frame per datagram; replies are datagrams sent back to the source IP/port. Invalid, truncated, concatenated or over-8192-byte datagrams are silently dropped. UDP is connectionless: no TCP stream reassembly, session/idle timeout, or retransmission is provided. Use application-level timeouts/retries and keep UDP packets small enough to avoid IP fragmentation. Stock pymcprotocol client connections are TCP; UDP clients must use a datagram-capable client. The TCP-oriented --fault-plan is rejected if any UDP listener is enabled, rather than pretending its close/RST/partial-send actions work for UDP. --max-clients limits TCP sessions only.

Nonlocal network binds continue to require --allow-nonlocal and --idle-timeout; that timeout does not add UDP session semantics. There is no authentication; UDP rate limiting is off unless --udp-rate-limit is set (per source IP; flood protection, not access control). Run on loopback or on an isolated, trusted test network only; never connect to production PLCs.

For local UDP diagnostics and opt-in TCP session counters (Server(tcp_metrics=True)), see UDP observability.

UDP response faults (optional)

UDP listeners support the independent datagram plan (drop, delay, duplicate, truncate, corrupt_header); see FAULTS.

Standalone UDP client example (source checkout)

The pinned pymcprotocol==0.3.0 client uses TCP. For UDP, use the stdlib-only, loopback-only (127.0.0.1 or --host ::1) 3E binary D0 reader. It sends exactly one complete request datagram and validates the sender, frame header, length, status and word size; malformed replies fail closed. No retry, authentication, physical PLC compatibility or nonlocal target.

python -m virtual_plc --listen udp:5000
python examples/udp_read_d0.py 5000

Seed D0 via an --init JSON file if you want a nonzero response. See client compatibility for the distinction between this raw UDP example and the tested TCP-only pinned client.

CLI reference

Run virtual-plc guide options for the generated flag/default table, or virtual-plc --help; see virtual-plc guide run for quick-start flags. Long options must be spelled in full; abbreviations exit 2.

Other options (meanings in virtual-plc guide options): lock and caps --password, --max-points; ASCII wire --ascii-dword-order, --ascii-numbering; memory --limit, --latch-range, --max-cells (optional stored-point cap); UDP faults --udp-fault-plan; UDP flood protection --udp-rate-limit N/SECONDS (per source IP; semantics); 1E --slmp-1e-run-switch; -v/--verbose (stderr request log); --version. JSON files for --init, --fault-plan, --udp-fault-plan and --ethernet-config may start with a UTF-8 BOM. The --dump-on-exit directory must already exist and be writable, and an existing target file must not be read-only (probed with a temporary file before binding, exit 2). Empty FILE paths exit 2.

Guide topic Contents
virtual-plc guide run Installation, startup and shutdown
virtual-plc guide options All flags and defaults, generated from argparse
virtual-plc guide client Client reads/writes, control and addressing
virtual-plc guide profiles Generated command/frame/wire/profile/device matrix
virtual-plc guide ethernet Hardware model settings, overrides and offline reference JSON
virtual-plc guide embed Python API and pytest fixture
virtual-plc guide faults Plan schema, actions and examples
virtual-plc guide memory Seeding, snapshots, ranges and devices
virtual-plc guide troubleshoot Symptoms and fixes
virtual-plc guide divergences Simulator policies and unverified PLC behavior

Every topic accepts --json.

MELSEC iQ-F SLMP 1E mode (opt-in)

Enable with --slmp-1e; this selects a separate 1E-only parser on synthetic Q memory. See BEHAVIOR for command bounds, diagnostics, remote-control policy and error codes; this is not FX5 hardware parity.

Offline physical-oracle preparation

For separately and manually captured read-only 1E 15H model and 16H loopback responses, use python -m virtual_plc.oracle plan, template, and compare FILE --report REPORT.json. This offline utility never contacts a PLC and never certifies real hardware. See offline oracle procedure.

FX5 vendor SLMP port preflight (offline reference)

Offline helper virtual_plc.ethernet.inspect_reference_slmp_port(model, port) checks vendor recommended and reserved port ranges; see virtual-plc guide ethernet --json and FX5 port scope.

RnCPU built-in service connection settings (offline reference)

Service-specific connection rules for RnCPU built-in Ethernet (auto-open UDP vs per-GOT TCP); see virtual-plc guide ethernet --json and RnCPU service connection scope.

FX5 CPU firmware compatibility (offline reference)

Offline helper virtual_plc.ethernet.inspect_reference_fx5_simple_cpu_communication(model, firmware_version=None) checks model/firmware support gates; see virtual-plc guide ethernet --json and FX5 firmware matrix.

FX5-ENET existence-check settings (offline reference)

Offline helper virtual_plc.ethernet.inspect_reference_fx5_enet_alive_timers() validates module alive-check timer ranges; see virtual-plc guide ethernet --json and FX5-ENET source matrix.

RnCPU built-in Ethernet remote RUN/STOP setting check (offline reference)

Offline helper virtual_plc.ethernet.inspect_reference_rncpu_remote_opening() checks vendor opening settings; see virtual-plc guide ethernet --json and RnCPU opening guide.

R04CPU built-in Ethernet A038H offline timer preflight

Offline helper virtual_plc.ethernet.inspect_reference_rncpu_data_timers() validates R04CPU timer references; see virtual-plc guide ethernet --json and R04CPU timer guide.

R04CPU built-in Ethernet external-device settings (offline)

Offline helper virtual_plc.ethernet.inspect_reference_rncpu_external_device() checks user slot and port ranges; see virtual-plc guide ethernet --json and R04CPU port matrix.

R04CPU external-device multi-entry offline review

Offline helper virtual_plc.ethernet.inspect_reference_rncpu_external_device_batch() lints multiple configuration rows; see virtual-plc guide ethernet --json and R04CPU batch lint guide.

GX Works2/3 model-specific Ethernet settings (opt-in)

GX Works2/3 is an editor, not the hardware identity. This source checkout supports qj71e71 (Q / GX Works2), lj71e71 (L / GX Works2), and rj71en71 (iQ-R / GX Works3). Factory defaults, validated JSON overrides and a nonbinding effective-settings inspection are in the Ethernet settings guide. The official-manual research matrix documents model boundaries and unresolved physical fidelity. See the RnCPU/FX5 hardware gap matrix before using an unsupported model's defaults. The existing guide ethernet --json exposes FX5 built-in, FX5-ENET and RnCPU built-in as reference-only, not runnable hardware; unknown timer defaults remain unknown.

# Inspect defaults and configurable ranges without listening:
virtual-plc --ethernet-model qj71e71 --show-effective-ethernet
virtual-plc --ethernet-model lj71e71 --show-effective-ethernet
virtual-plc --ethernet-model rj71en71 --show-effective-ethernet

# Load optional JSON changes, share PLC state across explicit TCP/UDP ports:
virtual-plc --ethernet-model qj71e71 --ethernet-config ethernet.json --listen tcp:5000 --listen udp:5000
virtual-plc guide ethernet --json

The hardware model automatically selects its matching wire profile in the CLI; explicitly conflicting --profile values are errors. JSON overrides preserve whether each key was explicitly specified, even if equal to its factory value. Selecting a model simulates its binary/ASCII data-code setting and RUN-time MC write permission. Only those settings change runtime behavior; module TCP/IP retransmit, response monitoring and alive timers are explicitly marked reference_only, not substituted for --idle-timeout. Legacy --port 5000 and Q behavior remain unchanged without a model selection. Independent PLCs run in separate OS processes; all listeners within one process share the same PLC state.

Profiles, frames and encodings

Commands support 3E/4E and binary/ASCII on all profiles. Q, L, QnA and iQ-L share a wire class; iQ-R uses different descriptors/subcommands. Client plctype and server --profile must agree for iQ-R. Inspect virtual-plc guide profiles --json for the current matrix and device kinds.

This local recipe covers every profile/frame/wire combination:

from itertools import product
from pymcprotocol import Type3E, Type4E
from virtual_plc import Server

for profile in ("Q", "L", "QnA", "iQ-L", "iQ-R"):
    with Server(profile=profile) as server:
        for kind, wire in product((Type3E, Type4E), ("binary", "ascii")):
            client = kind(plctype=profile)
            client.setaccessopt(commtype=wire)
            client.connect(*server.address)
            try:
                client.batchwrite_wordunits("D0", [123])
                assert client.batchread_wordunits("D0", 1) == [123]
            finally:
                client.close()

Memory seeding, snapshots and embedding

Save seed.json (decimal address keys, unsigned native point values):

{"D": {"100": [1, 2, 3]}, "M": {"0": [1, 0, 1]}}

--init loads before listening; --dump-on-exit saves the same schema after graceful shutdown. The snapshot is written to a same-directory temporary file, flushed and atomically replaced on successful completion. A failed write leaves an existing snapshot untouched; abrupt process or OS termination may still prevent saving the latest state. Dumps preserve explicit zeros and include SD203. Loads validate the complete snapshot before overlaying data. See virtual-plc guide memory for bounds, retention and device names.

Latch ranges are cleared by latchclear and preserved by remote_run(clear_mode=1). SM and SD are never cleared; latchclear requires STOP.

from virtual_plc import Server, Memory, FaultPlan, __version__

memory = Memory()
memory.load({"D": {"100": [1, 2, 3]}})
with Server(memory=memory, fault_plan=FaultPlan([])) as server:
    print(server.address, __version__)
    # Connect clients here. Context exit stops and joins workers.
snapshot = memory.dump()

Seed before start/context entry; dump after stop. Direct Memory calls are not synchronized with requests. Use clients for live writes. CLI --init seeding runs before server construction, so --initial-state sets SD203 (a snapshot's SD203 is overwritten). Writing SD203 changes the word without changing CPU state. For a pytest fixture, run virtual-plc guide embed.

Response faults

Save faults.json and pass --fault-plan faults.json:

Minimal example:

{"steps": [{"match": {"command": "0x0619"}, "action": {"type": "delay", "seconds": 0.02}}]}

See FAULTS for the contract.

Troubleshooting

  • Large reads: pinned upstream pymcprotocol 0.3.0 calls recv(4096) once. Split batches; the server's point limit is separate. A raw reader can follow response lengths.
  • CPU names: default Q returns "VirtualPLC-Q\x00\x00\x00\x00". The client retains NULs; --text-pad 0x20 returns "VirtualPLC-Q". Non-UTF-8 fill such as 0xFF causes client decoding failure.
  • Reset requires STOP. On receive/status failure, the client waits about 1 s and reconnects. A normal reset keeps the connection open.
  • With pinned upstream pymcprotocol 0.3.0, truncated or closed binary replies can look like status/value 0 because that client does not validate headers/lengths. This is not proof of success.
  • Idle timeout is disabled by default; explicit timeouts close idle or incomplete connections. Reconnect afterward.

More fixes: virtual-plc guide troubleshoot. Protocol details: BEHAVIOR.

Known divergences from a real PLC

virtual-plc guide divergences (DIVERGENCES in virtual_plc/guide.py) is the canonical list of simulator policies and unverified PLC behavior; see BEHAVIOR for wire protocol details.

Repository safeguards and verified handoff

For the repository, use the branch/release gate checklist before configuring main protection or creating a v* tag. The handoff log records current state and CI evidence (older prompts under docs/handoff/ are historical); fetch remote main first. An active GitHub ruleset cannot be inferred from a successful CI run.

Development and releases

python -m pip install -e . -r requirements-dev.txt
python -m pytest -q

The dev extra installs pytest only. The test extra adds the compatible oracle range declared in pyproject.toml. requirements-dev.txt is the only exact client pin. Dependabot updates that file and pyproject; the coverage guard rejects newly uncovered public methods, device constants and lookup-only names.

The wheel hash is checked when a matching-version wheel is retained under .venv/oracle. A bumped oracle skips only the stale artifact-hash check; the full suite and coverage guard still run. Oracle watch installs the latest client and changes only that run's local version expectation, without committing the change.

python -m pip install -r requirements-release.txt
python -m build
python -m twine check dist/*

CI tests Windows/Linux on Python 3.10–3.14 (plus one macOS job), runs the bundled tests from the extracted sdist, and installs built wheels in clean environments. Tags must match pyproject version and point at a commit whose main CI succeeded; trusted publishing requires the PyPI publisher and GitHub environment pypi to be configured.

See DESIGN, BEHAVIOR and FAULTS. Licensed under MIT.

Metadata

Release files for virtual-plc 0.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for virtual-plc 0.10.0
File Size Uploaded Provenance Actions
virtual_plc-0.10.0.tar.gz 377.9 kB With provenance Details for virtual_plc-0.10.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for virtual-plc 0.10.0
File Interpreter ABI Platform Actions
virtual_plc-0.10.0-py3-none-any.whl Python 3 none any Details for virtual_plc-0.10.0-py3-none-any.whl

Total release size: 456.3 kB

Release files / virtual_plc-0.10.0.tar.gz

Download URL virtual_plc-0.10.0.tar.gz
Size 377.9 kB
Tags Source
SHA-256 checksum
How to use checksums
871d27de4b09cfabfdb12f7de2169ac8ec62d6611bb170a1267130b4ff8b6aa5
BLAKE2b-256 checksum
How to use checksums
bf7d83fd5dbeef5837305816ca10fc92a9dfb1c55b603b75dba57208d7aca52a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / virtual_plc-0.10.0-py3-none-any.whl

Download URL virtual_plc-0.10.0-py3-none-any.whl
Size 78.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2bf055bb6234b85bec1fc0981e66bf42596d2312c830295626008f4a6331f56e
BLAKE2b-256 checksum
How to use checksums
98d542c87685ccb0460fa883aa4616f580dcc174550f4b76cd7a809ee70afa18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for virtual-plc 0.10.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for virtual_plc-0.10.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page