# Problem with send() function making OOT

**URL:** <https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454>\
**Category:** GNU Radio\
**Created:** [June 24, 2015, 9:15pm UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454 "2015-06-24T21:15:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [June 24, 2015, 9:15pm UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/1 "2015-06-24T21:15:09Z")

</div>

Hi all,

I am trying to make 2 TX 1Rx tagged stream block(OOT). For 1Tx 1Rx, it  
was  
working fine. I am trying to extend it. Now the problem is, I am not  
being  
able to configure the send() function.

If I try make test, it does not show any problem. However, when I check  
after reception I find that 1 transmitter is transmitting, and another  
is  
not. I checked the every port of my USRP X310, it is working fine.

Here’s the code. I putting a short part where I have problem. Please  
let  
me if you need the header and grc file as well, to correct the code.

* * *

void

usrp\_echotimer\_cc\_impl::send()

{

// Data to USRP

num\_tx\_samps = d\_tx\_stream-\>send(d\_in\_send1, total\_num\_samps,  
d\_metadata\_tx, total\_num\_samps/(float)d\_samp\_rate+d\_timeout\_tx);

num\_tx\_samps = d\_tx\_stream-\>send(d\_in\_send0, total\_num\_samps,  
d\_metadata\_tx, total\_num\_samps/(float)d\_samp\_rate+d\_timeout\_tx);

}

int

usrp\_echotimer\_cc\_impl::work (int noutput\_items,

gr\_vector\_int &ninput\_items,

gr\_vector\_const\_void\_star &input\_items,

gr\_vector\_void\_star &output\_items)

{

gr\_complex \*in0 = (gr\_complex \*) input\_items[0];

gr\_complex \*in1 = (gr\_complex \*) input\_items[1];

gr\_complex \*out = (gr\_complex \*) output\_items[0];

// Set output items on packet length

noutput\_items = ninput\_items[0]=ninput\_items[1];

// Resize output buffer

if(d\_out\_buffer.size()!=noutput\_items)  
d\_out\_buffer.resize(noutput\_items);

// Send thread

d\_in\_send0 = in0;

d\_in\_send1 = in1;

d\_noutput\_items\_send = noutput\_items;

d\_thread\_send =  
gr:🧵:thread(boost::bind(&usrp\_echotimer\_cc\_impl::send, this));

// Receive thread

d\_out\_recv = &d\_out\_buffer[0];

d\_noutput\_items\_recv = noutput\_items;

d\_thread\_recv =  
gr:🧵:thread(boost::bind(&usrp\_echotimer\_cc\_impl::receive, this));

* * *

My system config is X310, daughterboard SBX-120 and I am using UHD-3.9

I checked the subdev specification, gain and frequency assignment. Those  
are fine.

Please let me know how to correct and thanks in advance.

best regards

Sanjoy

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [June 24, 2015, 10:55pm UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/2 "2015-06-24T22:55:39Z")

</div>

Hi Sanjoy,

> I am trying to make 2 TX 1Rx tagged stream block(OOT). For 1Tx 1Rx, it  
> was working fine. I am trying to extend it. Now the problem is, I am  
> not being able to configure the send() function.  
> Is there a particular reason you’re creating your own block? Is there a  
> feature missing on the USRP sink and source that come with gr-uhd?  
> If I try make test, it does not show any problem.  
> Since you wrote those tests: what are they covering?

your send() function looks a bit strange; what you’re doing is  
transmitting two things on a single channel after each other. Also, what  
you should be doing (from a programming style point of view) is pass the  
buffers you want to send as references or something, but not save them  
to class properties and then call send().  
To send two buffers to two different channels, you will need to use a  
vector containing the two buffers – have a look at rx\_multi\_samples;  
that of course recv()s rather than sends(), but the semantics are the  
same.

> noutput\_items = ninput\_items[0]=ninput\_items[1];  
> That’s wrong.  
> noutput\_items is/given to you/ to let your work now how much it may  
> produce, that’s why it’s a parameter. overwriting that with the number  
> of input items in the 1st input stream doesn’t make much sense, and  
> setting ninput\_items[0]=ninput\_items[1] makes even less!  
> d\_thread\_send =  
> gr:🧵:thread(boost::bind(&usrp\_echotimer\_cc\_impl::send, this));  
> Uh-oh. This is where things will definitely go wrong. You break every  
> multithreading rule there is, by not restricting access to your internal  
> state, especially d\_send\_\*! There’s no reason GNU Radio couldn’t already  
> call your work function again while usrp\_echotimer\_cc\_impl::send()  
> hasn’t even started to transmit samples. Then, your d\_send variables  
> will be overwritten. I really doubt this will work reliably, even in a  
> 1TX 1RX configuration, but multithreading makes it really hard to notice  
> such mistakes.

I think what you’re trying to do is most probably already implemented in  
the USRP sink and source blocks:

Since a single X310 is inherently coherent and has the same time, you  
can simply use a USRP sink and a source, use set\_start\_time(…) on both  
with the same time spec, and have your flow graph consume and produce  
samples in different threads, coherently.

Best regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [June 24, 2015, 10:59pm UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/3 "2015-06-24T22:59:10Z")

</div>

Sorry, Sanjoy,

we’ll need some more information before we can give you better feedback.  
It’s not clear exactly what you’re trying to achieve, and what exactly  
is failing.

Perhaps this helps getting started:  
[http://gnuradio.org/redmine/projects/gnuradio/wiki/ReportingErrors](http://gnuradio.org/redmine/projects/gnuradio/wiki/ReportingErrors)

Cheers,  
Martin

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [June 25, 2015, 12:28pm UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/4 "2015-06-25T12:28:51Z")

</div>

Hi Sanjoy,  
Nice project!

> With this, I am actually trying to achieve time sync for X310. With  
> separate UHD source and sink–I could not achieve time sync with  
> different ports.  
> That’s absolutely possible! Two channels in the same X310 have the same  
> clock and time source.

For example, generate a Flow graph with GRC with a USRP Source,  
configure it to have 2 channels. This will automatically make sure that  
these two channels are time-aligned.  
If you have a sink in your flow graph, you can do exactly the same:  
do the followin in your python file, just before tb.start() gets called:

now = usrp\_source0.get\_time\_now() #or however your source is called  
starttime = now + uhd.time\_spec(1.5) #1.5 seconds in the future  
usrp\_source0.set\_start\_time(starttime)  
usrp\_sink0.set\_start\_time(starttime).

The multiple sample streams that now come out of the USRP source and go  
into the USRP sink will now start exactly at the same time; that means,  
there is (for a single sampling rate/master clock rate/frequency  
combination) a static offset between RX and TX, due to the fact that the  
TX timestamp applies to the point in time where a sample enters the DSP  
chain on the X310, and the RX timestamp to the time it exits the DSP  
chain.

If you have more than one X310, you will need to synchronize these by  
means of hardware (e.g. external PPS) and use multiple addresses as the  
USRP block’s address.

> The code is from CEL,KIT --Echotimer. I tried it over X310, after using  
> the subdev\_spec, it worked nicely for SISO. I am trying to use it for  
> MIMO. I am not really much good at this coding. I just started learning  
> it this month.  
> Echotimer was originally a tool used to calibrate out the static RX/TX  
> delay you get for the direct path between the radar TX antenna and RX  
> antenna. That offset was typically empirically observed and then used as  
> a parameter.  
> Now, for MIMO, the situation would be much more complex, because you  
> wouldn’t only have an offset between TX and RX, but also between  
> different TXes and RXes, if not the USRPs had the ability to be  
> coherent, based on timed commands (which you use by setting the start  
> time).  
> You can get the same behaviour for your MIMO case (that is, delay one  
> stream by a N samples) by just starting the USRP sink N/f\_sample later,  
> using the set\_start\_time method.

Stefan’s Wunsch’s adaptation of echotimer in gr-radar has the advantage  
that it is a tagged stream block, which is really handy for “bursty”  
applications like radar. I don’t know if only getting bursts of data is  
what you want, or if you’d prefer continuous sample streams like the UHD  
sink and source give you.

There’s one thing that could use improvement in the echotimer code: If  
the set\_?x\_freq() were done after the time and clock sources had been  
set as a timed command (i.e. after calling set\_command\_time()), then the  
SBX daughterboards would always have the same phase relationship after  
tuning to the same frequency, which might be really handy in MIMO.

In its wholeness, your block looks a lot better! I’m especially relieved  
by seeing the thread.join() methods being used to ensure that the block  
waits long enough until samples have been sent and received.

Overall, I think you might want to try using the normal USRP sink and  
USRP source, synchronizing them via timed commands (set\_start\_time) 🙂  
If they give you what you want, then you’re better off without  
echotimer. If you really want to integrate echotimer into your own OOT  
(instead of just relying on installing gr-radar, which would probably be  
totally OK), you should make the “namespace … {” directive match your  
OOT’s other blocks.

Best regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [June 25, 2015, 11:45am UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/5 "2015-06-25T11:45:18Z")

</div>

Hi Marcus,  
Thanks for the response. I did not get any notification in my inbox. So  
I put it there.

I am trying to implement MIMO OFDM radar with X310.  
With this, I am actually trying to achieve time sync for X310. With  
separate UHD source and sink–I could not achieve time sync with  
different ports.

The code is from CEL,KIT --Echotimer. I tried it over X310, after using  
the subdev\_spec, it worked nicely for SISO. I am trying to use it for  
MIMO. I am not really much good at this coding. I just started learning  
it this month.

I will try to correct the code with the points you mentioned.  
I am putting the the impl.cc code, which is a bit large. But it would be  
really kind if you can go through and find any further problem to  
correct.

best regards  
Sanjoy Basak  
thesis student, IHE, KIT

The subdev spec and others I hardcoded those to check whether works or  
not.

/\* -_- c++ -_- _/  
/_

- Copyright 2014 Communications Engineering Lab, KIT.
- 
- This is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3, or (at your option)
- any later version.
- 
- This software is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- 
- You should have received a copy of the GNU General Public License
- along with this software; see the file COPYING. If not, write to
- the Free Software Foundation, Inc., 51 Franklin Street,
- Boston, MA 02110-1301, USA.  
\*/

#ifdef HAVE\_CONFIG\_H  
#include “config.h”  
#endif

#include \<gnuradio/io\_signature.h\>  
#include “usrp\_echotimer\_cc\_impl.h”  
#include

namespace gr {  
namespace radar {

```
usrp_echotimer_cc::sptr
usrp_echotimer_cc::make(int samp_rate, float center_freq, int

```

num\_delay\_samps, std::string args\_tx, std::string wire\_tx, std::string  
clock\_source\_tx, std::string time\_source\_tx, std::string antenna\_tx1,  
std::string antenna\_tx2, float gain\_tx, float timeout\_tx, float wait\_tx,  
float lo\_offset\_tx, std::string wire\_rx, std::string antenna\_rx, float  
gain\_rx, float timeout\_rx, float wait\_rx, float lo\_offset\_rx,  
std::string spec\_tx, std::string spec\_tx2, std::string spec\_rx, const  
std::string& len\_key)  
{  
return gnuradio::get\_initial\_sptr  
(new usrp\_echotimer\_cc\_impl(samp\_rate, center\_freq,  
num\_delay\_samps, args\_tx, wire\_tx, clock\_source\_tx, time\_source\_tx,  
antenna\_tx1, antenna\_tx2, gain\_tx, timeout\_tx, wait\_tx, lo\_offset\_tx,  
wire\_rx, antenna\_rx, gain\_rx, timeout\_rx, wait\_rx, lo\_offset\_rx,  
spec\_tx, spec\_tx2, spec\_rx, len\_key));  
}

```
/*
 * The private constructor
 */
usrp_echotimer_cc_impl::usrp_echotimer_cc_impl(int samp_rate, float

```

center\_freq, int num\_delay\_samps, std::string args\_tx, std::string  
wire\_tx, std::string clock\_source\_tx, std::string time\_source\_tx,  
std::string antenna\_tx1, std::string antenna\_tx2, float gain\_tx, float  
timeout\_tx, float wait\_tx, float lo\_offset\_tx, std::string wire\_rx,  
std::string antenna\_rx, float gain\_rx, float timeout\_rx, float wait\_rx,  
float lo\_offset\_rx, std::string spec\_tx, std::string spec\_tx2,  
std::string spec\_rx, const std::string& len\_key)  
: gr::tagged\_stream\_block(“usrp\_echotimer\_cc”,  
gr::io\_signature::make(2, 2, sizeof(gr\_complex)),  
gr::io\_signature::make(1, 1, sizeof(gr\_complex)), len\_key)  
{  
d\_samp\_rate = samp\_rate;  
d\_center\_freq = center\_freq;  
d\_num\_delay\_samps = num\_delay\_samps;  
d\_out\_buffer.resize(0);

```
// *****Setup USRP TX***** //

d_spec_tx=spec_tx;
d_spec_tx2=spec_tx2;
d_args_tx = args_tx;
d_wire_tx = wire_tx;
d_clock_source_tx = clock_source_tx;
d_time_source_tx = time_source_tx;
d_antenna_tx1 = antenna_tx1;
d_antenna_tx2 = antenna_tx2;
d_lo_offset_tx = lo_offset_tx;
d_gain_tx = gain_tx;
d_timeout_tx = timeout_tx; // timeout for sending
d_wait_tx = wait_tx; // secs to wait befor sending

// Setup USRP TX: args (addr,...)
d_usrp_tx = uhd::usrp::multi_usrp::make(d_args_tx);
std::cout << "Using USRP Device (TX): " << std::endl <<

```

d\_usrp\_tx-\>get\_pp\_string() \<\< std::endl;

```
//setup subdev spec
d_usrp_tx -> set_tx_subdev_spec ( uhd::usrp::subdev_spec_t ("B:0

```

A:0"));  
//set\_subdev\_spec(const std::string &spec\_tx, size\_t mboard);

```
//setup subdev spec for tx2--same usrp
//d_usrp_tx -> set_tx_subdev_spec ( uhd::usrp::subdev_spec_t

```

(d\_spec\_tx2),0);

```
// Setup USRP TX: sample rate
std::cout << "Setting TX Rate: " << d_samp_rate << std::endl;
d_usrp_tx->set_tx_rate(d_samp_rate);
std::cout << "Actual TX Rate: " << d_usrp_tx->get_tx_rate() <<

```

std::endl;

```
// Setup USRP TX: gain
set_tx_gain(d_gain_tx);

// Setup USRP TX: tune request
d_tune_request_tx = uhd::tune_request_t(d_center_freq); // FIXME:

```

add alternative tune requests  
d\_usrp\_tx-\>set\_tx\_freq(d\_tune\_request\_tx);  
//d\_usrp\_tx-\>set\_tx\_freq(d\_tune\_request\_tx,1);

```
// Setup USRP TX: antenna
d_usrp_tx->set_tx_antenna(d_antenna_tx1,0);
d_usrp_tx->set_tx_antenna(d_antenna_tx2,1);

// Setup USRP TX: clock source
d_usrp_tx->set_clock_source(d_clock_source_tx); // Set TX clock, TX

```

is master

```
// Setup USRP TX: time source
d_usrp_tx->set_time_source(d_time_source_tx); // Set TX time, TX is

```

master  
//d\_usrp\_tx-\>set\_time\_source(d\_time\_source\_tx,1); // Set  
TX time, TX is mast  
// Setup USRP TX: timestamp  
if(d\_time\_source\_tx!=“gpsdo”){  
d\_usrp\_tx-\>set\_time\_now(uhd::time\_spec\_t(0.0)); // Do set time on  
startup if not gpsdo is activated.  
}

```
// Setup transmit streamer
uhd::stream_args_t stream_args_tx("fc32", d_wire_tx); // complex

```

floats  
d\_tx\_stream = d\_usrp\_tx-\>get\_tx\_stream(stream\_args\_tx);

```
// *****Setup USRP RX***** //

d_spec_rx=spec_rx;

d_args_rx = args_tx;
d_wire_rx = wire_rx;
d_clock_source_rx = clock_source_tx;
d_time_source_rx = time_source_tx;
d_antenna_rx = antenna_rx;
d_lo_offset_rx = lo_offset_rx;
d_gain_rx = gain_rx;
d_timeout_rx = timeout_rx; // timeout for receiving
d_wait_rx = wait_rx; // secs to wait befor receiving

// Setup USRP RX: args (addr,...)
d_usrp_rx = uhd::usrp::multi_usrp::make(d_args_rx);
std::cout << "Using USRP Device (RX): " << std::endl <<

```

d\_usrp\_rx-\>get\_pp\_string() \<\< std::endl;

```
//setup subdev spec
d_usrp_rx -> set_rx_subdev_spec ( uhd::usrp::subdev_spec_t

```

(d\_spec\_rx));

```
// Setup USRP RX: sample rate
std::cout << "Setting RX Rate: " << d_samp_rate << std::endl;
d_usrp_rx->set_rx_rate(d_samp_rate);
std::cout << "Actual RX Rate: " << d_usrp_rx->get_rx_rate() <<

```

std::endl;

```
// Setup USRP RX: gain
set_rx_gain(d_gain_rx);

// Setup USRP RX: tune request
d_tune_request_rx = uhd::tune_request_t(d_center_freq,

```

d\_lo\_offset\_rx); // FIXME: add alternative tune requests  
d\_usrp\_rx-\>set\_rx\_freq(d\_tune\_request\_rx);

```
// Setup USRP RX: antenna
d_usrp_rx->set_rx_antenna(d_antenna_rx);

// Setup USRP RX: clock source
d_usrp_rx->set_clock_source(d_clock_source_rx); // RX is slave,

```

clock is set on TX

```
// Setup USRP RX: time source
d_usrp_rx->set_time_source(d_time_source_rx);

// Setup receive streamer
uhd::stream_args_t stream_args_rx("fc32", d_wire_rx); // complex

```

floats  
std::vector\<size\_t\> channel\_nums; channel\_nums.push\_back(0); //  
define channel!  
stream\_args\_rx.channels = channel\_nums;  
d\_rx\_stream = d\_usrp\_tx-\>get\_rx\_stream(stream\_args\_rx);

```
// *****Misc***** //

// Setup rx_time pmt
d_time_key = pmt::string_to_symbol("rx_time");
d_srcid = pmt::string_to_symbol("usrp_echotimer");

// Setup thread priority
//uhd::set_thread_priority_safe(); // necessary? doesnt work...

// Sleep to get sync done
boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); //

```

FIXME: necessary?  
}

```
/*
 * Our virtual destructor.
 */
usrp_echotimer_cc_impl::~usrp_echotimer_cc_impl()
{
}

int
usrp_echotimer_cc_impl::calculate_output_stream_length(const

```

gr\_vector\_int &ninput\_items)  
{  
int noutput\_items = ninput\_items[0];  
return noutput\_items ;  
}

