python opcua服务

import logging
import asyncio
import sys
sys.path.insert(0, "..")

from asyncua import ua, Server
from asyncua.common.methods import uamethod



@uamethod
def func(parent, value):
    return value * 2


async def main():
    """
    This example show how to host the server in an environment, where the expected endpoint addresses does not match
    the address the server listens to. This, for example, can be behind a NAT or in a Docker container.

    The server address the server listens to could be in range 172.16.x.x in case of Docker (represented by 0.0.0.0),
    while the endpoint description can be a real IP:port that the Docker host
    machine has (example-endpoint.freeopcua.github.com:32000 in this example)
    """
    _logger = logging.getLogger('asyncua')
    # setup our server
    server = Server()
    await server.init()
    server.set_endpoint('opc.tcp://example-endpoint.freeopcua.github.com:32000/freeopcua/server/')

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

    # setting the network properties
    server.socket_address = ("0.0.0.0", 4840)
    server.set_match_discovery_client_ip(False)

    # populating our address space
    myvar = await server.get_objects_node().add_variable(idx, 'MyVariable', 0.0)
    # Set MyVariable to be writable by clients
    await myvar.set_writable()

    _logger.info('Starting server!')
    async with server:
        while True:
            await asyncio.sleep(1)
            new_val = await myvar.get_value() + 0.1
            _logger.info('Set value of %s to %.1f', myvar, new_val)
            await myvar.write_value(new_val)


if __name__ == '__main__':

    logging.basicConfig(level=logging.DEBUG)

    asyncio.run(main(), debug=True)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值