2 Star 0 Fork 2

byronartest/OPCUA-Client-Simple

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
opcua_client_simple.py 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
Matthias 提交于 2018-02-26 07:01 . try to replace ^M AS CR/LF
# Install on windows:
# pip install opcua
import sys
sys.path.insert(0, "..")
from opcua import Client
from opcua import ua
import logging
logging.basicConfig()
#logging.basicConfig(level=logging.DEBUG)
if __name__ == "__main__":
logging.info("Read/Write variable with OPC-UA")
client = Client("opc.tcp://localhost:4840")
#client = Client("opc.tcp://admin@192.168.2.1:4840") #connect using a user
try:
client.connect()
# Get UA nodes in root
root = client.get_root_node()
# get a specific variable node knowing its node id
#-----------------------------------------------------------------------------
# read int16 variable
var = client.get_node("ns=4;s=|var|CODESYS Control Win V3 x64.Application.GVL.uiCycleCounter")
print("Value read from int16 variable: %s" % var.get_value())
logging.info("Value read from int16 variable")
# write int16 value
newValue = var.get_value() + 1
var.set_value(newValue, ua.VariantType.Int16)
print("Value read from int16 variable (after write +1) : %s" % var.get_value())
# read string variable
var1 = client.get_node("ns=4;s=|var|CODESYS Control Win V3 x64.Application.GVL.sInfoMsg")
print("Value read from string variable: %s" % var1.get_value())
# write string value
var1.set_value("Cool, I can write the value", ua.VariantType.String)
print("Value read from string variable (after write): %s" % var1.get_value())
# Now getting a variable node using its browse path
#-----------------------------------------------------------------------------
myvar = root.get_child(["0:Objects", "2:DeviceSet", "4:CODESYS Control Win V3 x64", "4:Resources", "4:Application", "3:GlobalVars", "4:GVL", "4:uiCycleCounter"])
print("Value read from int16 variable using its browse path: %s" % myvar.get_value())
finally:
client.disconnect()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/byronartest/OPCUA-Client-Simple.git
git@gitee.com:byronartest/OPCUA-Client-Simple.git
byronartest
OPCUA-Client-Simple
OPCUA-Client-Simple
master

搜索帮助