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

pulling values out of PLC and storing in mysql #16

Closed tbrew01 closed 7 years ago

tbrew01 commented 8 years ago

Hi, I'm just looking to write a simple script to pull some floats out of a PLC and store in mysql at regular intervals for logging purposes / fault finding purposes. the client.read function seems to return a lot of stuff, is there anyway just to return the actual tag value?

pjkundert commented 8 years ago

Most of the complexity is because client.read is a low-level API entry point, and must be usable in many different situations. Each different type of EtherNet/IP CIP device supports a different dialect of the protocol. so you must be selective. The client.read API has to support this.

Is it a ControlLogix, CompactLogix (supports Read Tag [Fragmented], and routable requests)? Then use the standard cpppo.server.enip.client APIs. Also, you can use the Multiple Service Packet request to aggregate multiple requests into a single request.

Is it a MicroLogix? Then, no Read Tag, no routable requests. Use the cpppo.server.enip.get_attribute APIs. No support for data types (eg. REAL); you have to compose the raw 8-bit SINT data into the desired data type. Or, use the cpppo.server.enip.get_attribute proxy... classes to do it for you.

Others support only Read Tag Fragmented, others only Read Tag. You just have to try them out, and see. Others just have a broken EtherNet/IP CIP implementation (eg. PowerFlex "List Identity").

So, it's not a simple answer. This is why the API is somewhat... flexible. And, therefore, somewhat confusing.

There are several higher-level abstractions that you should use, if simplicity is a goal, and if you don't have any throughput challenges (eg. high latency).

Probably the simplest API to use is the cpppo.server.enip.get_attribute proxy APIs.

1) Try the proxy or proxy_simple class with your device, to see if it has support for "routable" requests. Try something simple, like accessing the Identity object:

from cpppo.server.enip.get_attribute import proxy, proxy_simple
via = proxy( 'hostname' ) # try proxy_simple here...
with via:
    vendor,product_name = via.read( [('@1/1/1','INT'), ('@1/1/7','SSTRING)] )

2) If that works, and you have a Tag you want to access, try:

from cpppo.server.enip.get_attribute import proxy
value, = proxy( '10.1.2.3' ).read( 'Some_Tag' )

These examples establish a connection, read the data and then close the connection. Simple, but not efficient. However, you can use the provided examples to improve things (eg. retain the connection, handle I/O failures, adjust pipelining to improve thruput, etc.), if you need to.

Cheers,