pjkundert / cpppo

Communications Protocol Python Parser and Originator -- EtherNet/IP CIP
https://hardconsulting.com/products/6-cpppo-enip-api
Other
332 stars 109 forks source link

How do I write to string tags? #54

Closed lintamacar closed 6 years ago

lintamacar commented 6 years ago

I'm trying to connect to my PLC and just read/write to a string tag. I've looked at the documentation and through all of the forum posts people have made on here for clues. Right now I'm able to read/write individual characters as DINTs, but that means I'll have to do an ascii translation for every character I want to read or write.

from cpppo.server.enip import client
from time import ctime

host = "172.16.17.160"
tags = ["TRM_ElementReject_Data.Production_Order.DATA[0]*40", "TRM_ElementReject_Data.Production_Order[0]"]

with client.connector( host=host, timeout=5.0 ) as conn:

    #write
    operations = client.parse_operations(["TRM_ElementReject_Data.Production_Order[0]=(DINT)99"])
    failures,replies = conn.process(operations=operations, timeout=5.0)
    for rpy in replies:
        print rpy

    #read tags
    print ''
    for index,descr,op,reply,status,value in conn.pipeline(operations=client.parse_operations( tags ), depth=2 ):
        print "%s: %20s: %s" % ( ctime(), descr, value, )

I've tried something like operations = client.parse_operations(["TRM_ElementReject_Data.Production_Order[0]=(STRING)xyz"]), but I haven't had any success. Help, please?

lintamacar commented 6 years ago

I needed to write in the ASCII numbers like this:

operations = client.parse_operations(["TRM_ElementReject_Data.Production_Order.DATA[0-2]=(SINT)97,98,99"])

You can actually do ascii-to-number conversions and vice versa like ord('a') and chr(97) .