Skip to content

Getting started

Start here

Use this page to make your first safe SLMP read and write with the high-level API. Start with one D register before trying routed devices, module buffers, or long timer families.

Prerequisites

Requirement Value
Python >=3.10
PLC host 192.168.250.100
TCP port 1025
First profile melsec:iq-r
First read target D100

Install

pip install plc-comm-slmp

Choose your PLC profile

plc_profile in SlmpConnectionOptions is the only required PLC selector for the public helper layer. The library derives the frame type and access mode from it. This is intentional. ReadTypeName and model-code data are diagnostic only because some PLCs or communication paths cannot return a useful type name. If a model-to-profile conversion guesses wrong, the library can use the wrong address grammar or device-range catalog. Let a human, configuration file, or UI choose the canonical plc_profile.

from slmp import SlmpConnectionOptions, SlmpTarget


def main() -> None:
    options = SlmpConnectionOptions(
        host="192.168.250.100",
        port=1025,
        transport="tcp",
        plc_profile="melsec:iq-r",
        default_target=SlmpTarget(network=0, station=0xFF, module_io=0x03FF, multidrop=0),
    )
    print(options.plc_profile)


main()

First read

import asyncio
from slmp import SlmpConnectionOptions, open_and_connect, read_typed, SlmpTarget


async def main() -> None:
    options = SlmpConnectionOptions(
        host="192.168.250.100",
        port=1025,
        transport="tcp",
        plc_profile="melsec:iq-r",
        default_target=SlmpTarget(network=0, station=0xFF, module_io=0x03FF, multidrop=0),
    )
    async with await open_and_connect(options) as client:
        value = await read_typed(client, "D100", "U")
        print(f"D100={value}")


asyncio.run(main())

Expected output:

D100=0

Your value may be different because it is read from your PLC.

First write

Only write to an address that your PLC program reserves for testing.

import asyncio
from slmp import SlmpConnectionOptions, open_and_connect, read_typed, write_typed, SlmpTarget


async def main() -> None:
    options = SlmpConnectionOptions(
        host="192.168.250.100",
        port=1025,
        transport="tcp",
        plc_profile="melsec:iq-r",
        default_target=SlmpTarget(network=0, station=0xFF, module_io=0x03FF, multidrop=0),
    )
    async with await open_and_connect(options) as client:
        original = await read_typed(client, "D100", "U")
        write_confirmed = False
        try:
            await write_typed(client, "D100", "U", 42)
            write_confirmed = True
            value = await read_typed(client, "D100", "U")
            print(f"D100={value}")
        finally:
            if write_confirmed:
                await write_typed(client, "D100", "U", original)


asyncio.run(main())

The example restores the saved value after a confirmed write, including when readback fails. If the write result is outcome-unknown, it deliberately avoids a blind restore or retry: reopen the client, inspect D100, and reconcile the actual value explicitly. If restoration fails, also inspect and reconcile the register manually; do not assume that the saved value was restored.

Expected output:

D100=42

Confirm success

  1. Confirm your PLC accepts a TCP connection to 192.168.250.100:1025.
  2. Confirm the PLC-side communication data code is Binary and the port/open setting matches your transport; see the MELSEC SLMP PLC Setup Guide.
  3. Confirm PLC-side RUN-time write permission before running a write example where the PLC exposes that setting.
  4. Confirm plc_profile matches your PLC family.
  5. Confirm the first read uses a simple word register such as D100.
  6. Confirm writes use only a known-safe test address, restore confirmed writes, and explicitly reconcile any outcome-unknown result.
  7. Confirm the value read back matches the test value you wrote.

If it does not work

Symptom Check
The PLC returns an end code error plc_profile must match the actual PLC hardware. A wrong profile can cause end code errors.
Connection opens but all requests fail Confirm Binary communication data code in the PLC setup guide.
Reads work but writes fail Confirm RUN-time write permission in the PLC setup guide and the selected profile write policy.
The connection uses the wrong frame Do not set frame_type manually. It is derived from plc_profile.
The first read fails on an advanced family Start with D word reads. Do not start with G, HG, LTN, or LCN.