Skip to content

fix(tsp): check write_termination before loading large scripts#607

Open
AdarshJ173 wants to merge 1 commit into
tektronix:mainfrom
AdarshJ173:fix/tsp-load-script-write-termination-check
Open

fix(tsp): check write_termination before loading large scripts#607
AdarshJ173 wants to merge 1 commit into
tektronix:mainfrom
AdarshJ173:fix/tsp-load-script-write-termination-check

Conversation

@AdarshJ173

Copy link
Copy Markdown

Summary

Closes #500

TSP instruments enforce a 1000-character maximum per VISA write when write_termination is not configured. load_script() previously assembled the entire loadscript … endscript block into a single write string with no length check, silently truncating or erroring on any script body that pushes the total over 1000 characters.

Root Cause

# BEFORE — single write, no length guard
self.write(f"loadscript {script_name}\n{script_body}\nendscript")

The reporter's repro (print("123456…" with 967+ chars) produces a 1000+ char write. Without write_termination, the instrument receives the truncated bytes and returns an unexpected error; the caller gets no indication of why.

Fix

_TSP_WRITE_MAX_CHARS: int = 1000   # new module-level constant

Three-path logic in load_script(), replacing the single write:

Condition Behaviour
len(full_cmd) <= 1000 Single write — identical to before, zero behaviour change for small scripts
len(full_cmd) > 1000 AND write_termination falsy Raise ValueError with a clear message explaining the limit and how to fix it
len(full_cmd) > 1000 AND write_termination set 3-phase multi-write: loadscript {name} → body in ≤1000-char chunks → endscript

Multi-write protocol (phase 3 path)

# Phase 1: open context
self.write(f"loadscript {script_name}")
# Phase 2: stream body in chunks
for chunk_start in range(0, len(script_body), _TSP_WRITE_MAX_CHARS):
    self.write(script_body[chunk_start : chunk_start + _TSP_WRITE_MAX_CHARS])
# Phase 3: close context
self.write("endscript")

This matches the TSP multi-write protocol described in Keithley's TSP documentation.

Error message (path 2)

ValueError: Script 'overrun' requires 1023 characters to load, which exceeds the TSP
per-write limit of 1000 characters. TSP instruments cannot receive scripts larger than
this limit without a write_termination character configured on the VISA resource.
Set write_termination before loading large scripts, e.g.:
device.visa_resource.write_termination = '\n'

Impact

  • Small scripts (≤ 1000 chars total): no behaviour change whatsoever.
  • Large scripts without write_termination: was silent truncation / confusing instrument error → now immediate ValueError with actionable message.
  • Large scripts with write_termination: was broken → now works correctly via chunked write.

Changes

  • src/tm_devices/driver_mixins/device_control/tsp_control.py
    • New module constant _TSP_WRITE_MAX_CHARS = 1000
    • load_script() length check and three-path write logic
    • Updated docstring with Raises section and Note about the 1000-char limit

No public API changes, no new dependencies, no schema migrations.

Closes tektronix#500

TSP instruments enforce a 1000-character maximum per write when
`write_termination` is absent. `load_script()` previously issued the
entire `loadscript ... endscript` block as a single write, silently
truncating or erroring on scripts that exceed that limit.

Changes in `tsp_control.py`:
- Add `_TSP_WRITE_MAX_CHARS = 1000` module-level constant documenting
  the hardware limit.
- In `load_script()`, after assembling the full command string:
  * If `len(cmd) <= 1000` — write as before (no behaviour change for
    small scripts).
  * If `len(cmd) > 1000` AND `write_termination` is falsy (empty string
    or None) — raise `ValueError` with a clear message explaining the
    limit and how to fix it.
  * If `len(cmd) > 1000` AND `write_termination` is set — split the
    load into three phases matching the TSP multi-write protocol:
    1. `write("loadscript {name}")` — open the script context.
    2. Iterate over `script_body` in ≤1000-char chunks, writing each.
    3. `write("endscript")` — close the script context.
- Update docstring to document the 1000-character limit and
  `write_termination` requirement for large scripts.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@tek-githubbot-1010
tek-githubbot-1010 requested a review from a team July 19, 2026 08:55
# write_termination character is configured. Scripts longer than this limit
# must be sent in multiple writes, which requires write_termination to be set
# so that the instrument can distinguish successive chunks.
_TSP_WRITE_MAX_CHARS: int = 1000

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a class constant, so subclasses can overwrite it if necessary

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.

[BUG]: TSP devices don't check for a write_termination character when loading a script

3 participants