Skip to content

Fix OPC-UA type mappings#95

Merged
thiagoralves merged 4 commits into
mainfrom
development
Jan 29, 2026
Merged

Fix OPC-UA type mappings#95
thiagoralves merged 4 commits into
mainfrom
development

Conversation

@thiagoralves
Copy link
Copy Markdown
Contributor

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:

  • Expanded the map_plc_to_opcua_type function 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-UA VariantType.
  • Updated convert_value_for_opcua and convert_value_for_plc to 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:

  • Added unit tests for all new type mappings in test_type_conversions.py, ensuring that each PLC type is correctly mapped to its OPC-UA equivalent. [1] [2] [3]
  • Introduced tests for value conversion and round-trip integrity for new types, including boundary and clamping behavior, especially for SINT and other integer types. [1] [2]

Code robustness:

  • Improved error handling in float conversion sections by catching specific exceptions (struct.error) and ensuring fallback to correct types. [1] [2]
  • Standardized type checks to use exact string matches (e.g., 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.

thiagoralves and others added 4 commits January 28, 2026 19:07
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
@thiagoralves thiagoralves merged commit c677f72 into main Jan 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants