python opc

本文介绍如何使用Python通过OpenOPC库实现OPC-UA及OPC-DA的数据采集,并提供了详细的代码示例。同时,推荐了几款常用的OPC Client及Server软件,帮助读者更好地进行OPC通讯测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工业数据采集:Python从OPC读、写设备实时数据so easy - 知乎 (zhihu.com)
 

Matrikon OPC使用教程连载

python 2.7 读写 opc数据

Python2.7 opc-DA 数据采集-OpenOPC使用说明

OpenOPC Gateway Service

The Gateway Service is an optional Windows service which handles all the Win32 COM/DCOM calls used by the OpenOPC library module. This offers the following potential advantages:

  • Avoidance of DCOM security headaches and firewall issues when running the OPC client and OPC server on different nodes.
  • The ability to write OPC client applications that can run on non-Windows platforms such as Linux, Unix, and Mac OS X.

    Installing the service

    C:\OpenOPC\bin> OpenOPCService.exe -install
    Installing service OpenOpcService
    Service installed
    

    Starting the service

    C:\OpenOPC\bin> net start zzzOpenOpcService
    

    Stopping the service

    C:\OpenOPC\bin> net stop zzzOpenOpcService

FreeOpcUa:开源C ++和Python OPC-UA服务器以及客户端库和工具

PYTHON工业互联网监控项目实战4—PYTHON OPCUA

OPC UA设备接入实践

莓派安装freeopcua server

代码:

openopc_client.py(phpstudy_pro, KEPServerEX6, MatrikonOPC Explorer)

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# openopc 官方文档地址
#http://openopc.sourceforge.net/api.html
# 导入包
import OpenOPC

# 生成OpenOPC实例(Open mode)
# In Open mode a connection is made to the OpenOPC Gateway  Service running on the specified node. This mode is available to both Windows and non-Windows clients.
opc = OpenOPC.open_client('localhost')
# 显示可连接的opc服务器
print opc.servers()
# If the OPC server is running on a different node, you can include the optional host parameter...
opc.connect('Kepware.KEPServerEX.V6', 'localhost')


taglist=['mysqltest.test.test_id', 'mysqltest.test.test_name']
# # 读取一系列数据
opc_datas = opc.read(taglist)
print opc_datas
opc.close()

D:\Python27\Scripts\examples\server-minimal.py

import sys
sys.path.insert(0, "..")
import time
import random


from opcua import ua, Server


if __name__ == "__main__":

    # setup our server
    server = Server()
    server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")

    # setup our own namespace, not really necessary but should as spec
    uri = "http://examples.freeopcua.github.io"
    idx = server.register_namespace(uri)

    # get Objects node, this is where we should put our nodes
    objects = server.get_objects_node()

    myobj = objects.add_object(idx, "Tank4C9")
    status = myobj.add_variable(idx, "Status", 0)
    overheadFlow = myobj.add_variable(idx, "OverheadFlow", 0)
    buttomsFlow = myobj.add_variable(idx, "ButtomsFlow", 0)
    power = myobj.add_variable(idx, "Power", 0)

    #myVar.set_writable()    # Set MyVariable to be writable by clients


    # starting!
    server.start()
    
    try:
        count = 0
        while True:
            time.sleep(5)
            count = count + 1
            print(count%4)
            if count%4>0:
                status.set_value(1)
            else:
                status.set_value(0)

            a= random.randint(100,500)
            print("OverheadFlow:"+str(a))
            overheadFlow.set_value(a)
            buttomsFlow.set_value(random.randint(50,500))
            power.set_value(random.randint(1000,5000))
    finally:
        #close connection, remove subcsriptions, etc
        server.stop()

PYTHON3 + OPENOPC读取OPC服务器方法_工业自动化工作者-优快云博客_python 读取opc

用OPC采集mysql的配置

1.先安装mysql

2.在安装ODBC驱动 mysql-connector-odbc**

3.在控制面板或window搜索框 找到ODBC数据源管理器

    选择"系统DSN" -> 添加 ->mysql odbc *** driver->配置mysql连接信息 [配置数据源名称 TESTER]

4.打开KeepServerEX

   1、新建 一个channel 

       Input Name-> [driver] Advanced Simulator ->next-> [DataSource] TESTER [User Name] root [password] ****** (mysql 用户密码)

   2. 新建一个Decvie

      Input Name -> [decvice mode] Mysql -> 选择表名称->Next..Next->Ok

   这样就联通了

DDE OPC服务器 (OPC Server) (matrikonopc.cn)

3、读取opc服务器数据

taglist=['Channel_4.Device_6.Word_1','Channel_4.Device_6.Word_2',
         'Channel_2.Device_3.Tag_1','Channel_2.Device_3.Tag_2',
         'Channel_2.Device_3.Tag_3','Channel_4.Device_5.Tag_1',
         'Channel_3.Device_4.Word_1','Channel_3.Device_4.Word_2',]
# 读取一系列数据
opc_datas = opc.read(taglist)
datas = [i[1] for i in opc_data]
# 读取一个点
opc_data = opc.read(taglist)
data = opc_data[1]

4、写入opc服务器

# 写入一个点
# 方式1
opc.write( ('Triangle Waves.Real8', 100.0) )
# 方式2
opc['Triangle Waves.Real8'] = 100.0

# 写入多个点
opc.write( [('Triangle Waves.Real4', 10.0), ('Random.String', 20.0)] )

OPC测试常用的OPCClient和OPCServer软件推荐

原文链接:http://sns.ca800.com/space.php?uid=45972&do=blog&id=2310

各位在进行OPC通讯时,常会遇到两种情况:

1)使用一个OPCClient在同一台计算机上连接远程计算机上的多个OPCServer时,发现某个OPCServer是通畅的,但其他的OPCServer却无论如何都不通。不知该如何定位问题了?

