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?
Negative. In two's complement the top bit of the whole value is the sign, and 0xFF has its top bit set. As an unsigned number the three bytes are 16 773 120; subtracting gives −4096. The reference voltage sets how many volts a count is worth, but not the sign.
Name it
The workflow
- Build the command with an f-string, fixing the number of decimals the instrument expects:
f"SOUR:VOLT {v:.3f}". - Split the reply on its separator and convert each piece:
[float(s) for s in reply.split(",")].floatreadsEnotation directly. - Decode raw bytes with
int.from_bytes(b, "big"), then fix the sign for two's complement: subtract when the top bit of an -bit value is set. - Scale counts to units. A bipolar -bit converter with reference is worth volts per count.
- 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
- 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
- Read the mean. It is 0.9999875 V.
- Find the worst reading. The reading farthest from the mean, 0.99965 V, is 337.5 µV away:
- 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
- Decode the bytes as two's complement. Join them big-endian, subtract when the top bit is set, and scale by .
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
- The first code. 0x400000 is , half of , so it's half the reference:
- The second code. 0x800000 has the top bit set; minus it's , the most negative code, which is exactly −2.048 V.
- 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, 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?
Most decoding bugs only show at the edges. A missing two's complement step gives a plausible number for small positive readings and only goes wrong once the top bit is set; an off-by-one in the scale factor is tiny in the middle and obvious at full scale. The extreme codes have answers you can work out exactly, −2.048 V and one count under +2.048 V, so they test the sign handling and the scaling where errors are largest and easiest to recognize.
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.