From ecd3d91b55d60f0d44d41c77f6a46bc6d5aca01c Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 27 Oct 2014 13:46:10 +0100 Subject: examples: Fixed multi-channel ops --- host/examples/txrx_loopback_to_file.cpp | 59 +++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 7 deletions(-) (limited to 'host/examples/txrx_loopback_to_file.cpp') diff --git a/host/examples/txrx_loopback_to_file.cpp b/host/examples/txrx_loopback_to_file.cpp index a62ccd7b2..10d6b7af4 100644 --- a/host/examples/txrx_loopback_to_file.cpp +++ b/host/examples/txrx_loopback_to_file.cpp @@ -24,13 +24,12 @@ #include #include #include -#include #include #include #include +#include #include #include -#include #include #include @@ -42,6 +41,26 @@ namespace po = boost::program_options; static bool stop_signal_called = false; void sig_int_handler(int){stop_signal_called = true;} +/*********************************************************************** + * Utilities + **********************************************************************/ +//! Change to filename, e.g. from usrp_samples.dat to usrp_samples.00.dat, +// but only if multiple names are to be generated. +std::string generate_out_filename(const std::string &base_fn, size_t n_names, size_t this_name) +{ + if (n_names == 1) { + return base_fn; + } + + boost::filesystem::path base_fn_fp(base_fn); + base_fn_fp.replace_extension( + boost::filesystem::path( + str(boost::format("%02d%s") % this_name % base_fn_fp.extension().string()) + ) + ); + return base_fn_fp.string(); +} + /*********************************************************************** * Waveform generators **********************************************************************/ @@ -142,9 +161,26 @@ template void recv_to_file( stream_args.channels = rx_channel_nums; uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args); + // Prepare buffers for received samples and metadata uhd::rx_metadata_t md; - std::vector buff(samps_per_buff); - std::ofstream outfile(file.c_str(), std::ofstream::binary); + std::vector > buffs( + rx_channel_nums.size(), std::vector< samp_type >(samps_per_buff) + ); + //create a vector of pointers to point to each of the channel buffers + std::vector buff_ptrs; + for (size_t i = 0; i < buffs.size(); i++) { + buff_ptrs.push_back(&buffs[i].front()); + } + + // Create one ofstream object per channel + // (use shared_ptr because ofstream is non-copyable) + std::vector > outfiles; + for (size_t i = 0; i < buffs.size(); i++) { + const std::string this_filename = generate_out_filename(file, buffs.size(), i); + outfiles.push_back(boost::shared_ptr(new std::ofstream(this_filename.c_str(), std::ofstream::binary))); + } + UHD_ASSERT_THROW(outfiles.size() == buffs.size()); + UHD_ASSERT_THROW(buffs.size() == rx_channel_nums.size()); bool overflow_message = true; float timeout = settling_time + 0.1; //expected settling time + padding for first recv @@ -159,7 +195,7 @@ template void recv_to_file( rx_stream->issue_stream_cmd(stream_cmd); while(not stop_signal_called and (num_requested_samples != num_total_samps or num_requested_samples == 0)){ - size_t num_rx_samps = rx_stream->recv(&buff.front(), buff.size(), md, timeout); + size_t num_rx_samps = rx_stream->recv(buff_ptrs, samps_per_buff, md, timeout); timeout = 0.1; //small timeout for subsequent recv if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) { @@ -187,10 +223,19 @@ template void recv_to_file( num_total_samps += num_rx_samps; - outfile.write((const char*)&buff.front(), num_rx_samps*sizeof(samp_type)); + for (size_t i = 0; i < outfiles.size(); i++) { + outfiles[i]->write((const char*) buff_ptrs[i], num_rx_samps*sizeof(samp_type)); + } } - outfile.close(); + // Shut down receiver + stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS; + rx_stream->issue_stream_cmd(stream_cmd); + + // Close files + for (size_t i = 0; i < outfiles.size(); i++) { + outfiles[i]->close(); + } } -- cgit v1.2.3