decode_uart
Builtin index · Signal processing
result = decode_uart(d, [baud=], [samples_per_bit=], [bits=8], [parity="none"], [stop=1])
Input: d, baud, samples_per_bit, bits, parity, stop · Output: result
What it is
d: a Digital value from to_digital. Decodes asynchronous serial frames into bytes at a stated bit rate. Bit timing is stated, not recovered: give either baud= (bits per second, converted through the signal's own sample rate) or samples_per_bit= directly — one of the two is required. baud= needs a Signal, since a bits-per-second figure has nothing to refer to on a bare vector; that case is an error naming samples_per_bit= rather than a silent Fs = 1 that would decode the whole capture into nonsense. bits (optional, 5–9, default 8) data bits; parity (optional string) "none" (default), "even" or "odd"; stop (optional, 1 or 2, default 1). The line is assumed to idle high (a frame opens on a falling edge, the stop bit is high) and data is LSB-first, which the standard fixes — there is deliberately no bit_order= here, unlike decode_spi. Each frame is sampled at its bit centres measured from its own start edge, so the decoder re-syncs on every frame instead of drifting off the bit centres across a long capture.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = decode_uart(d, [baud=], [samples_per_bit=], [bits=8], [parity="none"], [stop=1]) | d —d: a Digital value from to_digital.baud Signal or vector, optionalbaud= needs a Signal, since a bits-per-second figure has nothing to refer to on a bare vector.samples_per_bit optionalBit timing is stated, not recovered: give either baud= (bits per second, converted through the signal's own sample rate) or samples_per_bit= directly — one of the two is required.bits optional (default 8)bits (optional, 5–9, default 8) data bits.parity string, optional (default "none")parity (optional string) "none" (default), "even" or "odd".stop optional (default 1)stop (optional, 1 or 2, default 1). | byte numbernumberhex stringe.g. "0x48"start Signalthe falling edge that opened the frame — seconds for a Signal, samples otherwisevalid —1/0error stringstring: empty, "framing", "parity" or "framing+parity" | Returns a Table, one row per frame, with columns byte (number), hex (string, e.g. "0x48"), start (the falling edge that opened the frame — seconds for a Signal, samples otherwise), valid (1/0) and error (string: empty, "framing", "parity" or "framing+parity"). A frame whose stop bit is low or whose parity disagrees is reported with its decoded bits and flagged, never silently returned as a clean byte; a frame cut off by the end of the record is not reported at all, the same rule find_pulses applies to a partial pulse. Added in v0.3.0. |
Examples
No documentation example calls this form yet. The Signal processing chapter is where one belongs.
How it works
decode_uart(d, baud=|samples_per_bit=, [bits=8], [parity="none"], [stop=1]) -- asynchronous serial. Bit timing is DERIVED, not recovered: the caller states the baud rate (or the samples-per-bit directly) and every bit of a frame is sampled at its nominal centre, measured from the falling edge that opened the frame. Re-deriving the centre from j at each frame -- rather than free-running a sample counter across the whole record -- is what keeps a long capture from walking off the bit centres when the sender's clock and baud= disagree slightly, which is the entire reason real UARTs re-sync on every start bit. The line is assumed to IDLE HIGH (start bit is a falling edge, stop bit is high), which is the convention at the logic level -- i.e. downstream of an RS-232 receiver, which has already inverted the marking-low line. Data bits are LSB-first, which the standard fixes; there is deliberately no bit_order= knob here (unlike decode_spi, where both orders are genuinely in use). A frame whose stop bit is not high is reported with error="framing" and valid=0, and one whose parity bit disagrees with the data with error="parity" -- reported, not dropped and not silently returned as a clean byte. The decoded bits are still in byte for both, because that is what a logic analyser shows and because the bits are what a caller debugging a baud-rate mismatch actually needs to see.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.