Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
66 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from asyncua import ua | ||
|
||
class UaFile: | ||
def __init__(self, file_node): | ||
self._file_node = file_node | ||
|
||
async def open(self, open_mode): | ||
""" open file method """ | ||
open_node = await self._file_node.get_child("Open") | ||
arg = ua.Variant(open_mode, ua.VariantType.Byte) | ||
return await self._file_node.call_method(open_node, arg) | ||
|
||
async def get_size(self): | ||
""" gets size of file """ | ||
size_node = await self._file_node.get_child("Size") | ||
return await size_node.read_value() | ||
|
||
async def close(self, handle): | ||
""" close file method """ | ||
read_node = await self._file_node.get_child("Close") | ||
arg1 = ua.Variant(handle, ua.VariantType.UInt32) | ||
return await self._file_node.call_method(read_node, arg1) | ||
|
||
async def read(self, handle, size): | ||
""" | ||
:param handle: handle from open() | ||
:param size: size of file from from get_size() | ||
""" | ||
read_node = await self._file_node.get_child("Read") | ||
arg1 = ua.Variant(handle, ua.VariantType.UInt32) | ||
arg2 = ua.Variant(size, ua.VariantType.Int32) | ||
return await self._file_node.call_method(read_node, arg1, arg2) | ||
|
||
async def read_once(self): | ||
""" open, read, close in one operation """ | ||
handle = await self.open(ua.OpenFileMode.Read.value) | ||
size = await self.get_size() | ||
contents = await self.read(handle, size) | ||
await self.close(handle) | ||
return contents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import asyncio | ||
from asyncua import ua | ||
from asyncua.client import Client | ||
from asyncua.client.ua_file import UaFile | ||
|
||
async def read_file(): | ||
""" read file example """ | ||
|
||
url = "opc.tcp://10.0.0.199:4840" | ||
async with Client(url=url) as client: | ||
file_node = client.get_node("ns=2;s=NameOfNode") | ||
ua_file_client = UaFile(file_node) | ||
|
||
# open(), read(), close() all in one operation | ||
contents = await ua_file_client.read_once() | ||
print(contents) | ||
|
||
# handling all methods by the user | ||
handle = await ua_file_client.open(ua.OpenFileMode.Read.value) | ||
size = await ua_file_client.get_size() | ||
contents = await ua_file_client.read(handle, size) | ||
await ua_file_client.close(handle) | ||
print(contents) | ||
|
||
|
||
asyncio.run(read_file()) |