A45 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A4 · Lesson 5 · Lab: instrument data, text and bits

Commands out, readings and raw bytes back

Every automated bench runs the same loop: send an instrument a line of text, get a line of text or a block of raw bytes back, and turn it into numbers you can trust. Lessons 1 to 4 gave you the pieces: functions, floats and their rounding, strings and formatting, and bits and bytes. This lab puts them together on the kinds of data a real bench produces, a meter's comma-separated readings, a 24-bit converter's raw samples, a status word, and a table of results someone else has to read.

Predict

A 24-bit converter sends the three bytes 0xFF, 0xF0, 0x00, most significant first, in two's complement. Is the reading positive or negative?

Name it

The workflow

  1. Build the command with an f-string, fixing the number of decimals the instrument expects: f"SOUR:VOLT {v:.3f}".
  2. Split the reply on its separator and convert each piece: [float(s) for s in reply.split(",")]. float reads E notation directly.
  3. Decode raw bytes with int.from_bytes(b, "big"), then fix the sign for two's complement: subtract 2n when the top bit of an n-bit value is set.
  4. Scale counts to units. A bipolar n-bit converter with reference Vref is worth Vref/2n−1 volts per count.
  5. Format the results for a person: a fixed width per column, a sensible number of significant figures, and units.

Test each step on a value whose answer you already know, a datasheet's example or a hand calculation, before trusting it on real data.

Worked example

A meter returns +1.00012E+00,+9.99870E-01,+1.00031E+00,+9.99650E-01. Find the mean and the largest deviation from it.

Show the solution
  1. Parse the reply and compute. Split at the commas, convert each piece with float, then take the mean and the largest distance from it.
reply = "+1.00012E+00,+9.99870E-01,+1.00031E+00,+9.99650E-01"
x = [float(s) for s in reply.split(",")]
m = sum(x) / len(x)
dev = max(abs(v - m) for v in x)
print(f"{m:.6f} V, worst {dev * 1e6:.0f} µV")    # 0.999988 V, worst 337 µV
  1. Read the mean. It is 0.9999875 V.
  2. Find the worst reading. The reading farthest from the mean, 0.99965 V, is 337.5 µV away: Δ=0.9999875−0.99965V=337.5μV
  3. Choose the format. Printing the deviation in microvolts with no decimals says what matters without six meaningless digits.

Answer the mean is 0.9999875 V, and the largest deviation from it is 337.5 µV.

Worked example

A 24-bit bipolar converter with a 2.048 V reference sends 0x40 0x00 0x00, then 0x80 0x00 0x00. What voltages are these?

Show the solution
  1. Decode the bytes as two's complement. Join them big-endian, subtract 224 when the top bit is set, and scale by Vref/223.
def volts(b, vref=2.048):
    raw = int.from_bytes(b, "big")
    if raw >= 1 << 23:
        raw -= 1 << 24
    return raw * vref / (1 << 23)

print(volts(bytes([0x40, 0x00, 0x00])))   # 1.024
print(volts(bytes([0x80, 0x00, 0x00])))   # -2.048
  1. The first code. 0x400000 is 222, half of 223, so it's half the reference: V=12×2.048V=1.024V
  2. The second code. 0x800000 has the top bit set; minus 224 it's −223, the most negative code, which is exactly −2.048 V.
  3. The other end of the range. The most positive code, 0x7FFFFF, is one count short of +2.048 V: a two's complement range is lopsided by one.

Answer 0x40 0x00 0x00 is 1.024 V, and 0x80 0x00 0x00 is −2.048 V.

Check

A meter answers a "read all" query with comma-separated readings in E notation. Write stats(reply) returning (mean, pk2pk), the mean and the largest minus the smallest reading. Then set s = stats(reply) for the reply the starter picks.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write volts(b, vref=2.048) for a 24-bit bipolar converter: three bytes, most significant first, two's complement, Vref/223 volts per count. Then set v for the bytes the starter picks.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A bench supply takes the commands VOLT <v> and CURR <i>, with the voltage to 3 decimals and the current to 4, then OUTP ON. Write setup(v, i) returning the three commands as one string separated by "\n". Then set cmd = setup(v, i) for v = 1.25 and i = 0.01.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A converter's control register takes the channel in bits 0 to 3, a gain code in bits 4 to 6, and a start flag in bit 15. Write control(ch, gain, start) returning the register value, and decode(reg) returning (ch, gain, start) back from it. Set reg = control(ch, gain, start) for ch = 5, gain = 0 and start = 1, and back = decode(reg).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write table(rows) that formats a list of (name, value, unit) rows as text, one row per line: the name left-aligned in 8 characters, the value right-aligned in 10 characters with 4 significant figures (.4g), a space, then the unit. Set text = table(rows) for the rows the starter picks.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A bipolar 16-bit converter covers −1.25 V to +1.25 V. How many volts is one count?

Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.

Explain why

Why should you test a decoder on the extreme codes, like 0x800000 and 0x7FFFFF, rather than on a typical reading?

Practice

Keep practicing

6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.