解码未知长度的protobuf的环形缓冲区消息

时间:2019-07-16 10:39:46

标签: c serial-port stm32

我正在尝试使用串行端口传输protobuf消息。 我正在使用将传入数据写入其中的环形缓冲区。

我的问题是我正在读取环形缓冲区,字节字节,我怎么知道何时完全接收到消息。邮件长度不明。

这是我的试用版: buffer counter 是全局变量。

 while (1)
  {
    /* USER CODE END WHILE */

        while(RingBufferU8_available(&ringbuffer) > 0)
        {
            uint8_t byte = RingBufferU8_readByte(&ringbuffer);
            buffer[counter++]=byte;
        }


        if ( counter >0)
        {
        print(buffer);
        counter = 0;
        bool status;

 /* Allocate space for the decoded message. */
        SimpleMessage message = SimpleMessage_init_zero;

        /* Create a stream that reads from the buffer. */
        pb_istream_t stream = pb_istream_from_buffer(buffer, counter);

        /* Now we are ready to decode the message. */
        status = pb_decode(&stream, SimpleMessage_fields, &message);

        /* Check for errors... */
        if (status)
        {
                //print("Decoding failed: %s\n");
        uint8_t myString[50];
        sprintf(myString, " lucky number was %d\n", message.lucky_number);

        /* Print the data contained in the message. */
        print(myString);
        }

        }

0 个答案:

没有答案