diff options
author | Josh Blum <josh@joshknows.com> | 2011-03-11 11:14:30 -0800 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2011-03-11 11:14:30 -0800 |
commit | 1ab3803180009399aba8d8c58d5fae2645ac1ff3 (patch) | |
tree | 3b99cbcf4d2f44fd0ea109b892192d1ab2cbe40e /host/examples/rx_samples_to_file.cpp | |
parent | ab263488b00622b94e4644ea432c8185d58501c6 (diff) | |
download | uhd-1ab3803180009399aba8d8c58d5fae2645ac1ff3.tar.gz uhd-1ab3803180009399aba8d8c58d5fae2645ac1ff3.tar.bz2 uhd-1ab3803180009399aba8d8c58d5fae2645ac1ff3.zip |
uhd: created tx_samples_from_file.cpp and added to rx_samples_to_file.cpp
Diffstat (limited to 'host/examples/rx_samples_to_file.cpp')
-rw-r--r-- | host/examples/rx_samples_to_file.cpp | 81 |
1 files changed, 39 insertions, 42 deletions
diff --git a/host/examples/rx_samples_to_file.cpp b/host/examples/rx_samples_to_file.cpp index e202fcb1c..bbfcc6d7c 100644 --- a/host/examples/rx_samples_to_file.cpp +++ b/host/examples/rx_samples_to_file.cpp @@ -27,12 +27,41 @@ namespace po = boost::program_options; +template<typename samp_type> void recv_to_file( + uhd::usrp::multi_usrp::sptr usrp, + const uhd::io_type_t &io_type, + const std::string &file, + size_t samps_per_buff +){ + uhd::rx_metadata_t md; + std::vector<samp_type> buff(samps_per_buff); + std::ofstream outfile(file.c_str(), std::ofstream::binary); + + while(true){ + size_t num_rx_samps = usrp->get_device()->recv( + &buff.front(), buff.size(), md, io_type, + uhd::device::RECV_MODE_FULL_BUFF + ); + + if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) break; + if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE){ + throw std::runtime_error(str(boost::format( + "Unexpected error code 0x%x" + ) % md.error_code)); + } + + outfile.write((const char*)&buff.front(), num_rx_samps*sizeof(samp_type)); + } + + outfile.close(); +} + int UHD_SAFE_MAIN(int argc, char *argv[]){ uhd::set_thread_priority_safe(); //variables to be set by po - std::string args, file; - size_t total_num_samps; + std::string args, file, type; + size_t total_num_samps, spb; double rate, freq, gain; //setup the program options @@ -40,8 +69,10 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ desc.add_options() ("help", "help message") ("args", po::value<std::string>(&args)->default_value(""), "multi uhd device address args") - ("file", po::value<std::string>(&file)->default_value("out.16sc.dat"), "name of the file to write binary samples to") + ("file", po::value<std::string>(&file)->default_value("usrp_samples.dat"), "name of the file to write binary samples to") + ("type", po::value<std::string>(&type)->default_value("float"), "choose the sample type: double, float, or short") ("nsamps", po::value<size_t>(&total_num_samps)->default_value(1000), "total number of samples to receive") + ("spb", po::value<size_t>(&spb)->default_value(10000), "samples per buffer") ("rate", po::value<double>(&rate)->default_value(100e6/16), "rate of incoming samples") ("freq", po::value<double>(&freq)->default_value(0), "rf center frequency in Hz") ("gain", po::value<double>(&gain)->default_value(0), "gain for the RF chain") @@ -85,45 +116,11 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ stream_cmd.stream_now = true; usrp->issue_stream_cmd(stream_cmd); - //loop until total number of samples reached - size_t num_acc_samps = 0; //number of accumulated samples - uhd::rx_metadata_t md; - std::vector<std::complex<short> > buff(usrp->get_device()->get_max_recv_samps_per_packet()); - std::ofstream outfile(file.c_str(), std::ofstream::binary); - - while(num_acc_samps < total_num_samps){ - size_t num_rx_samps = usrp->get_device()->recv( - &buff.front(), buff.size(), md, - uhd::io_type_t::COMPLEX_INT16, - uhd::device::RECV_MODE_ONE_PACKET - ); - - //handle the error codes - switch(md.error_code){ - case uhd::rx_metadata_t::ERROR_CODE_NONE: - break; - - case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: - if (num_acc_samps == 0) continue; - std::cout << boost::format( - "Got timeout before all samples received, possible packet loss, exiting loop..." - ) << std::endl; - goto done_loop; - - default: - std::cout << boost::format( - "Got error code 0x%x, exiting loop..." - ) % md.error_code << std::endl; - goto done_loop; - } - - //write complex short integer samples to the binary file - outfile.write((const char*)&buff[0], num_rx_samps * sizeof(std::complex<short>)); - - num_acc_samps += num_rx_samps; - } done_loop: - - outfile.close(); + //recv to file + if (type == "double") recv_to_file<std::complex<double> >(usrp, uhd::io_type_t::COMPLEX_FLOAT64, file, spb); + else if (type == "float") recv_to_file<std::complex<float> >(usrp, uhd::io_type_t::COMPLEX_FLOAT32, file, spb); + else if (type == "short") recv_to_file<std::complex<short> >(usrp, uhd::io_type_t::COMPLEX_INT16, file, spb); + else throw std::runtime_error("Unknown type " + type); //finished std::cout << std::endl << "Done!" << std::endl << std::endl; |