```
void
usrp_echotimer_cc_impl::set_num_delay_samps(int num_samps){
d_num_delay_samps = num_samps;

```

}

void  
usrp\_echotimer\_cc\_impl::set\_rx\_gain(float gain){  
d\_usrp\_rx-\>set\_rx\_gain(gain);  
}

void  
usrp\_echotimer\_cc\_impl::set\_tx\_gain(float gain){  
d\_usrp\_tx-\>set\_tx\_gain(gain);  
}

```
void
usrp_echotimer_cc_impl::send()
{
// Setup metadata for first package
    d_metadata_tx.start_of_burst = true;
d_metadata_tx.end_of_burst = false;
d_metadata_tx.has_time_spec = true;
d_metadata_tx.time_spec = d_time_now_tx+uhd::time_spec_t(d_wait_tx);

```

// Timespec needed?

```
// Send input buffer
size_t num_acc_samps = 0; // Number of accumulated samples
size_t num_tx_samps, total_num_samps;
total_num_samps = d_noutput_items_send;
// Data to USRP
num_tx_samps = d_tx_stream->send(d_in_send0, total_num_samps,

```

d\_metadata\_tx, total\_num\_samps/(float)d\_samp\_rate+d\_timeout\_tx);  
num\_tx\_samps = d\_tx\_stream-\>send(d\_in\_send1, total\_num\_samps,  
d\_metadata\_tx, total\_num\_samps/(float)d\_samp\_rate+d\_timeout\_tx);  
// Get timeout  
if (num\_tx\_samps \< total\_num\_samps) std::cerr \<\< “Send timeout…”  
\<\< std::endl;

