OPCUA Python客户端套接字关闭的连接

时间:2019-01-19 15:29:23

标签: python multithreading sockets ubuntu opc-ua

我在ubuntu上使用FreeOpcUa库用于python,更确切地说,是以下示例: https://github.com/FreeOpcUa/python-opcua/blob/master/examples/client-example.py

一段时间(〜5-10分钟)后,我在python窗口中收到一条消息,提示:

  

INFO:opcua.client.ua_client.Socket:套接字已关闭连接   INFO:opcua.client.ua_client.Socket:线程已结束

是什么原因造成的,我该如何解决?服务器在PLC上运行。结果显然是数据更改订阅不再起作用。

我的python代码:

#! /usr/bin/python3
import sys
sys.path.insert(0, "..")
import logging
import time

import os

try:
    from IPython import embed
except ImportError:
    import code

    def embed():
        vars = globals()
        vars.update(locals())
        shell = code.InteractiveConsole(vars)
        shell.interact()


from opcua import Client
from opcua import ua


class SubHandler(object):

    """
    Subscription Handler. To receive events from server for a subscription
    data_change and event methods are called directly from receiving thread.
    Do not do expensive, slow or network operation there. Create another 
    thread if you need to do such a thing
    """

    def datachange_notification(self, node, val, data):
        #print("Python: New data change event", node, val)
        os.system("~/brightness.sh " + str(val))

    def event_notification(self, event):
        print("Python: New event", event)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    #logger = logging.getLogger("KeepAlive")
    #logger.setLevel(logging.DEBUG)

    client = Client("opc.tcp://192.168.1.10:4840")

    try:
        client.connect()

        myvar = client.get_node("ns=4;s=|var|WAGO 750-8102 PFC100 2ETH RS.Application.PLC_PRG.bVisuDimmer")

        # subscribing to a variable node
        handler = SubHandler()
        sub = client.create_subscription(100, handler)
        handle = sub.subscribe_data_change(myvar)
        time.sleep(0.1)

        #sub.subscribe_events()

        embed()
    finally:
        client.disconnect()

0 个答案:

没有答案