Fix OPC-UA type mappings#95
Merged
Merged
Conversation
SINT variables were not mapped in map_plc_to_opcua_type(), causing BadInternalError when OPC-UA clients tried to read them. The asyncua library failed to serialize responses because unmapped types defaulted to ua.VariantType.Variant, which couldn't handle raw integer values. Added type mappings for: - SINT (SByte), USINT (Byte) - UINT (UInt16), WORD (UInt16) - UDINT (UInt32), DWORD (UInt32) - ULINT (UInt64), LWORD (UInt64) - LREAL (Double) Also added corresponding value conversion handling in both convert_value_for_opcua() and convert_value_for_plc() functions to support both read and write operations. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Remove redundant mixed-case strings from datatype checks since .upper() is already called (e.g., "Sint" is unreachable after "sint".upper() == "SINT") - Replace bare except clauses with specific struct.error catches to avoid catching unintended exceptions like KeyboardInterrupt https://claude.ai/code/session_01FULyWikeb5YVJX8NVX5hax
refactor: Clean up type checking in OPC-UA plugin
fix: Add missing IEC 61131-3 type mappings to OPC-UA plugin
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request expands and improves support for PLC (Programmable Logic Controller) data types in the OPC-UA plugin, ensuring more accurate and robust type conversions between PLC and OPC-UA representations. It introduces comprehensive handling for additional PLC types (such as SINT, USINT, UINT, WORD, UDINT, DWORD, ULINT, LWORD, and LREAL), updates the conversion logic, and adds extensive tests to verify correct mappings and conversions.
Type mapping and conversion improvements:
map_plc_to_opcua_typefunction to support a wider range of PLC data types, including all signed/unsigned integer types (SINT, USINT, UINT, WORD, UDINT, DWORD, ULINT, LWORD), and floating point type LREAL, mapping them to the appropriate OPC-UAVariantType.convert_value_for_opcuaandconvert_value_for_plcto handle the new types, including proper clamping, bit-width conversions, and floating-point conversions for both 32-bit and 64-bit types. [1] [2] [3] [4]Test coverage enhancements:
test_type_conversions.py, ensuring that each PLC type is correctly mapped to its OPC-UA equivalent. [1] [2] [3]Code robustness:
struct.error) and ensuring fallback to correct types. [1] [2]datatype.upper() == "BOOL") for clarity and correctness. [1] [2]These changes provide more comprehensive and reliable PLC/OPC-UA interoperability, especially for projects using a wide range of industrial data types.