```
//send a mini EOB packet
d_metadata_tx.start_of_burst = false;
d_metadata_tx.end_of_burst = true;
d_metadata_tx.has_time_spec = false;
d_tx_stream->send("", 0, d_metadata_tx);
}

void
usrp_echotimer_cc_impl::receive()
{
// Setup RX streaming
size_t total_num_samps = d_noutput_items_recv;
uhd::stream_cmd_t

```

stream\_cmd(uhd::stream\_cmd\_t::STREAM\_MODE\_NUM\_SAMPS\_AND\_DONE);  
stream\_cmd.num\_samps = total\_num\_samps;  
stream\_cmd.stream\_now = false;  
stream\_cmd.time\_spec = d\_time\_now\_rx+uhd::time\_spec\_t(d\_wait\_rx);  
d\_rx\_stream-\>issue\_stream\_cmd(stream\_cmd);

```
size_t num_rx_samps;
// Receive a packet
num_rx_samps = d_rx_stream->recv(d_out_recv, total_num_samps,

```

d\_metadata\_rx, total\_num\_samps/(float)d\_samp\_rate+d\_timeout\_rx);

```
// Save timestamp
d_time_val = pmt::make_tuple
  (pmt::from_uint64(d_metadata_rx.time_spec.get_full_secs()),
   pmt::from_double(d_metadata_rx.time_spec.get_frac_secs()));

// Handle the error code
if (d_metadata_rx.error_code !=

```

