Skip to content

Commit

Permalink
Added case for literal (W)STRING to be handled by a presized buffer i…
Browse files Browse the repository at this point in the history
…nstead of a single character
  • Loading branch information
RobertoRoos committed Sep 13, 2024
1 parent 0682e6c commit 0a259d8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 1 addition & 2 deletions pyads/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
ads_type_to_ctype,
PLCSimpleDataType,
PLCDataType,
STRING_BUFFER,
)
from .filetimes import filetime_to_dt
from .pyads_ex import (
Expand Down Expand Up @@ -449,7 +448,7 @@ def get_all_symbols(self) -> List[AdsSymbol]:
symbol_size_msg = self.read(
ADSIGRP_SYM_UPLOADINFO2,
ADSIOFFS_DEVDATA_ADSSTATE,
PLCTYPE_STRING * STRING_BUFFER,
PLCTYPE_STRING,
return_ctypes=True,
)
sym_count = struct.unpack("I", symbol_size_msg[0:4])[0]
Expand Down
6 changes: 5 additions & 1 deletion pyads/pyads_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,11 @@ def adsSyncReadReqEx2(
index_offset_c = ctypes.c_ulong(index_offset)

# Strings were handled specifically before, but their sizes are contained and we
# can proceed as normal:
# can proceed as normal
if data_type == PLCTYPE_STRING or data_type == PLCTYPE_WSTRING:
# This implies a string of size 1, which is so are we instead use a large fixed
# size buffer:
data_type = data_type * STRING_BUFFER
data = data_type()

data_pointer = ctypes.pointer(data)
Expand Down
8 changes: 5 additions & 3 deletions tests/test_connection_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,11 @@ def test_read_string(self):
# Assert that the server received the correct command
self.assert_command_id(requests[0], constants.ADSCOMMAND_READ)

# We are reading only a single character, which the test-server defaults at 0
expected_result = "\x00"
self.assertEqual(expected_result, result)
# The string buffer is 1024 bytes long, this will be filled with \x0F
# and null terminated with \x00 by our test server. The \x00 will get
# chopped off during parsing to python string type
expected_result = "\x0F" * 1023
self.assertEqual(result, expected_result)

def test_write_uint(self):
value = 100
Expand Down

0 comments on commit 0a259d8

Please sign in to comment.