{"id":1466,"date":"2015-02-19T16:46:27","date_gmt":"2015-02-19T16:46:27","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1466"},"modified":"2015-01-17T03:02:20","modified_gmt":"2015-01-17T03:02:20","slug":"usrp1-simple-data-capture-in-c-example-uhd","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2015\/02\/usrp1-simple-data-capture-in-c-example-uhd\/","title":{"rendered":"USRP1 simple data capture in C++ example (UHD)"},"content":{"rendered":"<p>The code below streams data using the UHD at 1MSPS. It just prints the first 2 datapoints to the screen but can be used as the basis for other stuff. It&#8217;s also worth checking out rx_samples_to_file.cpp in the UHD source, but this example removes a lot of the cruft. Can be compiled with:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ng++ rx.cpp -luhd -o rx\r\n<\/pre>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &amp;lt;uhd\/types\/tune_request.hpp&amp;gt;\r\n#include &amp;lt;uhd\/utils\/thread_priority.hpp&amp;gt;\r\n#include &amp;lt;uhd\/utils\/safe_main.hpp&amp;gt;\r\n#include &amp;lt;uhd\/usrp\/multi_usrp.hpp&amp;gt;\r\n#include &amp;lt;uhd\/exception.hpp&amp;gt;\r\n#include &amp;lt;iostream&amp;gt;\r\n#include &amp;lt;fstream&amp;gt;\r\n#include &amp;lt;csignal&amp;gt;\r\n#include &amp;lt;complex&amp;gt;\r\n\r\nusing namespace std;\r\n\r\nint UHD_SAFE_MAIN(int argc, char *argv&#x5B;]){\r\n    uhd::set_thread_priority_safe();\r\n\r\n    uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(string(&amp;quot;&amp;quot;));\r\n\r\n    usrp-&amp;gt;set_clock_source(&amp;quot;internal&amp;quot;); \/\/ set clock\r\n\r\n    \/\/always select the subdevice first, the channel mapping affects the other settings\r\n    usrp-&amp;gt;set_rx_subdev_spec(string(&amp;quot;A:A&amp;quot;));\r\n\r\n    std::cout &amp;lt;&amp;lt; &amp;quot;Using Device: &amp;quot; &amp;lt;&amp;lt; usrp-&amp;gt;get_pp_string() &amp;lt;&amp;lt; std::endl;\r\n    double rate = 1000000; \/\/ trying 1MSPS\r\n    usrp-&amp;gt;set_rx_rate(rate);\r\n\r\n    \/\/set the center frequency\r\n    double freq=0;\r\n    uhd::tune_request_t tune_request(freq);\r\n    usrp-&amp;gt;set_rx_freq(tune_request);\r\n\r\n    \/\/usrp-&amp;gt;set_rx_gain(5);\r\n    \/\/usrp-&amp;gt;set_rx_bandwidth(100);\r\n\r\n\/\/    ant = &amp;quot;&amp;quot;;\r\n\/\/    usrp-&amp;gt;set_rx_antenna(ant);\r\n\r\n\/\/   cout &amp;lt;&amp;lt; usrp-&amp;gt;get_mboard_sensor(&amp;quot;lo_locked&amp;quot;, 0).to_bool() &amp;lt;&amp;lt; endl;\r\n\r\n    sleep(1);\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ RECEIVE CODE\r\n\r\n    \/\/create a receive streamer\r\n    uhd::stream_args_t stream_args(&amp;quot;sc16&amp;quot;,&amp;quot;&amp;quot;);\r\n    uhd::rx_streamer::sptr rx_stream = usrp-&amp;gt;get_rx_stream(stream_args);\r\n\r\n    uhd::rx_metadata_t md;\r\n    std::vector&amp;lt;std::complex&amp;lt;short&amp;gt; &amp;gt; buffer(50000);\r\n\r\n    \/\/setup streaming\r\n    uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);\r\n    stream_cmd.num_samps = 0;\r\n    stream_cmd.stream_now = true;\r\n    stream_cmd.time_spec = uhd::time_spec_t();\r\n    rx_stream-&amp;gt;issue_stream_cmd(stream_cmd);\r\n\r\n    for(;;) {\r\n\r\n        size_t num_rx_samps = rx_stream-&amp;gt;recv(&amp;amp;buffer.front(), buffer.size(), md, 3.0, false);\r\n\r\n        if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT) { cerr &amp;lt;&amp;lt; &amp;quot;timeout&amp;quot;  &amp;lt;&amp;lt; endl; }\r\n        if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW){ cerr &amp;lt;&amp;lt; &amp;quot;overflow&amp;quot; &amp;lt;&amp;lt; endl; }\r\n        if (md.error_code != uhd::rx_metadata_t::ERROR_CODE_NONE)    { cerr &amp;lt;&amp;lt;  md.strerror() &amp;lt;&amp;lt; endl; }\r\n\r\n        cout &amp;lt;&amp;lt; &amp;quot;data size: &amp;quot; &amp;lt;&amp;lt; num_rx_samps &amp;lt;&amp;lt; &amp;quot; first: &amp;quot; &amp;lt;&amp;lt; (&amp;amp;buffer.front())&#x5B;0] &amp;lt;&amp;lt; endl;\r\n    }\r\n\r\n    \/\/ Will never get here\r\n    stream_cmd.stream_mode = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS;\r\n    rx_stream-&amp;gt;issue_stream_cmd(stream_cmd);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The code below streams data using the UHD at 1MSPS. It just prints the first 2 datapoints to the screen but can be used as the basis for other stuff. It&#8217;s also worth checking out rx_samples_to_file.cpp in the UHD source, but this example removes a lot of the cruft. Can be compiled with: g++ rx.cpp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1466","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-nE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1466"}],"version-history":[{"count":3,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1466\/revisions"}],"predecessor-version":[{"id":1964,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1466\/revisions\/1964"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}