Search code examples
gnuradiognuradio-companion

Why is my custom block going twice into general_work() function in GNU Radio?


I am creating a custom block "Combine" that gets 20 bytes of data from the first input. The value of first input specifies the number of bytes to be read from the second input, which are read and wrote to the output file. enter image description here

Whenever I execute the flowgraph, the printing shows that the code goes twice into the general work function. It reads the correct data in the first time and the second time, it just reads bogus values and writes this incorrect data to the output sink.

I am using the following signatures for the input:

Combine_impl::Combine_impl()
      : gr::block("Combine",
              gr::io_signature::make(2, 2, sizeof(unsigned char)),
              gr::io_signature::make(1, 1, sizeof(unsigned char)))
    {}

I think my problem is with the forecast function and the usage of consume each function. I have tried doing this in forecast but it still goes twice into the general_work function and writes incorrect data to the output file.

ninput_items_required[0] = 20;
ninput_items_required[1] = 7;   //because the first input has a value of 7 and will read 7 bytes of data from the second input

Can someone please help me this to determine what exactly is going wrong over here? Also, how is the consume_each() function supposed to be utilized over here?


Solution

  • I modified the forecast function to take the exact number of items that were utilized from each input:

    void Combine_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required)
    {
         ninput_items_required[0] = 20;
         ninput_items_required[1] = 7;
    }
    

    Instead of using consume_each() which specifies the number of bytes consumed in all of the inputs, I used the consume() function which specifies this number separately for each input:

    consume(0, 20); //20 bytes of input at index 0 were consumed
    consume(1, 7);  //7 bytes of input at index 1 were consumed
    

    And instead of returning noutput_items from the general_work function, I return the following. It exactly the specifies the number of bytes that are to returned, whose value is different than noutput_items.

    return (20 + 7);