Exchange data with Logix based PLC's via Python

dmroeder

Lifetime Supporting Member
Join Date
Apr 2006
Location
Vancouver, WA
Posts
3,604
I posted up some Python code on Github that will allow you to exchange data with CompactLogix and ControlLogix PLC's. It should run on anything that Python runs on.

Some time ago, forum member here Burt (NevisGroup) had written some code in Python for someone to pull/display some information from a Logix based PLC. He offered to share it with the forum and I took him up on the offer. At the time it worked really well but was very specific to the task that it was being used for. Over the last year or so I've been tinkering with it when I had spare time to add functionality to it. To deal with complex tag names, arrays, all the base data types, etc.

Without Burt's code to begin with, I probably would have never even made the attempt to mess with this stuff. Archie helped me quite a bit too so thanks to him for putting up with me. Having AdvancedHMI available was a huge help when things weren't working, I could see what was going on in WireShark to debug. I'm by no means an expert in any of this, just tinkering for fun. There's probably some stuff that professional coders would cringe at so keep my amateur-ness in mind. But it has been working well for me so I thought I'd share, maybe someone will find it useful.

It's still a work in progress, I'll still be hacking away at it as time allows.

https://github.com/dmroeder/pylogix
 
Hi, thanks for it. I will have a look into it and test it. Have you seen pycomm library available on github that does the same job?

question1:
does your code let read muliple arrays?

question2:
does your code let read multiple tags whih are Arrays e.g. 30x30 eac and are user datat types 10kB each?

e.g.
tag1 = 30x30, 'TestData'
tag2 = 30x30, 'TestData,
...
tag50 = 30x30, 'TestData'

TestData is an user data type and has got size of 10kB
 
Last edited:
Hey DINT2, I realized that pycomm library existed when you posted in that Raspberry Pi thread the other day. I thought to myself: shoot, it already exists. I thought about not posting it after that, but I had a few beers and posted it anyway. It has just been a learning experience for me.

As for you questions, it will read arrays of the standard data types (SINT/INT/DINT/REAL/STRING/BOOL). Also multi dimension arrays. It will not read UDT's, at least not yet. It will read any elements of the UDT, it just will not understand the structure. That is something I have been tinkering with the last few days.

For example if you had a tag of "TestCounter" that was a COUNTER type, and you tried to read TestCounter, it wouldn't know what to do with it. If you read TestCounter.ACC, since it's a DINT, it would work.

With some amount of work, there are CIP commands that seem to allow you to decode UDT's, I just haven't figured it out yet. If/when I do, I'll have to figure out the best way to present it back to the user.
 
hi dmroeder,

thanks. i will have a think and try to hack it so will try UDTs as well. let's colaborate in github if you find anything. cheers

PS. can you post an example of how to read multiple arrays in one shot?

the reason I ask is because you open the socket and next you call read function.

you pass array tag name and array size to the function.

so,do you just call read funcion for each array separately or you can pass multiple arguments at once? how?

cheers
 
You call each on its own. There is an example on my github of how to read an array, you'd call them individually

Code:
comm = PLC()
comm.IPAddress = "192.168.1.10"
array1 = comm.Read("FirstArray", 100)
array2 = comm.Read("SecondArray", 100)

You can use the .MultiRead() to read individual tags, but not arrays. I didn't think much about if someone wanted to read 2 arrays in one read, I figured they would just do each read individually. By doing them individually they wouldn't have to worry about where the first array ended and the second array began in the returned data. I was going for simplicity (for the user and me!)

edit: I'll find you on github.
 
Nice Work!

Back about 2 months ago I was helping someone with the PyComm library because they discovered if 2 RPi were trying to communicate at the same time, it would not work. We discovered a connection serial number was hard coded to a fixed value, so when a second connection was made with the same serial number, it stopped the first one from working.

What do you use for an Python editor? I have never programmed Python, but have looked at the Visual Studio add in for Python many times.
 
Nice Work!

Back about 2 months ago I was helping someone with the PyComm library because they discovered if 2 RPi were trying to communicate at the same time, it would not work. We discovered a connection serial number was hard coded to a fixed value, so when a second connection was made with the same serial number, it stopped the first one from working.

What do you use for an Python editor? I have never programmed Python, but have looked at the Visual Studio add in for Python many times.

The serial number is generated randomly using randrange.

