Stick with the "one data byte per UART frame" approach for frames which
carry 5 to 8 data bits.  But send two bytes per UART frames (big endian
representation) for configurations with 9 data bits.

This addresses Bug 708, passing values greater than 255 to the bytes()
Python routine broke execution of the decoder.  The proposed patch to
replace bytes() by bin() is not desirable as it generates variable width
output.  Using format "{:09b}" is not desirable either as it's rather
verbose for a binary output stream, and (rather unexpectedly) switches
from a byte stream output to ASCII characters to represent individual
bits.  Switching from one byte to two bytes (and only when required) is
the least intrusive approach, i.e. the least of all available evils.
Generating 16bit output data for input data which exceeds the 8bit space
should be most acceptable.

Signed-off-by: Gerhard Sittig <>
 decoders/uart/ | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/decoders/uart/ b/decoders/uart/
index 151cae424fef..b78c617af978 100644
--- a/decoders/uart/
+++ b/decoders/uart/
@@ -285,11 +285,18 @@ def get_data_bits(self, rxtx, signal):
         elif f == 'bin':
             self.putx(rxtx, [rxtx, [bin(b)[2:].zfill(8)]])
-        self.putbin(rxtx, [rxtx, bytes([b])])
-        self.putbin(rxtx, [2, bytes([b])])
+        formatted = self.format_bytes(b)
+        self.putbin(rxtx, [rxtx, formatted])
+        self.putbin(rxtx, [2, formatted])
         self.databits[rxtx] = []
+    def format_bytes(self, b):
+        """Format data to either one or two bytes (5-8, and 9 bits)."""
+        bits = self.options['num_data_bits']
+        data = [ b >> 8, b & 0xff, ] if bits > 8 else [ b, ]
+        return bytes(data)
     def get_parity_bit(self, rxtx, signal):
         # If no parity is used/configured, skip to the next state immediately.
         if self.options['parity_type'] == 'none':

Check out the vibrant tech community on one of the world's most 
engaging tech sites,!
sigrok-devel mailing list

Reply via email to