title: c++ serial communication linux 9data bits
body: <p>a bit exotic question :D</p>
  
  <p>I'm programming c++ in ubuntu 10 and i need to code a mdb(multi drop bus) protocol which uses 9 data bits in serial communication (YES 9 data bits :D)</p>
  
  <p>Some drivers do support 9 data bits on some uart chips, but mostly they do not.</p>
  
  <p>To explain briefly:
  mdb uses 8 data bits for data and 9th data bit for mode set.
  So when master sends first BYTE it sets mode=9thbit to 1 which means that all devices on the bus are interrupted and are looking for this first byte that holds the address of a device.
  If the listening device (one of many) finds its address in this first byte it knows that the following bytes will be data bytes for it. data bytes have bit 9 = mode bit set to 0</p>
  
  <p>example in bits: 000001011 000000010 000000100  000000110   (1stbyte address and 3 data bytes)</p>
  
  <p>The return situation from slave -> master mode bit is used for end of transmission.
  So the master reads from serial so long until it finds a 9bit packet that has 9th bit = 1 usualy the last 9bit sequence is a chk byte + mode = 1</p>
  
  <p>So finally my question:</p>
  
  <p>I know how to user CMPAR flag in termios to use parity bit for mode bit eg. setting it to either MARK(1) or SPACE(0)</p>
  
  <p>example FOR ALL that don't know how:</p>
  
  <p>First check if this is defined if not probably no support in termios:</p>
  
  <pre><code>    # define CMSPAR   010000000000      /* mark or space (stick) parity */
  </code></pre>
  
  <p>And the code for sending with mark or space parity eg. simulating 9th data bit</p>
  
  <pre><code>   struct termios tio;
      bzero(&amp;tio, sizeof(tio));
      tcgetattr(portFileDescriptor, &amp;tio);
  
      if(useMarkParity)
      {
              // Send with mark parity
              tio.c_cflag |= PARENB | CMSPAR | PARODD;
              tcsetattr(portFileDescriptor, TCSADRAIN, &amp;tio);
  
      }
      else
      {
              // Send  with space parity
              tio.c_cflag |= PARENB | CMSPAR;
              tio.c_cflag &amp;= ~PARODD;
              tcsetattr(portFileDescriptor, TCSADRAIN, &amp;tio);
      }
      write(portFileDescriptor,DATA, DATALEN);
  </code></pre>
  
  <p>Now what i don't know HOW to set the parity checking on receive, i have tried almost all combinations and i cannot get that error parity byte sequence.</p>
  
  <p>Can anyone help me how to set parity checking on receive that it does not ignore parity and does not strip bytes but it adds DEL before "bad" received byte:</p>
  
  <p>As it says in the POSIX Serial help</p>
  
  <blockquote>
    <p>INPCK and PARMRK If IGNPAR is enabled, a NUL character (000 octal) is
    sent to your program before every character with a parity error.
    Otherwise, <strong>a DEL (177 octal) and NUL character is sent along with
    the bad character.</strong></p>
  </blockquote>
  
  <p>So how to correctly set PARMRK AND INPCK that it will detect mode bit = 1 as parity bit error and insert DEL 177 octal in the return stream.</p>
  
  <p>Thank you :D</p>