Most of the editing was done in Linux using Kate (KDE's text editor). In windows I typically use Notepad++.

I did install the Python add on for visual studio but have not used it.
 
As you now got my curiosity up with Python programming, on my Windows PC I opened a .py file letting it use the default program and it opened in Visual Studio Code. If you haven't seen this program yet, it is Microsoft's lightweight and cross platform code editor. I actually forgot that I installed it on my PC at one time just to see what it was about, but never used it.

Did you have much trouble perfecting the buildTagIOI? For some reason that posed quite a challenge to me especially doing things like parsing multi-dimensional arrays, UDTs, and bits within things like DINTs.
 
Did you have much trouble perfecting the buildTagIOI? For some reason that posed quite a challenge to me especially doing things like parsing multi-dimensional arrays, UDTs, and bits within things like DINTs.

Yeah for sure. I started off simple with just dealing with base tags. Then I fumbled my way through arrays by looking for a '['. Then I looked for UDT's by it containing a '.'. Of course then I had to combine the two. And there was some byte aligning that happened depending on odd/even number of characters in the tag name. There were times that I took a break from it because it was blowing my mind!
 
Nice Work!

Back about 2 months ago I was helping someone with the PyComm library because they discovered if 2 RPi were trying to communicate at the same time, it would not work. We discovered a connection serial number was hard coded to a fixed value, so when a second connection was made with the same serial number, it stopped the first one from working.

What do you use for an Python editor? I have never programmed Python, but have looked at the Visual Studio add in for Python many times.
Hello Archie just for clarification you can change any property within pycomm, including Connection ID, rack and all the rest. Also for the rest of the users in this thread, I have been working on pycomm intensely last year, but lately the work is thaking all my time. The code is on ghithub for a reason "sharing" :) so if anyone want help please give a buzz. At the moment pycomm can talk with contrologix, slc, micro800 series but also not AB PLCs., but there is more that can be improved ;)

Thanks,
Ago
 
Hello Archie just for clarification you can change any property within pycomm, including Connection ID, rack and all the rest. Also for the rest of the users in this thread, I have been working on pycomm intensely last year, but lately the work is thaking all my time. The code is on ghithub for a reason "sharing" :) so if anyone want help please give a buzz. At the moment pycomm can talk with contrologix, slc, micro800 series but also not AB PLCs., but there is more that can be improved ;)

Thanks,
Ago
I'm not sure what version he was using, but I don't remember seeing a property to change the value. We only spent about 15 minutes on it and I'm not a Python programmer, so I could've easily overlooked it.
 
Hello Archie just for clarification you can change any property within pycomm, including Connection ID, rack and all the rest. Also for the rest of the users in this thread, I have been working on pycomm intensely last year, but lately the work is thaking all my time. The code is on ghithub for a reason "sharing" :) so if anyone want help please give a buzz. At the moment pycomm can talk with contrologix, slc, micro800 series but also not AB PLCs., but there is more that can be improved ;)
I looked back to find what we changed to make multiple copies of PyComm work with the same PLC and it was in this region of code:
Code:
self.attribs = {'context': '_pycomm_', 'protocol version': 1, 'rpi': 5000,
 'port': 0xAF12, 'timeout': 10, 'backplane': 1, 'cpu slot': 0,
 'option': 0, 'cid': '\x27\x04\x19\x71', 'csn': '\x27\x04', 
 'vid': '\x09\x10', 'vsn': '\x09\x10\x19\x71', 'name': 'Base',
 'ip address': None}

I do not remember exactly, but we changed the value for either cid or csn. As I mentioned, I'm not a Python programmer, so I wasn't sure the best thing to do, so we just changed the value. Just a recommendation, but making it a random generated number would avoid the problem of 2 devices trying to connect to the same PLC.
 

Similar Topics

Hi there, Can anyone suggest the solution to communicate between AB Compactlogix L36ERM controller(Which is having remote flex IOs over ethernet)...
Replies
9
Views
3,659
Hi, i want to exchange data between 2 micrologix 1100 and devicenet. Each plc have a devicenet device, exactly 1761-net-dni. it´s possible to make...
Replies
18
Views
4,154
Hi. Is there anyone there have an idea to exchange data between a Creston control system ( http://www.crestron.com/ ) and a Rockwell Flexlogix...
Replies
1
Views
2,154
Hi all. I'm trying to set up communication between a 1756-L72 (EN2TR) and a 1756-L82 (CPU port) using produced / consumed tags. From the L72...
Replies
13
Views
368
Gents, I've a Beckhoff TwinSAFE project in TwinCAT 3 where I've 2 EL6910 Safety controllers, and need to exchange data between them. So I...
Replies
2
Views
830
Back
Top Bottom