Skip to main content

A basic PCOM implementation in Python

Project description

PCOM

A very basic Unitronics PCOM protocol implementation for Python 3.

How to use

from pcom import commands
from pcom.plc import EthernetPlc

with EthernetPlc(address=('192.168.5.43', 1616)) as plc:
    # Read realtime clock
    c = commands.ReadRtc()
    res = plc.send(c)
    print(res)
    
    # Set realtime clock
    c = commands.SetRtc(value=datetime.now())
    plc.send(c)
    
    # Set MB 300 through 304
    values = [True, False, True, True, False]
    c = commands.SetBits(code=commands.SetBits.MEMORY, address=300, values=values)
    plc.send(c)

When entering the with block, the plc instance connects to the PLC and the connection is closed when the block is done, or when an error occurs within the block.

You can also manually call the connect() and close() methods:

from pcom import commands
from pcom.plc import EthernetPlc

plc = EthernetPlc(address=('192.168.5.43', 1616))
try:
  plc.connect()
  # Read realtime clock
  c = commands.ReadRtc()
  res = plc.send(c)
  print(res)
finally:  # Always close the connection.
  plc.close()

See the commands Python package for available commands.

Datatable commands are in the commands.datatables package. To work with a datatable, its structure has to be defined. Here's an example:

from pcom import commands
from pcom.plc import EthernetPlc


with EthernetPlc(address=('192.168.5.47', 1616)) as plc:
    table_structure = commands.datatables.DatatableStructure("My table", offset=19000, rows=2, columns=[
        commands.datatables.Int(),
        commands.datatables.Int(2),
        commands.datatables.Int(),
        commands.datatables.Int(),
        commands.datatables.Long(),
    ])
    c = commands.datatables.ReadDatatable(structure=table_structure, row_count=2)
    res = plc.send(c)
    print('My table', res)

Writing to a datatable is very much alike:

from pcom import commands
from pcom.plc import EthernetPlc


with EthernetPlc(address=('192.168.5.47', 1616)) as plc:
    table_structure = commands.datatables.DatatableStructure("My table", offset=19000, rows=2, columns=[
        commands.datatables.Int(2),
        commands.datatables.Int(),
        commands.datatables.String(5),
        commands.datatables.Int(),
        commands.datatables.Int(),
        commands.datatables.Long(),
    ])

    rows = [
        [[11, 12], [13], "hello", [14], [15], [-673542]],
        [[11, 14], [15], "bye  ", [16], [17], [655666]],
    ]
    c = commands.datatables.WriteDatatable(structure=table_structure, data=rows)
    try:
        plc.send(c)
    except datatables.WriteDatatableError as ex:
        print(ex)

Known limitations

  • General:
    • Serial communication is not implemented yet.
  • Datatables:
    • "Part of project" columns are not supported.
    • Reading a datatable structure is not supported.

Changelog

Version 0.11

  • Fix: IndexError when reading OneBitRequests (MB, SB...)

Version 0.10

  • Add hint about requested data being too large in binary command exception.
  • Fix: is_online should not be True when connect() fails.

Version 0.9.1

  • Add support for WriteIntegers and WriteFloats commands

Version 0.8

  • Start of changelog
  • BREAKING: Methods _connect() and _close() have been renamed to connect() and close(). Unless you have subclasses that overrides those methods, this has no impact.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pcom-0.11-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file pcom-0.11-py3-none-any.whl.

File metadata

  • Download URL: pcom-0.11-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.2

File hashes

Hashes for pcom-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 33950f2d24bd305a0e69425e402edafbc3ce21643661d61b2750e6906f03df95
MD5 2e725d28f7d5dfe0f9866c8aa9266b72
BLAKE2b-256 f786c0c54a7108b9626486f0ca97b016bf6d178fc41d0a4402570778bb12a63e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page