KrystianD / fxplc

Python connector for low-level Mitsubishi PLC MELSEC FX series (FX-232AW) serial protocol with builtin HTTP server.
MIT License
15 stars 2 forks source link
connector fx-232aw fx1n fx3u http http-server library melsec mitsubishi mitsubishi-plc plc python rs232 serial

FXPLC

Python connector for low-level Mitsubishi MELSEC FX series (FX-232AW) serial protocol.

Protocol specification - Link

Note it is not the same as Non-Protocol Communication (or D8120) as described in FX Series Programmable Controllers manuals.

Overview

Python library and CLI utility allow to read and write PLC registers like X0, Y0, S0, T0, M0 and D0.

Example usage

Library

import asyncio
from contextlib import closing
from fxplc.client.FXPLCClient import FXPLCClient
from fxplc.transports.TransportSerial import TransportSerial
from fxplc.transports.TransportTCP import TransportTCP

async def main():
    transport = TransportSerial("/dev/ttyUSB0")
    # or, for TCP transport (serial-over-ethernet only)
    # transport = TransportTCP("192.168.1.100", 8888)
    # await transport.connect()

    with closing(FXPLCClient(transport)) as fx:
        s0_state = await fx.read_bit("S0")
        t0_state = await fx.read_bit("T0")
        t0_value = await fx.read_int("T0")

        await fx.write_bit("S1", True)

asyncio.run(main())

CLI

fxplc -p /dev/ttyUSB0 read_bit S0
fxplc -p /dev/ttyUSB0 read_bit T0
fxplc -p /dev/ttyUSB0 read_int T0
fxplc -p tcp:192.168.1.100:8888 read_int T0

fxplc -p /dev/ttyUSB0 write_bit S1 on

fxplc -p /dev/ttyUSB0 read S0 T0
# S0 = off
# T0 = on, counter: 30

HTTP server

The project also includes HTTP webserver subproject which exposes REST API for external clients. It also serves a simple UI that allows variables viewing and modifying.

Usage

python --variables vars.yaml --path /dev/ttyUSB0 http_server/__main__.py
python --variables vars.yaml --path tcp:10.5.12.10:8887 http_server/__main__.py

Example variables file

vars.yaml

variables:
  - name: PUMP
    register: M10
  - name: MIXER_OPEN
    register: D0
  - name: MIXER_CLOSE
    register: D1
  - name: OUT_PUMP
    register: Y000
  - name: OUT_MIXER_OPEN
    register: Y000
  - name: OUT_MIXER_CLOSE
    register: Y001
  - name: FLOW_COUNTER_1
    register: D50

HTTP server documentation

.github/rest.png

HTTP server User Interface

.github/ui_example.png

Compatibility

Tested on: