发布时间:2025-06-24 18:24:36  作者:北方职教升学中心  阅读量:964


更改节点数值

action: 你需要了解SIEMENS plc博途plc项目创建流程及组态


一、在博途软件中创建SIEMENS 1500系列plc项目,并激活OPCUA服务器

在这里插入图片描述

2、配置用户认证
在这里插入图片描述

5、启动仿真,启用opcua服务器

启动仿真前提是博途软件已经成功安装PLCSIM Advance
在这里插入图片描述

7、密码、通过OPCUA加密及签名认证建立OPCUA通讯
2、密码直接通讯即可
运行.sh扩展名文件需要unix环境,如果在windows下运行可以选择安装git软件并用git bash软件执行文件

.sh文件代码如下:

:'Generate your own x509v3 CertificateStep 1:Change ssl.conf (subjectAltname,country,organizationName,...)ssl.conf:[req ]default_bits =2048default_md =sha256distinguished_name =subjectreq_extensions =req_extx509_extensions =req_extstring_mask =utf8onlyprompt =no[req_ext ]basicConstraints =CA:FALSEnsCertType =client,serverkeyUsage =nonRepudiation,digitalSignature,keyEncipherment,dataEncipherment,keyCertSignextendedKeyUsage=serverAuth,clientAuthnsComment ="OpenSSL Generated Certificat"subjectKeyIdentifier=hashauthorityKeyIdentifier=keyid,issuersubjectAltName =URI:urn:opcua:python:server,IP:127.0.0.1[subject ]countryName =DEstateOrProvinceName =HElocalityName =HEorganizationName =AndreasHeinecommonName =PythonOpcUaServerStep 2:openssl genrsa -out key.pem 2048Step 3:openssl req -x509 -days 365-new -out certificate.pem -key key.pem -config ssl.confthis way isproved withSiemens OPC UA Client/Server!'openssl req -x509 -newkey rsa:2048-keyout my_private_key.pem -out my_cert.pem -days 355-nodes -addext "subjectAltName = URI:urn:example.org:FreeOpcUa:python-opcua"openssl x509 -outform der -inmy_cert.pem -out my_cert.der

在这里插入图片描述

在这里插入图片描述

2、

python opcua 模块签名加密通讯教程

第一章 Python 通过opcua模块进行通讯搭建


文章目录

  • python opcua 模块签名加密通讯教程
  • 搭建流程
  • 一、配置客户端证书

在这里插入图片描述

6、搭建SIEMENS plc OPCUA服务器

1、启动仿真,启用opcua服务器
  • 7、在博途软件中创建SIEMENS 1500系列plc项目,并激活OPCUA服务器
  • 2、配置OPCUA通讯安全策略
  • 在这里插入图片描述## 4、配置客户端证书

  • 6、获取节点数值
    4、配置仿真环境
  • 8、仿真环境搭建完成验证

    可以通过UAExpert调试工具对OPCUA服务器通讯进行验证

    二、配置SIEMENS OPCUA 服务器

    在这里插入图片描述

    3、调试python代码进行客户端搭建

    • 1.pip安装opcua模块
    • 2.编写python代码
    • 3.生成客户端通讯证书及私钥
      • 1、配置仿真环境

        设置plc实例名称、搭建SIEMENS plc OPCUA服务器

        • 1、通过opcua模块代码中自带的generate_certificate.sh生成客户端证书及私钥
        • 2、配置SIEMENS OPCUA 服务器
        • 3、配置python opcua客户端
      • 3.通讯测试
    • 总结

  • 搭建流程

    通过SIEMES博途配置OPCUA服务器,并启动PLC仿真完成OPCUA服务器的搭建
    通过Python pip安装OPCUA模块,搭建OPCUA客户端进行通讯
    教程目标:
    1、调试python代码进行客户端搭建

    本文以OPCUA模块搭建OPCUA通讯基本搭建,最新的OPCUA模块建议选择opcua-asnicio;
    如果你对python异步陌生,那OPCUA通讯仍可适用

    1.pip安装opcua模块

    通过pip install opcua来为python 环境安装模块
    建议通过虚拟环境安装模块

    2.编写python代码

    代码如下(示例):

    importtimefromopcua importClient,uaimportjson# opcua client classclassOpcuaClient:def__init__(self,url:str,user_name:str=None,user_password:str=None,basic256:bool=False,sha256:bool=False,cert_path:str=None,key_path:str=None,application_uri=None)->None:self._url =url        self._client =Client(url)ifuser_name anduser_password:self._client.set_user(user_name)self._client.set_password(user_password)ifbasic256 andnotsha256:self._client.set_security_string("Basic256,SignAndEncrypt,None,None")ifsha256:self._client.set_security_string(f"Basic256Sha256,SignAndEncrypt,{cert_path},{key_path}")self._client.application_uri ="urn:example.org:FreeOpcUa:python-opcua"ifapplication_uri isNoneelseapplication_uri        # print(f'client application uri: {self._client.application_uri}')self._client.connect()@propertydefroot_node(self):returnself._client.get_root_node()defget_all_childeren(self,node)->dict:_node_dict ={}for_nc innode.get_children():if_nc:print(type(_nc),_nc)if_nc.get_children():_sub_nc =self.get_all_childeren(_nc)_sub_nc['value']=str(_nc)_node_dict[f'{_nc.get_browse_name()}']=_sub_nc                else:_node_dict[f'{_nc.get_browse_name()}']=str(_nc)return_node_dict    defjson_all_childeren(self,file_path:str)->str:_time =time.perf_counter()_node_dict =self.get_all_childeren(self.root_node)withopen(file_path,'w')asf:json.dump(_node_dict,f,indent=4)print(f'generate json file time: {time.perf_counter()-_time}')returnfile_path    defread_node(self,node):returnself._client.get_node(node).get_value()defwrite_node(self,node,value):self._client.get_node(node).set_value(value)defread_nodes(self,nodes):return{node:self.read_node(node)fornode innodes}defwrite_nodes(self,nodes):fornode,value innodes.items():self.write_node(node,value)defclose(self):self._client.disconnect()if__name__ =='__main__':url ='opc.tcp://192.168.10.1:4840'user_name ='user1'user_password ='Azxs1254'cert_path ="./opc_comm/my_cert.pem"key_path ="./opc_comm/my_private_key.pem"my_client =OpcuaClient(url,user_name,user_password,True,True,cert_path,key_path)print(my_client.root_node.get_browse_name())# print(client.get_all_childeren(client.root_node))# get all children nodes and save to json filemy_client.json_all_childeren('./opc_comm/OPC_Nodes.json')# read node valueprint(my_client.read_node('ns=3;s="my_data"."my_bool1"'))my_bool =Falsetry:whileTrue:_time =time.perf_counter()my_bool =notmy_bool            my_client.write_node('ns=3;s=\"my_data\".\"my_bool1\"',ua.DataValue(ua.Variant(my_bool,ua.VariantType.Boolean)))print(f'write value: {str(my_bool):<8},write time: {time.perf_counter()-_time:10.8f}')time.sleep(1)exceptKeyboardInterrupt:raisefinally:my_client.close()

    该处使用的url网络请求的数据。服务器url、

    3.生成客户端通讯证书及私钥

    1、子网掩码及plc系列型号,点击start;
    实例启动完成后,需要在博途中点击在线,搜索plc,并将项目下载到plc实例中,启动plc完成OPCUA服务器搭建工作

    在这里插入图片描述

    8、配置python opcua客户端

    配置用户名、仿真环境搭建完成验证

  • 二、私钥文件地址,如下图

    if__name__ =='__main__':url ='opc.tcp://192.168.10.1:4840'user_name ='user1'user_password ='Azxs1254'cert_path ="./opc_comm/my_cert.pem"key_path ="./opc_comm/my_private_key.pem"my_client =OpcuaClient(url,user_name,user_password,True,True,cert_path,key_path)

    3.通讯测试

    在这里插入图片描述


    总结

    OPCUA通讯如果在测试环境可以不需要设置安全策略及验证,但数据安全性较低
    如果设置加密及签名,则在通讯将需要准备客户端证书及私钥
    在通讯时如果报application_uri错误,检查.sh文件中的uri是否与python client初始代码中的名称一致,如果不一致需要修改client.application_uri的字符串与.sh中的保持一致

    生成的json文件树状图如下:

    在这里插入图片描述