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.

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 client separately with pip install pymcprotocol:

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()

Use localhost only. Passwords simulate PLC locking; there is no network authentication. Never connect to real equipment or production networks. Non-local binds print a warning, not an access control.

CLI reference

Run virtual-plc guide options for the option/default/meaning table generated from the parser, or virtual-plc --help. For programs, use virtual-plc guide options --json. Bad configuration exits 2 with a topic hint; bind failures exit 1 without a traceback.

The stderr banner reports the bound address (including the assigned port for --port 0), profile/state, idle timeout, text pad, dword order and fault plan. Ctrl+C, SIGTERM and Windows SIGBREAK stop workers and save exit snapshots. SIGBREAK needs a shared console; forced termination bypasses Python handlers. -v logs request summaries and close reasons, excluding payloads/passwords.

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 embed Python API and pytest fixture
virtual-plc guide faults Plan schema, actions and three 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. Unknown topics list the choices and exit 2.

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. 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. Server construction initializes SD203; CLI seeding runs afterward. 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:

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

Faults run after execution and do not roll back writes. Run virtual-plc guide faults for the schema and examples; see FAULTS for delivery semantics.

Troubleshooting

  • Large reads: the client 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.
  • Truncated or closed binary replies can look like status/value 0 because the 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

  • CPU identities, ranges and SD203 state encoding are synthetic; SD203 mirrors state in one direction. CPU names default to NUL fill on both encodings; the client retains NULs. Physical fill remains unverified.
  • ASCII addresses use decimal text even for hex-base devices. Dword order defaults to low-word-first (swapped); client selects high-word-first. Physical order is unverified; roundtrips remain symmetric.
  • Locking is server-wide, survives disconnect and gates only CPU control. It provides no authentication. Native iQ-R dword stride is one 32-bit point per address; this and physical lock/reset policies are unverified.
  • Timers/counters store values without execution. Monitoring timers are parsed/ignored; routes alias one memory/state. Error meanings await checks.

Run virtual-plc guide divergences for the compact list of simulator policies.

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 build twine
python -m build
python -m twine check dist/*

CI tests Windows/Linux on Python 3.10–3.13 and installs built wheels in clean environments. Tags must match pyproject version; 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.5.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.5.0
File Size Uploaded Provenance Actions
virtual_plc-0.5.0.tar.gz 120.3 kB With provenance Details for virtual_plc-0.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 152.8 kB

Release files / virtual_plc-0.5.0.tar.gz

Download URL virtual_plc-0.5.0.tar.gz
Size 120.3 kB
Tags Source
SHA-256 checksum
How to use checksums
453c14c6722bd8aba25fa75f70db80de2ba7fd4fc3c7e001ea72d7c0d0e10f61
BLAKE2b-256 checksum
How to use checksums
dbe8d570716b1a1c94c86974d78dd5f7e399fe36e3949f393340e6eabf088ff2
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 Oct 30, 2026.

Transparency log

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

Download URL virtual_plc-0.5.0-py3-none-any.whl
Size 32.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8c5d7ad8e02021455c36fb7162726ecb868c46577a2b02bc747ba898def99da5
BLAKE2b-256 checksum
How to use checksums
abcb7176690cd281719e69fc16abc008437a9f2ca5aeeea899859c9e382434bb
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 Oct 30, 2026.

Transparency log

Security for virtual-plc 0.5.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.5.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 Oct 30, 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