2)多个厂家的OPCClient连接同一计算机上的OPCServer,发现有的OPCClient通了,有的不通,有的能获取数据,有的不能获取数据。

基于以上情况,将一直以来常用的OPCClient和OPCServer推荐几款,以备大家不时之需。

不是最好,但可能是恰好!

1. OPC DA Client推荐

推荐的理由(此标准仅仅代表个人意见):

1) 单文件;携带方便,无需安装,绿色环保。

2) 遵循OPC标准,稳定。

3) 功能无需多,但适用功能有一个或多个。

推荐的第一位:OPCClient.exe

把它放在第一位并不是有多好,而是100KB的体积,单文件,也很符合OPC标准,所以是个超便携的OPCClient。

推荐的第二位:Matrikon公司的OPCClient.

很不错的一个OPCClient,标准,稳定,单文件,通讯过程的信息还比较丰富,我是比较喜欢用它在创建OPC组时定义同步方式或异步方式来验证远程计算机的OPC配置是否正确。缺点就是大了点,2M多。

推荐的第三位:Kepware公司的OPCClient

功能很强大,标准,稳定,日志信息很丰富,最推荐的功能是支持对OPCServer中点名的条件过滤,支持点表的导入导出,支持自动导入OPCServer的所有点,根据点表识别好点坏点,按照列排序,用它可以弥补很多国产组态软件不能在线遍历OPCServer点表的功能、不能过滤OPCServer点的功能、不能识别OPCServer中好点坏点的功能等。缺点就是非单文件。

其实还有其他很多的很好的OPCClient软件,欢迎各位推荐分享。

2. OPC DA Server推荐:

推荐的理由(此标准仅仅代表个人意见):

1) 依然单文件;携带方便,无需安装,绿色环保。

2) 依然遵循OPC标准,稳定。

3) 能模拟常见的大多数应用场景和数据,如各种曲线(三角函数曲线等),各种数据类型,各种故障类型,这一条大多数情况下与第一点是冲突的,所以基本上是常备几种OPCServer

推荐的第一位:Knight.OPCServer

简单,还是简单,单文件运行起来啥都有了,148KB,很适合做简单的测试。

推荐的第二位:Matrikon公司的OPCServer Simulator

简单,单文件注册后,各种数据类型,各种点类型(只读点,只写点,读写点)都有了,更好的一点是,如果想做大规模的测试,10万点,那么只需要在它的Random.下添加任意名称即可,OPCServer会自动给你建出对应的测点,还是随机数变化。

推荐的第三位:Kepware公司的OPCServer

本来不想推荐,因为这是一个商业软件,需要花费金钱购买License的,但Kepware公司的OPCServer需要花钱的不是OPCServer本身,而是采集驱动。不过Kepware公司的OPCServer提供了不花钱的仿真驱动,用来测试上正是杀人越货的好东西啊,推荐理由的第三条就是说它老兄了。 缺点就是太庞大,不简单,学习成本较高。但为了能满足OPC这类产品的各种测试,它真的是最佳选择。由于它是商业软件,我就不提供下载地址了,各位去它的官方网站可以下载,做好的几万点的测试工程需要的找我要。

### PythonOPC的相关开发及集成 #### 使用FreeOpcUa库进行Python OPC-UA应用开发 对于希望利用Python进行OPC统一架构(OPC UA)应用程序开发的开发者而言,`FreeOpcUa`提供了一种完全基于Python的方法来创建服务器和客户端程序[^2]。 安装该库非常简单,只需通过pip工具执行命令即可完成环境配置: ```bash pip install opcua ``` 此过程会自动下载并设置好所有必要的依赖项,使得开发者能够立即着手于项目构建工作之中。 #### 创建简单的OPC UA服务器实例 下面展示了一个基础版本的OPC UA服务器实现方式,在这里定义了一些变量节点供后续测试连接时读取数据之用: ```python from opcua import ua, Server if __name__ == "__main__": # 初始化server对象 server = Server() # 设置端口号和其他网络参数 url = "opc.tcp://0.0.0.0:4840/freeopcua/server/" server.set_endpoint(url) # 配置命名空间 uri = "http://examples.freeopcua.github.io" idx = server.register_namespace(uri) # 获取根节点和对象节点 objects = server.get_objects_node() # 添加自定义的对象到地址空间内 myobj = objects.add_object(idx, "MyObject") # 向上述对象下挂载几个可变属性 myvar = myobj.add_variable(idx, "MyVariable", 6.7) # 将这些新加入的内容设为可写状态以便外部修改其值 myvar.set_writable() # 开启服务监听来自其他设备的消息请求 server.start() try: while True: time.sleep(1) finally: server.stop() ``` 这段代码展示了如何快速搭建起一个简易版的服务端框架,并允许远程访问其中存储的信息资源。值得注意的是,实际生产环境中还需要考虑更多安全性和稳定性方面的要求。 #### 构建基本的OPC UA客户端逻辑 除了作为信息发布者外,有时也需要扮演消费者角色去获取第三方平台共享出来的实时监测数值或其他有用情报。此时就可以借助同样的API接口轻松达成目标了: ```python from opcua import Client client = Client("opc.tcp://localhost:4840/freeopcua/server/") try: client.connect() root = client.get_root_node() print("Root node is:", root) obj = client.get_node('ns=2;i=2') print(obj.get_browse_name().to_string()) var = client.get_node('ns=2;i=3') print(var.get_value()) # 输出 MyVariable 的当前值 finally: client.disconnect() ``` 以上脚本说明了怎样建立至指定位置处运行着的目标实体之间的通信链路,并从中提取特定路径下的具体条目详情以及它们所携带的数据负载情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值