-
Notifications
You must be signed in to change notification settings - Fork 117
String and string array fixes #513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
0ab3897
0ad92eb
f5258a5
4fe5cae
3b5f48e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1452,7 +1452,7 @@ def test_read_wstring(self): | |||||||||||
| var = PLCVariable( | ||||||||||||
| "wstr", | ||||||||||||
| expected1.encode("utf-16-le") + b"\x00\x00", | ||||||||||||
| constants.ADST_WSTRING, f"WSTRING({len(expected1)})" | ||||||||||||
| constants.ADST_WSTRING, "WSTRING(80)" | ||||||||||||
|
Comment on lines
1454
to
+1455
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what's causing the CI failure. The fixture now declares The Padding the value to the declared byte length fixes the immediate failure and makes the fixture match real PLC behaviour:
Suggested change
Generated by Claude Code |
||||||||||||
| ) | ||||||||||||
| self.handler.add_variable(var) | ||||||||||||
|
|
||||||||||||
|
|
@@ -1490,9 +1490,9 @@ def test_read_write_list_wstr_array(self): | |||||||||||
|
|
||||||||||||
| # Add to test plc | ||||||||||||
| self.handler.add_variable(PLCVariable( | ||||||||||||
| name = "wstr_test_array", | ||||||||||||
| value = bytes(w_string_bytes), | ||||||||||||
| ads_type = constants.ADST_WSTRING, | ||||||||||||
| name = "wstr_test_array", | ||||||||||||
| value = bytes(w_string_bytes), | ||||||||||||
| ads_type = constants.ADST_WSTRING, | ||||||||||||
| symbol_type = f"WSTRING({w_string_char_size})")) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
@@ -1540,9 +1540,9 @@ def test_read_write_list_str_array(self): | |||||||||||
|
|
||||||||||||
| # Add to test plc | ||||||||||||
| self.handler.add_variable(PLCVariable( | ||||||||||||
| name = "str_test_array", | ||||||||||||
| value = bytes(string_bytes), | ||||||||||||
| ads_type = constants.ADST_STRING, | ||||||||||||
| name = "str_test_array", | ||||||||||||
| value = bytes(string_bytes), | ||||||||||||
| ads_type = constants.ADST_STRING, | ||||||||||||
| symbol_type = f"STRING({string_char_size})")) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
@@ -1591,9 +1591,9 @@ def test_read_write_list_int_array(self): | |||||||||||
|
|
||||||||||||
| # Add to test plc | ||||||||||||
| self.handler.add_variable(PLCVariable( | ||||||||||||
| name = "int_test_array", | ||||||||||||
| value = bytes(int_array_bytes), | ||||||||||||
| ads_type = constants.ADST_INT16, | ||||||||||||
| name = "int_test_array", | ||||||||||||
| value = bytes(int_array_bytes), | ||||||||||||
| ads_type = constants.ADST_INT16, | ||||||||||||
| symbol_type = f"INT")) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
@@ -1643,17 +1643,17 @@ def test_read_write_list_real_array(self): | |||||||||||
|
|
||||||||||||
| # Add to test plc | ||||||||||||
| self.handler.add_variable(PLCVariable( | ||||||||||||
| name = "real_test_array", | ||||||||||||
| value = bytes(real_array_bytes), | ||||||||||||
| ads_type = constants.ADST_REAL32, | ||||||||||||
| name = "real_test_array", | ||||||||||||
| value = bytes(real_array_bytes), | ||||||||||||
| ads_type = constants.ADST_REAL32, | ||||||||||||
| symbol_type = f"REAL")) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| # Read variable | ||||||||||||
| with self.plc: | ||||||||||||
| read_values = self.plc.read_list_by_name(["real_test_array"]) | ||||||||||||
|
|
||||||||||||
| # Verify result to 1dp | ||||||||||||
| # Verify result to 1dp | ||||||||||||
| for i, value in enumerate(read_values["real_test_array"]): | ||||||||||||
| self.assertEqual(expected_real_array[i], round(value, 1)) | ||||||||||||
|
|
||||||||||||
|
|
@@ -1669,7 +1669,7 @@ def test_read_write_list_real_array(self): | |||||||||||
| with self.plc: | ||||||||||||
| read_values = self.plc.read_list_by_name(["real_test_array"]) | ||||||||||||
|
|
||||||||||||
| # Verify result to 1dp | ||||||||||||
| # Verify result to 1dp | ||||||||||||
| for i, value in enumerate(read_values["real_test_array"]): | ||||||||||||
| self.assertEqual(expected_real_array[i], round(value, 1)) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small platform note:
c_wcharis 2 bytes on Windows but 4 bytes on Linux/macOS. The good news is thatbytes(ctypes_array)always returns raw memory regardless of ctypes type interpretation, sobytes(data).decode("utf-16-le").rstrip("\x00")inget_value_from_ctype_dataworks correctly on both platforms.One side-effect though is that the read buffer allocated in
adsSyncReadReqEx2becomesc_wchar * STRING_BUFFER = 4 * 1024 = 4096 byteson Linux, where the old code usedc_uint8 * STRING_BUFFER = 1024 bytes. Requesting more bytes than the symbol holds should be harmless in practice (the ADS library returns what's available and the length check is skipped for strings), but it's worth a quick sanity check if you have a live PLC to test against.Generated by Claude Code