Serial Port Auto-Baud Detection

Hard Python Interfacing Asked at Silicon Labs
Solve this problem

Infer the baud rate of an unknown serial stream by measuring the shortest pulse width in a captured bit stream, given the sampling rate of the capture.

Constraints

  • Input is a list of 0/1 samples plus the capture sample rate in Hz.
  • The shortest run of identical samples corresponds to one bit period.
  • Snap the result to the nearest standard baud rate.
  • Standard rates: 9600, 19200, 38400, 57600, 115200.
  • A constant stream with no transitions must raise ValueError.
Starter codePython
1STANDARD_BAUDS = [9600, 19200, 38400, 57600, 115200]2 3def detect_baud(samples, sample_rate):4    # TODO5    return 0