uhd::rx\_metadata\_t::ERROR\_CODE\_NONE){  
throw std::runtime\_error(str(boost::format(“Receiver error %s”) %  
d\_metadata\_rx.strerror()));  
}

```
if (num_rx_samps < total_num_samps) std::cerr << "Receive timeout

```

before all samples received…" \<\< std::endl;  
}

```
int
usrp_echotimer_cc_impl::work (int noutput_items,
                   gr_vector_int &ninput_items,
                   gr_vector_const_void_star &input_items,
                   gr_vector_void_star &output_items)
{
    gr_complex *in0 = (gr_complex *) input_items[0]; // remove const

```

gr\_complex \*in1 = (gr\_complex \*) input\_items[1];  
gr\_complex \*out = (gr\_complex \*) output\_items[0];

```
    // Set output items on packet length
    noutput_items = ninput_items[0]=ninput_items[1];

    // Resize output buffer
    if(d_out_buffer.size()!=noutput_items)

```

d\_out\_buffer.resize(noutput\_items);

```
    // Get time from USRP TX
    d_time_now_tx = d_usrp_tx->get_time_now();
    d_time_now_rx = d_time_now_tx;

    // Send thread
    d_in_send0 = in0;
    d_in_send1 = in1;
    d_noutput_items_send = noutput_items;
    d_thread_send =

```

