c++ - Gnu_radio,工作循环和停止

标签 c++ gnuradio

我从 gnuradio 开始,

我正在尝试创建一个自定义 block 。我的目标是从 txt 文件中读取十六进制数据并以带有序言的二进制形式发送它们。

我尝试使用以下代码,但工作有时会在结束之前退出,有时会循环...... 它永远不会到达转换部分。我怀疑我的 noutput 有问题。

预先感谢您的所有帮助! :)

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/io_signature.h>
#include "acars_gen_impl.h"

namespace gr {
  namespace Acars_send {

    acars_gen::sptr
    acars_gen::make(char* file)
    {
      return gnuradio::get_initial_sptr
        (new acars_gen_impl(file));
    }

    /*
     * The private constructor
     */
    acars_gen_impl::acars_gen_impl(char* file)
      : gr::sync_block("acars_gen",
      gr::io_signature::make(0, 0, 0),
        gr::io_signature::make(1, 1, sizeof(char))),
        preamble_sync{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x2B,0x2A,0x16,0x16}
        /*preamble_sync_bit{11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,11111111,
      11111111,11111111,11111111,11111111,11111111,00101011,00101010,00010110,00010110}*/
{


  myfile.open(file);
  int ret = myfile.is_open();
  printf("Fichier ouvert %d\n",ret);
}
    /*
     * Our virtual destructor.
     */
    acars_gen_impl::~acars_gen_impl()
    {
      myfile.close();
      printf("Fichier fermé\n");
    }


    /* -------------
     *  W O R K
     -------------*/

    int
    acars_gen_impl::work (int noutput_items,
        gr_vector_const_void_star &input_items,
        gr_vector_void_star &output_items)
    {
      printf("Debut WORK\n");

      char *out = (char *) output_items[0];

      for(int i =0; i < noutput_items; i++){
      out[i] = 0;
      consume_each (noutput_items);
      }

    /*if (noutput_items<(162))
        return 0;*/
 //Compte du nombre de charactères du fichier

    char current_char;
    int c = 0;
    if (myfile.is_open()){

      printf("Fichier BIEN ouvert\n");
      while(myfile.get(current_char)){
        printf("%c\n",current_char);
        if(current_char == EOF)
          {
            break;
          }
          c++;
          printf("Taille : %d\n",c);

          }
        }

    myfile.clear();
    myfile.seekg(0, std::ios::beg);

    unsigned char message[c]={0};

    std::string line;
    getline (myfile,line);
    std::cout<<line;
    if (line.find("0x")==0) {
      printf("Trouvé!\n");

    int i = 0;

    for ( std::string::iterator it=(line.begin()+2); it!=line.end(); ++it){
        unsigned char hex = *it;
        unsigned char conv;
  printf("Conversion en cours %c\n",c);
        switch(toupper(c))
    {

        case '0': conv = 0;
        case '1': conv = 1;
        case '2': conv = 2;
        case '3': conv = 3;
        case '4': conv = 4;
        case '5': conv = 5;
        case '6': conv = 6;
        case '7': conv = 7;
        case '8': conv = 8;
        case '9': conv = 9;
        case 'A': conv = 10;
        case 'B': conv = 11;
        case 'C': conv = 12;
        case 'D': conv = 13;
        case 'E': conv = 14;
        case 'F': conv = 15;
    }


    message[i/2]= message[i/2] | (conv<< (4*(1-i%2)));
    ++i;

    }
  }

  for(int i=0 ; i < c ; i ++ ){
        std::cout << message[i];
  }

  memcpy(out,preamble_sync,20*sizeof(char));
  memcpy(out+20,message,c*sizeof(char));
  return noutput_items;
  return -1;

    }

  } /* namespace Acars_send */
} /* namespace gr */

最佳答案

您的代码有很多问题。很困惑。

  • 您使用 c 作为文件中的字符计数。然后,您将其解释为 toupper[c] 的字符,并忽略您刚刚读取的十六进制字符。
  • 您的十六进制转换 switch 语句没有 break,因此始终会产生 conv = 15
  • 您的工作函数必须写入不超过 noutput_items,并保存其余部分供以后使用。相反,它想写多少就写多少,可能会溢出提供的缓冲区。

鉴于这里的困惑,我强烈建议您首先解决较小的问题并更好地掌握该语言。让您的转换器从十六进制工作,只需打印结果即可。创建一个不读取文件的源 block 。

此外,打开 C++ 编译器的警告并聆听它们。它可能会告诉您 switch 语句已损坏。

关于c++ - Gnu_radio,工作循环和停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552907/

相关文章:

c++ - 使用 swig 时出错 : Syntax error in input(1)

python - gnuradio `ImportError undefined symbol`

file - GNU radio 文件接收器如何工作?

c++ - 无法在 C++ 中构建复杂结构

c++ - 使用从模板化基类继承的成员变量 (C++)

c++ - 为什么我应该在 C++ 中使用 typedef?

ubuntu-16.04 - GNU Radio Companion WX Instrumentation

c++ - 实际发送电子邮件时捕获

java - 是否有 Apache Jena 的 C/C++ 绑定(bind)?

linux - Gnu Radio 在 Ubuntu 单元测试 qa_ofdm_txrx 上编译失败