Solution1: Python2.7+OpenOPC
第一步:安装Python2.7
第二步:安装MatrikonOPCExplorer.EXE或者KEPServerEx V4.0,新建Tags:Channel_0_User_Defined.Random.Random1
第三步:安装OpenOPC-1.3.1.win32-py2.7.exe
第四步:安装Pypiwin32 库
第五步:编程
# -*- coding: UTF-8 -*-
import OpenOPC
import time
opc = OpenOPC.client()
# list all the opc server
opc.servers()
print(opc.servers())
opc.connect('KEPware.KEPServerEx.V4')
print(opc.info())
tags = ['Channel_0_User_Defined.Random.Random1']
value = opc.read(tags)
print(value)
完成读的操作,更详细操作见OpenOPC文件。
读结果如下:
[(‘Channel_0_User_Defined.Random.Random1’, 740, ‘Good’, ‘2020-03-26 20:24:29.38’)]
第二、三、四步源文件在我网盘内。
Solution2: Python3.8+OpenOPC
第一步:安装Python3.8
第二步:安装MatrikonOPCExplorer.EXE或者KEPServerEx V4.0,新建Tags:Channel_0_User_Defined.Random.Random1
第三步:安装Pypiwin32、OpenOPC-Python3X 库
第四步:编程
-*- coding: UTF-8 -*-
import OpenOPC
import time
import pywintypes
pywintypes.datetime = pywintypes.TimeType
opc = OpenOPC.client()
# list all the opc server
opc.servers()
print(opc.servers())
# connect opc server
opc.connect('KEPware.KEPServerEx.V4')
print(opc.info())
tags = ['Channel_0_User_Defined.Random.Random1']
value = opc.read(tags) # read Device&group&Tag
print(value)
报错:_pickle.PicklingError: Can’t pickle <class ‘pywintypes.datetime’>: attribute lookup datetime on pywintypes failed
Debug 即可。
读结果如下:[(‘Channel_0_User_Defined.Random.Random1’, -475, ‘Good’, ‘2020-03-26 20:41:42.984000+00:00’)]
对比还是Python3.8更方便。