title: state machine for serial protocol
body: <p>i am writing a state machine in c for a serial protocol. My packet format looks like this.</p>
  
  <p>delim0  interface  command_H  command_L  length_H  length_l  payload  checksum  delim1</p>
  
  <p>edit:my question is how to write a c code for computing the payload and checksum using this state machine. </p>
  
  <p>i am new to this topic and if i have made any simple mistakes please excuse me.</p>
  
  <pre><code>switch (dev_int_state) {
            case UART_INT_STATE_IDLE:
                  if (c == '&lt;') {         // Delim0 = 0x3C
  
                  dev_int_state = UART_INT_STATE_INTERFACE;
    }
    break;
            case UART_INT_STATE_INTERFACE:
                 if (c == 0x00) {         //Generic Commands = 0x00
  
                 dev_int_state = UART_INT_STATE_REQ_INTERFACE_SUPPORTED;
    }
    break;
             case UART_INT_STATE_REQ_INTERFACE_SUPPORTED:
                  if (c == 0x00) {        // REQ_INTERFACE_SUPPORTED = 0x00
  
                 dev_int_state = UART_INT_STATE_GET_PROTOCOLHH;
    }
    break;
             case UART_INT_STATE_GET_PROTOCOLHH:
                  if (c == 0x00) {          // GET_PROTOCOL = 0x00
  
                 dev_int_state = UART_INT_STATE_GET_PROTOCOLHL;
    }
   break;
             case UART_INT_STATE_GET_PROTOCOLHL:
                  if (c == 0x0B) {           // GET_PROTOCOL_RESPONSE = 0x000B
  
                 dev_int_state = UART_INT_STATE_LENGTHH;
    }
   break;
            case UART_INT_STATE_LENGTHH:
                 if (c ==0x04) {
  
            dev_int_state = UART_INT_STATE_LENGTHL;          // LENGHT = 4 bytes
    }
   break;
            case UART_INT_STATE_LENGTHL:
            if (c == 0x00) {
  
  
  
            dev_int_state = UART_INT_STATE_PAYLOAD;
    }
   break;
            case UART_INT_STATE_PAYLOAD:
  
  
                         {        //--- i want to store the payload in this state
  
                                        }
  
            case UART_INT_STATE_CHECKSUM:
  
  {
                size = 0;
                for (unsigned i = 0; i&lt; headerLength-1; i++) { /* i have computed checksum but not sure about it */
                    size += headerBuffer[i];
                }
  
                dev_int_state = UART_INT_STATE_DELIM1;
  }
   break;
  
                 dev_int_state = UART_INT_STATE_DELIM1;
   }
   break;
           case UART_INT_STATE_DELIM1:
                if (c == '&gt;') {
  
  
  
  
  
        }
         break;
    default:
         break;
  }
  </code></pre>