gr:🧵:thread(boost::bind(&usrp\_echotimer\_cc\_impl::send, this));

```
    // Receive thread
    d_out_recv = &d_out_buffer[0];
    d_noutput_items_recv = noutput_items;
    d_thread_recv =

```

gr:🧵:thread(boost::bind(&usrp\_echotimer\_cc\_impl::receive, this));

```
    // Wait for threads to complete
    d_thread_send.join();
    d_thread_recv.join();

    // Shift of number delay samples (fill with zeros)
    memcpy(out,&d_out_buffer[0]+d_num_delay_samps,(noutput_items-d_num_delay_samps)*sizeof(gr_complex));

```

// push buffer to output  
memset(out+(noutput\_items-d\_num\_delay\_samps),0,d\_num\_delay\_samps\*sizeof(gr\_complex));  
// set zeros

```
    // Setup rx_time tag
    add_item_tag(0, nitems_written(0), d_time_key, d_time_val,

```

d\_srcid);

```
    // Tell runtime system how many output items we produced.
    return noutput_items;
}

```

} /\* namespace radar _/  
} /_ namespace gr \*/

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [June 27, 2015, 4:42am UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/6 "2015-06-27T04:42:06Z")

</div>

This is just a wish, but it would be nice if the set\_start\_time sequence  
shown below could be added to the UHD Sink and Source blocks in GRC.  
Seems generic enough (with the delay as a variable).

