python实现Ethernet/IP协议的客户端(二)

Ethernet/IP是一种工业自动化领域中常用的网络通信协议,它是基于标准以太网技术的应用层协议。作为工业领域的通信协议之一,Ethernet/IP 提供了一种在工业自动化设备之间实现通信和数据交换的标准化方法。python要实现Ethernet/IP的客户端,可以采用pycomm3模块,但不一定是pycomm3,其中原委咋们慢慢聊,本文为第二篇。

一、模块选型

1、查找模块的方式

(1)可以去python官方网站查询

在查询时注意输入Ethernet/IP时,将/去掉,这跟官网的查询方式有关。

PyPI · The Python Package Index

(2)EthernetIP

GitHub: Let's build from here · GitHub

(3)搜索引擎

可以通过百度、谷歌等查找模块。

2、备选模块

(1)pycomm3

简单看一下模块的介绍,还是跟PLC相关的,因此这个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相关的,不过实现的是与Allen-Bradley通信,而且一些实现并不完整。

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相关,大家可以跟据上述提供的查找方式进行查询,我们先以这两个相对靠谱模块实验一把。

二、实验方式

1、如何实验

(1)先抓一个正常的交互包,作为基准

(2)使用模块安装官方文档进行请求,查看包的情况

(3)根据包的情况与官方文档调整请求参数

(4)一步步实验直到通讯正常

2、已知EDS文件的情报

(1)包含了连接与请求的相关信息

(2)不确定这些信息该在模块中如何使用

三、开始实验

1、使用pycomm3的哪个类?

通过上一篇文章,我们知道pycomm3的三个类情况,而我们的实验对象不是标准的PLC,因此先实验 CIPDriver

复制代码
from pycomm3 import CIPDriver

print(CIPDriver.discover())

如下图,这个模块是可以查询 EIP的相关设备的,有点靠谱。

而通过以下方法可以获取指定IP的EIP设备信息:

复制代码
CIPDriver.list_identity('192.168.1.189')

现在我们连接一下EIP设备,看看表现:

复制代码
from pycomm3 import CIPDriver

CIPDriver("192.168.1.189").open()

如下图,没有报错说明是可连的。

但当进一步想要获取设备的模块信息时,却无法进行:

复制代码
from pycomm3 import CIPDriver

driver=CIPDriver("192.168.1.189")
driver.open()
driver.get_module_info(0)  # Slot 0: PLC

很显然,报错表明无法获取EIP设备的模块信息,猜测其原因可能是EIP设备没有相关获取信息的服务提供给出来。这个模块看起来可以获取PLC的其他插槽的板卡信息。

我们查看数据包的情况,发现我们的请求时不连接发送,可能这种请求方式不被服务器处理:

经过上述实验,使用CIPDriver类还是比较靠谱的,而其他两个类是针对指定型号的PLC,基于CIPDriver类进行的封装,因此不需要再去尝试其他类。

2、后续实验

经过上述操作,我们简单了解了实验过程,并且确定了使用哪个模块哪个类去做实验,而后续实验将基于这个类慢慢展开。

相关推荐
专注API从业者24 分钟前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python
java1234_小锋31 分钟前
[免费]基于Python的协同过滤电影推荐系统(Django+Vue+sqlite+爬虫)【论文+源码+SQL脚本】
python·django·电影推荐系统·协同过滤
看海天一色听风起雨落1 小时前
Python学习之装饰器
开发语言·python·学习
XiaoMu_0012 小时前
基于Python+Streamlit的旅游数据分析与预测系统:从数据可视化到机器学习预测的完整实现
python·信息可视化·旅游
THMAIL2 小时前
深度学习从入门到精通 - 生成对抗网络(GAN)实战:创造逼真图像的魔法艺术
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·cnn
我没想到原来他们都是一堆坏人3 小时前
(未完待续...)如何编写一个用于构建python web项目镜像的dockerfile文件
java·前端·python
总有刁民想爱朕ha4 小时前
车牌模拟生成器:Python3.8+Opencv代码实现与商业应用前景(C#、python 开发包SDK)
开发语言·python·数据挖掘
人衣aoa4 小时前
Python编程基础(八) | 类
开发语言·python
大模型真好玩5 小时前
深入浅出LangGraph AI Agent智能体开发教程(四)—LangGraph全生态开发工具使用与智能体部署
人工智能·python·mcp
百锦再5 小时前
脚本语言的大浪淘沙或百花争艳
java·开发语言·人工智能·python·django·virtualenv·pygame