Search code examples
c++pythongnuradio

C++ and python : TypeError resolution for vector arguments ?.


I am currently working on a piece of python code integrated with c++ on an open source platform. Python classes and definitions pass arguments to c++ files and the function runs thereby. My c++ file is a packet_sink file which takes 3 input arguments that are mentioned below :

packet_sink (const std::vector<unsigned char>& sync_vector,
                                msg_queue_sptr target_queue, int threshold)

The partial piece of python code is as follows :

   self.threshold = -1
   if access_code is None:
       code = tuple(string_to_1_0_list(default_access_code))
       access_code = to_1_0_string(code)
   if not packet_utils.is_1_0_string(access_code):
       raise ValueError, "Invalid access_code %r. Must be string of 1's and 0's" % (access_code,)
   print " _access_code : %s " %(self._access_code,)

   self._msgq = gr.msg_queue()# holds packets fromthe PHY

   self.phy_anay_demod= phy_anay_demod(self,*args, **kwargs)
   self._packet_sink =gr.packet_sink(self._access_code,self._msgq,self.threshold)

The value of _access_code is:

0011010110111011001001010100011101001111001100010000010000111111

But the error message that i am receiving is as follows :

TypeError: in method 'packet_sink', argument 1 of type 'std::vector< unsigned char,std::allocator< unsigned char > > const &'.

Could anybody please help me understand the error if not a resolution to the problem.


Solution

  • It looks to me like self._access_code should be a list or tuple of integers which are either 0 or 1.

    Gnuradio uses SWIG to create the python interface from the C++ code. SWIG will map a C++ vector to a python list. So if you pass in a list of integers (0 or 1) this should happily be converted to vector.

    My guess is that your self._access_code is a string and you need to apply the function string_to_1_0_list to it.