Ron

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [June 28, 2015, 11:02am UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/7 "2015-06-28T11:02:59Z")

</div>

Hi Sanjoy,

On 06/28/2015 12:27 AM, Sanjoy Basak wrote:

> However, this is not working for 2 X310s. I am not being able to make  
> time aligned Tx, Rx. I am using external reference. Would you please  
> suggest me some code/ modification for pyton file, so that the ports can  
> be time aligned.  
> Have you set both the clock and the time source to “external”  
> (set\_clock\_source, set\_time\_source), and told both USRPs to have the  
> same time at the next time pulse (set\_time\_next\_pps)?

Best regards,  
Marcus

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [June 28, 2015, 12:27am UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/8 "2015-06-28T00:27:03Z")

</div>

Hi Marcus,  
Thank you very much for the tips and suggestions. This is really  
helpful.  
You are right, I should not rely on Echotimer, rather I should try to  
make one, based on my requirements.

First I tried the python code. This really worked for one X310 frontend  
ports. I tried for 1Tx -2Rx and 2Tx – 1Rx. Those are time aligned with  
the python code you suggested.

However, this is not working for 2 X310s. I am not being able to make  
time aligned Tx, Rx. I am using external reference. Would you please  
suggest me some code/ modification for pyton file, so that the ports can  
be time aligned.

