Skip to content

Conversation

@X9X0
Copy link
Owner

@X9X0 X9X0 commented Nov 28, 2025

Summary

Fixes equipment readings returning 404 Not Found errors and implements proper protocol support for BK Precision 1900B series power supplies.

Problems Fixed

1. Equipment 404 Errors

  • Equipment IDs were randomly generated using UUID, causing them to change on every reconnection
  • Resulted in 404 Not Found errors when trying to access equipment readings after reconnection

2. Invalid VISA Session Errors

  • Discovery scans would invalidate active equipment VISA sessions
  • No auto-reconnection mechanism to recover from invalid sessions
  • Resource manager would also become invalid, preventing reconnection

3. BK Precision Communication Failures

  • BK Precision 1900B series doesn't support SCPI commands
  • All commands (*IDN?, MEAS:VOLT?, etc.) timed out after 10 seconds
  • Required switching to proprietary BK Precision protocol

4. Serial Port Resource Leaks

  • Old instruments weren't properly closed before reconnection
  • Caused file descriptor corruption and NoneType errors

5. Response Buffer Contamination

  • PyVISA read_termination stopped at first \r, leaving OK\r in buffer
  • Subsequent reads got contaminated data instead of actual responses

Changes Made

Deterministic Equipment IDs

  • Generate equipment IDs using SHA256 hash of resource string
  • Same resource always produces same ID (e.g., ASRL/dev/ttyUSB0::INSTRps_56fdd3df)
  • Updated all equipment classes to use deterministic ID generation

Auto-Reconnection System

  • Added _is_instrument_valid() to detect invalid VISA sessions
  • Added _ensure_connected() to auto-reconnect on invalid sessions
  • Added _is_resource_manager_valid() to detect invalid resource managers
  • Added _refresh_resource_manager() to create new resource manager when needed
  • Integrated into all _write(), _query(), and _query_binary() operations

BK Precision Protocol Support

  • Switched from SCPI to BK Precision proprietary protocol
  • Changed line endings from \r\n to \r only
  • Implemented correct command format:
    • GMAX - Get max voltage/current (format: VVVCCC)
    • GETS - Get voltage/current settings (format: VVVCCC)
    • GETD - Get display readings (format: VVVVIIIIIM)
    • GOUT - Get output state (0=OFF, 1=ON)
    • VOLT{vvv} - Set voltage (voltage × 10)
    • CURR{ccc} - Set current (current × 10)
    • SOUT0/SOUT1 - Set output (0=ON, 1=OFF)
  • All responses end with \rOK\r which is properly parsed

Proper Resource Cleanup

  • Close old instruments before opening new ones
  • Prevents file descriptor leaks and corruption
  • Properly handles exceptions during cleanup

Response Buffer Handling

  • Read full BK responses byte-by-byte until OK\r terminator
  • No longer relies on read_termination for BK protocol
  • Eliminates buffer contamination issues

Testing

Tested with BK Precision 1902B power supply:

  • ✅ Equipment connects successfully
  • ✅ Deterministic ID generated correctly (ps_56fdd3df)
  • ✅ Readings return valid data (voltage, current, output state, CV/CC mode)
  • ✅ Auto-reconnection works after discovery scans
  • ✅ Resource manager refresh works
  • ✅ No timeouts or buffer errors

Commits

  • 2140b60 - Generate deterministic equipment IDs to prevent 404 errors
  • 27a6e9a - Add automatic VISA session recovery to prevent InvalidSession errors
  • ea3b8a5 - Refresh resource manager during auto-reconnection
  • d6a9191 - Call _refresh_resource_manager() in BK power supply connect method
  • 09da3e5 - Properly close old instrument before reconnection
  • 3bf6cf7 - Update BK Precision driver to use proprietary protocol instead of SCPI
  • 408a45c - Read full BK Precision response including OK terminator

Breaking Changes

None - all changes are backward compatible improvements to existing functionality.

The server returns PowerSupplyData/ScopeData directly, not wrapped in
a success/readings structure. Updated equipment panel to handle the
actual response format from the server.

Fixes 'Could not retrieve readings' error even when server returns
valid data with 200 OK.
Changed from /equipment/{id}/disconnect to /equipment/disconnect/{id}
to match the server API endpoint definition.

Fixes 404 error when clicking disconnect button in equipment panel.
@X9X0 X9X0 merged commit 05d78a2 into main Nov 28, 2025
25 checks passed
@X9X0 X9X0 deleted the claude/debug-bk-readings-011YNFgs3A54537srRvDcAe6 branch November 28, 2025 20:53
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.

3 participants