@@ -70,7 +70,7 @@ unsigned extract_nibbles_4b1s(uint8_t const *message, unsigned offset_bits, unsi
7070 return ret ;
7171}
7272
73- unsigned extract_bytes_uart (uint8_t const * message , unsigned offset_bits , unsigned num_bits , uint8_t * dst )
73+ unsigned extract_bytes_uart_8n1 (uint8_t const * message , unsigned offset_bits , unsigned num_bits , uint8_t * dst )
7474{
7575 unsigned ret = 0 ;
7676
@@ -97,7 +97,48 @@ unsigned extract_bytes_uart(uint8_t const *message, unsigned offset_bits, unsign
9797 return ret ;
9898}
9999
100- unsigned extract_bytes_uart_parity (uint8_t const * message , unsigned offset_bits , unsigned num_bits , uint8_t * dst )
100+ unsigned extract_bytes_uart_8n2 (uint8_t const * message , unsigned offset_bits , unsigned num_bits , uint8_t * dst )
101+ {
102+ unsigned ret = 0 ;
103+
104+ // skip until first start bit
105+ while (num_bits > 11 ) {
106+ int startb = message [offset_bits / 8 ] >> (7 - (offset_bits % 8 ));
107+ if ((startb & 1 ) == 0 ) {
108+ break ; // start bit found
109+ }
110+ offset_bits += 1 ;
111+ num_bits -= 1 ;
112+ }
113+ // get framed bytes
114+ while (num_bits >= 11 ) {
115+ int startb = message [offset_bits / 8 ] >> (7 - (offset_bits % 8 ));
116+ offset_bits += 1 ;
117+ int datab = message [offset_bits / 8 ];
118+ if (offset_bits % 8 ) {
119+ datab = (message [offset_bits / 8 ] << 8 ) | message [offset_bits / 8 + 1 ];
120+ datab >>= 8 - (offset_bits % 8 );
121+ }
122+ offset_bits += 8 ;
123+ int stopb1 = message [offset_bits / 8 ] >> (7 - (offset_bits % 8 ));
124+ offset_bits += 1 ;
125+ int stopb2 = message [offset_bits / 8 ] >> (7 - (offset_bits % 8 ));
126+ offset_bits += 1 ;
127+ if ((startb & 1 ) != 0 )
128+ break ; // start-bit error
129+ if ((stopb1 & 1 ) != 1 )
130+ break ; // stop-bit error
131+ if ((stopb2 & 1 ) != 1 )
132+ break ; // stop-bit error
133+ * dst ++ = reverse8 (datab & 0xff );
134+ ret += 1 ;
135+ num_bits -= 11 ;
136+ }
137+
138+ return ret ;
139+ }
140+
141+ unsigned extract_bytes_uart_8o1 (uint8_t const * message , unsigned offset_bits , unsigned num_bits , uint8_t * dst )
101142{
102143 unsigned ret = 0 ;
103144
@@ -568,18 +609,28 @@ int main(void) {
568609 // y0 xff y1 y0 xcc y1 y0 x80 y1 y0 x40 y1 y0 xc0 y1
569610 uint8_t uart123 [] = {0x07 , 0xfd , 0x99 , 0x40 , 0x48 , 0x16 , 0x04 , 0x00 };
570611
571- fprintf (stderr , "util::extract_bytes_uart ():\n" );
572- ASSERT_EQUALS (extract_bytes_uart (uart , 0 , 24 , bytes ), 2 );
612+ fprintf (stderr , "util::extract_bytes_uart_8n1 ():\n" );
613+ ASSERT_EQUALS (extract_bytes_uart_8n1 (uart , 0 , 24 , bytes ), 2 );
573614 ASSERT_EQUALS (bytes [0 ], 0xff );
574615 ASSERT_EQUALS (bytes [1 ], 0x33 );
575616
576- ASSERT_EQUALS (extract_bytes_uart (uart123 , 4 , 60 , bytes ), 5 );
617+ ASSERT_EQUALS (extract_bytes_uart_8n1 (uart123 , 4 , 60 , bytes ), 5 );
577618 ASSERT_EQUALS (bytes [0 ], 0xff );
578619 ASSERT_EQUALS (bytes [1 ], 0x33 );
579620 ASSERT_EQUALS (bytes [2 ], 0x01 );
580621 ASSERT_EQUALS (bytes [3 ], 0x02 );
581622 ASSERT_EQUALS (bytes [4 ], 0x03 );
582623
624+ // y0 xD1 y11 y0 x11 y11 y0 x4D y11 y0 xEE y11
625+ uint8_t uart8n2 [] = {0x45 , 0xe8 , 0x8d , 0x65 , 0x9d , 0xf0 };
626+
627+ fprintf (stderr , "util::extract_bytes_uart_8n2():\n" );
628+ ASSERT_EQUALS (extract_bytes_uart_8n2 (uart8n2 , 0 , 44 , bytes ), 4 );
629+ ASSERT_EQUALS (bytes [0 ], 0xd1 );
630+ ASSERT_EQUALS (bytes [1 ], 0x11 );
631+ ASSERT_EQUALS (bytes [2 ], 0x4d );
632+ ASSERT_EQUALS (bytes [3 ], 0xee );
633+
583634 fprintf (stderr , "util::ccitt_whitening():\n" );
584635 uint8_t buf1 [16 ] = {0 };
585636 uint8_t chk1 [16 ] = {0xff , 0x87 , 0xb8 , 0x59 , 0xb7 , 0xa1 , 0xcc , 0x24 , 0x57 , 0x5e , 0x4b , 0x9c , 0x0e , 0xe9 , 0xea , 0x50 };
0 commit comments