I also tried the code suggested in this link  
[http://usrp-users.ettus.narkive.com/6qvbyOIf/synchronizing-usrp-front-ends-via-gnu-radio](http://usrp-users.ettus.narkive.com/6qvbyOIf/synchronizing-usrp-front-ends-via-gnu-radio)

The code is actually to synchronize Tx front ends of 2 X310s.  
I tried this for 1Tx - 1 Rx (Tx on usrp with gpsdo and Rx on usrp with  
external reference). But this did not help to synchronize on time.

Best regards  
Sanjoy

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [July 19, 2015, 12:20am UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/9 "2015-07-19T00:20:32Z")

</div>

Hi Marcus,

I need help regarding the phase sync part that you mentioned in your  
previous post here.

“There’s one thing that could use improvement in the echotimer code: If  
the set\_?x\_freq() were done after the time and clock sources had been  
set as a timed command (i.e. after calling set\_command\_time()), then the  
SBX daughterboards would always have the same phase relationship after  
tuning to the same frequency, which might be really handy in MIMO.”

This part I tried in python, however, I am not actually getting any  
phase sync between the receive files. I am not really sure whether  
defining freq after set\_command\_time() in python can really help.  
However, please let me know what modification should I make or what  
exactly I need to do.

I am putting the python code here that I am using right now for 2 usrps.  
2 TXs are in one x310 (on gpsdo) and 2 RXs are on the other one  
(external ref).  
and the connection is TX-wire-30\_dB\_attenuator-wire-RX, I am taking  
wired measurement. I am using 2.45 GHz freq.

* * *

```
    self.usrp_source0 = uhd.usrp_source(
      ",".join(("addr=172.xx.xx.73", "")),
      uhd.stream_args(
        cpu_format="fc32",
        channels=range(2),
      ),
    )

    self.usrp_sink0 = uhd.usrp_sink(
      ",".join(("addr=172.xx.xx.75", "")),
      uhd.stream_args(
        cpu_format="fc32",
        channels=range(2),
      ),
    )

    self.usrp_source0.set_clock_source("external", 0)
    self.usrp_source0.set_time_source("external", 0)
    self.usrp_source0.set_subdev_spec("A:0 B:0", 0)
    self.usrp_source0.set_time_unknown_pps(uhd.time_spec())
    self.usrp_source0.set_samp_rate(samp_rate)
    self.usrp_source0.set_gain(26, 0)
    self.usrp_source0.set_antenna("RX2", 0)
    self.usrp_source0.set_gain(26, 1)
    self.usrp_source0.set_antenna("RX2", 1)

    self.usrp_sink0.set_clock_source("gpsdo", 0)
    self.usrp_sink0.set_time_source("gpsdo", 0)
    self.usrp_sink0.set_subdev_spec("A:0 B:0", 0)
    self.usrp_sink0.set_time_unknown_pps(uhd.time_spec())
    self.usrp_sink0.set_samp_rate(samp_rate)
    self.usrp_sink0.set_gain(0, 0)
    self.usrp_sink0.set_antenna("TX/RX", 0)
    self.usrp_sink0.set_gain(0, 1)
    self.usrp_sink0.set_antenna("TX/RX", 1)

```

time.sleep(1)  
print “receiver pps time:”  
cmd\_time = self.usrp\_source0.get\_time\_last\_pps()  
print “transmitter pps time:”  
cmd\_time0 = self.usrp\_sink0.get\_time\_last\_pps()  
real\_seconds = uhd.time\_spec\_t.get\_real\_secs(cmd\_time)  
real\_seconds0 = uhd.time\_spec\_t.get\_real\_secs(cmd\_time0)

print real\_seconds  
print real\_seconds0

self.usrp\_source0.set\_time\_next\_pps(uhd.time\_spec\_t(0.0))  
self.usrp\_sink0.set\_time\_next\_pps(uhd.time\_spec\_t(0.0))

now = self.usrp\_source0.get\_time\_now()  
starttime = now + uhd.time\_spec(1.5)  
self.usrp\_source0.set\_start\_time(starttime)  
self.usrp\_sink0.set\_start\_time(starttime)

```
    self.usrp_source0.set_center_freq(f, 0)
    self.usrp_source0.set_center_freq(f, 1)
    self.usrp_sink0.set_center_freq(f, 0)
    self.usrp_sink0.set_center_freq(f, 1)

    self.blocks_throttle_0_1 =

```

## blocks.throttle(gr.sizeof\_gr\_complex_1, samp\_rate,True) self.blocks\_throttle\_0\_0\_0 = blocks.throttle(gr.sizeof\_gr\_complex_1, samp\_rate,True) self.blocks\_throttle\_0\_0 = blocks.throttle(gr.sizeof\_gr\_complex_1, samp\_rate,True) self.blocks\_throttle\_0 = blocks.throttle(gr.sizeof\_gr\_complex_1, samp\_rate,True) self.blocks\_file\_source\_0\_0 = blocks.file\_source(gr.sizeof\_gr\_complex_1, “/home/sanjoy/Desktop/new/siso with new preamble 3/20150603\_siso\_echotimer/floatgen.bin”, True) self.blocks\_file\_source\_0 = blocks.file\_source(gr.sizeof\_gr\_complex_1, “/home/sanjoy/Desktop/new/siso with new preamble 3/20150603\_siso\_echotimer/floatgen.bin”, True) self.blocks\_file\_sink\_0\_0 = blocks.file\_sink(gr.sizeof\_gr\_complex_1, “/home/sanjoy/Desktop/siso2.bin”, False) self.blocks\_file\_sink\_0\_0.set\_unbuffered(False) self.blocks\_file\_sink\_0 = blocks.file\_sink(gr.sizeof\_gr\_complex_1, “/home/sanjoy/Desktop/siso1.bin”, False) self.blocks\_file\_sink\_0.set\_unbuffered(False)

My system config is usrp X310, daughterboard SBX-120, uhd 3.9.

best regards  
Sanjoy

---

<div class="post-metadata">

**Author:** ![Sanjoy\_Basak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@Sanjoy\_Basak](https://www.ruby-forum.com/u/Sanjoy_Basak)\
**Post date:** [June 28, 2015, 5:16pm UTC](https://www.ruby-forum.com/t/problem-with-send-function-making-oot/242454/10 "2015-06-28T17:16:33Z")

</div>

Hi Marcus,  
Thanks for the reply.  
This worked today. Unfortunately set\_command\_time(),  
clear\_command\_time() did not work well with my setup.  
The Rx start point was varying with every initiation.

With the same code today I tried with set\_start\_time() and this worked  
really well.  
The time sync property with 2 X310 is the same like 1 X310.  
Thanks a lot.

Best regards  
Sanjoy
