使用python通过opc ua连接plc
使用的软件
KEPServerEX 6
kepserverex中可以创建虚拟plc
kepserver设置
操作步骤:
安全策略设置
右键KepServer主窗口中项目,选择属性,点击OPC UA,允许匿名登陆 选择 是
![](https://img-blog.csdnimg.cn/img_convert/453eee2105530b26f3c7da7df1f3fa4f.png)
右键托盘中的图标,点击OPC UA配置
(如果第一次安装 托盘中没有出现图标,则索引到KepServer快捷方式位置,点击server_admin.exe即可)
设置安全策略,这里先设置为无需安全验证
![image-20220301203813730](https://img-blog.csdnimg.cn/img_convert/4b7190e9e390d618a3e2e753d68932b2.png)
创建KepServer模拟器
Simulator 驱动程序是基于内存的驱动程序,能为演示提供静态数据和变化数据
右键连接性,新建通道
![image-20220301204126371](https://img-blog.csdnimg.cn/img_convert/2bc4070050ffc94e3a2c6b284e17c051.png)
通道类型选择为Simulator
![image-20220301204428164](https://img-blog.csdnimg.cn/img_convert/f011cd67a0307553324dbc19cd486ad6.png)
设置通道名字
![image-20220301204503869](https://img-blog.csdnimg.cn/img_convert/908154e336ceb75f0cca1d359325f1f2.png)
后面一直默认点击下一页,直到通道创建完成
添加设备
![image-20220301204616430](https://img-blog.csdnimg.cn/img_convert/1f13d924ea102c612905cd8eb89d56cc.png)
设置设备名称
![image-20220301204638633](https://img-blog.csdnimg.cn/img_convert/9b1939068e362ae1676985b568ae3f69.png)
后面一直默认点击下一页,直到设备添加完成
添加标记
![image-20220301204758149](https://img-blog.csdnimg.cn/img_convert/f51f9eeb3a3737428d4fef172640e3c1.png)
设置名称、属性
![image-20220301204940390](https://img-blog.csdnimg.cn/img_convert/3e04039f4aff878b2a4efb48a083f162.png)
这里我们选用R0001
这里地址值说明如下
![image-20220301204842363](https://img-blog.csdnimg.cn/img_convert/d49ea6608f7f962b94e5aa1b344819f3.png)
添加成功后可以看到如下:
![image-20220301205024223](https://img-blog.csdnimg.cn/img_convert/7b63d5b740d82d044f0a05ff9fbd02b3.png)
运行opc quick client
点击工具栏中 [工具] -> [启动OPC Quick Client],使用kepserver的client工具来对数据进行读写
![image-20220301205347578](https://img-blog.csdnimg.cn/img_convert/5ecda122f06fe1f9c91c09f30124e558.png)
进入opc quick client界面,可以查看当前标签的值和状态
![image-20220301205545326](https://img-blog.csdnimg.cn/img_convert/2601d8b84473933f5477692582eccf8b.png)
18、选中标签右击,可以对当前标签的值进行修改
![image-20220301205745051](https://img-blog.csdnimg.cn/img_convert/1fb38d6daa8034fdbba8cd89265df31c.png)
python代码2
from queue import PriorityQueue
import time
from opcua import Client, ua
class SubHandler(object):
def datachange_notification(self, node, val, data):
print("Python: New data change event", node, val, data)
client = Client('opc.tcp://127.0.0.1:49320/', timeout=10)
# client.set_user('will')
# client.set_password('123456') # 需要在连接前调用
# client.set_security_string(r"Basic256,SignAndEncrypt,C:\Users\42082\Desktop\certificate-example.der,C:\Users\42082\Desktop\private-key-example.pem") # 只支持Basic128Rsa15, Basic256 or Basic256Sha256三种生成证书的方法
client.connect()
objects = client.get_objects_node()
print("objects:")
print(objects)
print("\n")
root = client.get_root_node()
print("children node:")
print(root.get_children()[0].get_children())
print("\n")
namespaces = client.get_namespace_array()
print("namespaces:")
print(namespaces)
print("\n")
endpoints = client.get_endpoints()
print("endpoints")
print(endpoints)
print("\n")
snode = client.get_server_node()
print("snode:")
print(snode)
print("\n")
try:
node = client.get_node('ns=2;s=通道 1.设备 1.tag1')
print("\n")
print(node.get_value())
dv = ua.DataValue(ua.Variant(200.0, ua.VariantType.Float))
node.set_value(dv)
handler = SubHandler()
sub = client.create_subscription(500, handler)
sub.subscribe_data_change(node)
time.sleep(1000)
except Exception as e:
print(e)
finally:
client.disconnect()