Gnu_radio,工作循环和停止

时间:2017-06-14 19:13:06

标签: c++ gnuradio

我开始使用gnuradio,

我正在尝试创建自定义块。我的目标是从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 */

1 个答案:

答案 0 :(得分:1)

您的代码中有许多错误。这很困惑。

  • 您使用c作为文件中的字符数。然后将其解释为带有toupper[c]的字符,并忽略刚刚读过的十六进制字符。
  • 您的十六进制转换switch语句没有break,因此始终会生成conv = 15
  • 您的工作函数必须写不超过 noutput_items,并保留其余部分以供日后使用。相反,它正在编写任意数量的内容,可能会溢出提供的缓冲区。

鉴于此处的混乱,我强烈建议您首先处理较小的问题并更好地掌握语言。只需打印结果,即可使用十六进制转换器。创建一个无法读取文件的源块。

另外,打开你的C ++编译器的警告并听取它们。它可能会告诉你你的switch语句坏了。

相关问题