Ethernet/IP是一種工業(yè)自動(dòng)化領(lǐng)域中常用的網(wǎng)絡(luò)通信協(xié)議,它是基于標(biāo)準(zhǔn)以太網(wǎng)技術(shù)的應(yīng)用層協(xié)議。作為工業(yè)領(lǐng)域的通信協(xié)議之一,Ethernet/IP 提供了一種在工業(yè)自動(dòng)化設(shè)備之間實(shí)現(xiàn)通信和數(shù)據(jù)交換的標(biāo)準(zhǔn)化方法。python要實(shí)現(xiàn)Ethernet/IP的客戶端,可以采用pycomm3模塊,但不一定是pycomm3,其中原委咋們慢慢聊,本文為第二篇。
一、模塊選型
1、查找模塊的方式
(1)可以去python官方網(wǎng)站查詢
在查詢時(shí)注意輸入Ethernet/IP時(shí),將/去掉,這跟官網(wǎng)的查詢方式有關(guān)。
PyPI · The Python Package Index
(2)EthernetIP
GitHub: Let’s build from here · GitHub
(3)搜索引擎
可以通過(guò)百度、谷歌等查找模塊。
2、備選模塊
(1)pycomm3
簡(jiǎn)單看一下模塊的介紹,還是跟PLC相關(guān)的,因此這個(gè)EIP比較靠譜。
pycomm3?started as a Python 3 fork of?pycomm, which is a Python 2 library for communicating with Allen-Bradley PLCs using Ethernet/IP. The initial Python 3 port was done in this?fork?and was used as the base for?pycomm3. Since then, the library has been almost entirely rewritten and the API is no longer compatible with?pycomm. Without the hard work done by the original?pycomm?developers,?pycomm3?would not exist. This library seeks to expand upon their great work.
(2)cpppo
也是跟PLC相關(guān)的,不過(guò)實(shí)現(xiàn)的是與Allen-Bradley通信,而且一些實(shí)現(xiàn)并不完整。
A subset of the EtherNet/IP client and server protocol is implemented, and a simulation of a subset of the Tag communications capability of a Allen-Bradley ControlLogix 5561 Controller is provided. It is capable of simulating ControlLogix Tag access, via the Read/Write Tag [Fragmented] services.
Only EtherNet/IP “Unconnected” type connections are supported. These are (somewhat anomalously) a persistent TCP/IP connection from a client to a single EtherNet/IP device (such as a *Logix Controller), which allow the client to issue a sequence of CIP service requests (commands) to be sent to arbitrary CIP objects resident on the target device. Cpppo does not implement “Connected” requests (eg. those typically used between *Logix PLCs, in an industrial LAN environment).
A Tag is simply a shortcut to a specific EtherNet/IP CIP Object Instance and Attribute. Instead of the Client needing to know the specific Instance and Attribute numbers, the more easily remembered and meaningful Tag may be supplied in the request path.
(3)其他
還有一些模塊跟EtherNet/IP相關(guān),大家可以跟據(jù)上述提供的查找方式進(jìn)行查詢,我們先以這兩個(gè)相對(duì)靠譜模塊實(shí)驗(yàn)一把。
二、實(shí)驗(yàn)方式
1、如何實(shí)驗(yàn)
(1)先抓一個(gè)正常的交互包,作為基準(zhǔn)
(2)使用模塊安裝官方文檔進(jìn)行請(qǐng)求,查看包的情況
(3)根據(jù)包的情況與官方文檔調(diào)整請(qǐng)求參數(shù)
(4)一步步實(shí)驗(yàn)直到通訊正常
2、已知EDS文件的情報(bào)
(1)包含了連接與請(qǐng)求的相關(guān)信息
(2)不確定這些信息該在模塊中如何使用
三、開始實(shí)驗(yàn)
1、使用pycomm3的哪個(gè)類?
通過(guò)上一篇文章,我們知道pycomm3的三個(gè)類情況,而我們的實(shí)驗(yàn)對(duì)象不是標(biāo)準(zhǔn)的PLC,因此先實(shí)驗(yàn) CIPDriver 。
from pycomm3 import CIPDriver
print(CIPDriver.discover())
如下圖,這個(gè)模塊是可以查詢 EIP的相關(guān)設(shè)備的,有點(diǎn)靠譜。
而通過(guò)以下方法可以獲取指定IP的EIP設(shè)備信息:
CIPDriver.list_identity('192.168.1.189')
現(xiàn)在我們連接一下EIP設(shè)備,看看表現(xiàn):
from pycomm3 import CIPDriver
CIPDriver("192.168.1.189").open()
如下圖,沒有報(bào)錯(cuò)說(shuō)明是可連的。
但當(dāng)進(jìn)一步想要獲取設(shè)備的模塊信息時(shí),卻無(wú)法進(jìn)行:
from pycomm3 import CIPDriver
driver=CIPDriver("192.168.1.189")
driver.open()
driver.get_module_info(0) # Slot 0: PLC
很顯然,報(bào)錯(cuò)表明無(wú)法獲取EIP設(shè)備的模塊信息,猜測(cè)其原因可能是EIP設(shè)備沒有相關(guān)獲取信息的服務(wù)提供給出來(lái)。這個(gè)模塊看起來(lái)可以獲取PLC的其他插槽的板卡信息。
我們查看數(shù)據(jù)包的情況,發(fā)現(xiàn)我們的請(qǐng)求時(shí)不連接發(fā)送,可能這種請(qǐng)求方式不被服務(wù)器處理:
經(jīng)過(guò)上述實(shí)驗(yàn),使用CIPDriver類還是比較靠譜的,而其他兩個(gè)類是針對(duì)指定型號(hào)的PLC,基于CIPDriver類進(jìn)行的封裝,因此不需要再去嘗試其他類。
2、后續(xù)實(shí)驗(yàn)
經(jīng)過(guò)上述操作,我們簡(jiǎn)單了解了實(shí)驗(yàn)過(guò)程,并且確定了使用哪個(gè)模塊哪個(gè)類去做實(shí)驗(yàn),而后續(xù)實(shí)驗(yàn)將基于這個(gè)類慢慢展開。