C# and BladeRF

Discussions related to embedded firmware, driver, and user mode application software development

Moderator: robert.ghilduta

ksmiller99
Posts: 10
Joined: Wed Jun 15, 2016 11:22 am

Re: C# and BladeRF

Post by ksmiller99 » Thu Aug 11, 2016 7:28 am

Thanks again jump. My code was pretty close to yours, but as a test I made the following method that is nearly identical to yours and is still crashing out of the debugger with an access violation. Hopefully you or someone else will see something obvious. The rest of my code works well - I'm able to configure the bladeRF and capture signals to a file that is processed off-line.

Code: Select all

public static void serNumTest()
        {
            IntPtr _dev;
            string sdrspec = "";
            string serial = "";
            
            var rv = NativeMethods.bladerf_open(out _dev, sdrspec);
            if (rv != 0)
                throw new ApplicationException(String.Format("Cannot open BladeRF device. Is the device locked somewhere?. {0}", NativeMethods.bladerf_strerror(rv)));

            if ((rv = NativeMethods.bladerf_get_serial(_dev, out serial)) != 0)
                throw new ApplicationException(String.Format("bladerf_get_serial() error. {0}", NativeMethods.bladerf_strerror(rv)));

             // above instruction crashes with following output:
             // A first chance exception of type 'System.AccessViolationException' occurred in mscorlib.dll
             // 'cOOKie.vshost.exe' (CLR v4.0.30319: cOOKie.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
             //  The program '[8572] cOOKie.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
            
            BrfNativeMethods.bladerf_close(_dev);
        }

jynik
Posts: 455
Joined: Thu Jun 06, 2013 8:15 pm

Re: C# and BladeRF

Post by jynik » Thu Aug 11, 2016 10:03 am

In the C API, the bladerf_get_serial() function expects that you're going to provide it an adequately sized (at least 33 bytes) buffer for the serial number string.

I'm not sure how things work with C-style strings and C#, but if you're calling that native method, you'll need to ensure you've got an adequately large destination buffer. Right now, I'm guessing this is not the case.

As the guy that wrote it, let me be the first to say that this was a poorly designed function, in retrospect. If/when there's a libbladeRF 2.x.x series, this function will be replaced with something that takes a pointer to a struct containing a fixed-length buffer (per issue 382).

ksmiller99
Posts: 10
Joined: Wed Jun 15, 2016 11:22 am

Re: C# and BladeRF

Post by ksmiller99 » Thu Aug 11, 2016 12:26 pm

Thanks to both jynik and jump as well as StackOverflow I got it.
See https://stackoverflow.com/questions/113 ... l-c-sharp# at StackOverflow.

Change import in NativeMethods.cs:

Code: Select all

[DllImport("bladeRF", EntryPoint = "bladerf_get_serial", CallingConvention = CallingConvention.Cdecl)]
        public static extern int bladerf_get_serial(IntPtr dev, StringBuilder serial);
This works:

Code: Select all

public static void serNumTest()
{
    IntPtr _dev;
    string sdrspec = "";
    StringBuilder serialSB = new StringBuilder(33);
    
    
    var rv = NativeMethods.bladerf_open(out _dev, sdrspec);
    if (rv != 0)
        throw new ApplicationException(String.Format("Cannot open BladeRF device. Is the device locked somewhere?. {0}", NativeMethods.bladerf_strerror(rv)));

    if ((rv = NativeMethods.bladerf_get_serial(_dev, serialSB)) != 0)
        throw new ApplicationException(String.Format("bladerf_get_serial() error. {0}", NativeMethods.bladerf_strerror(rv)));

string serial = serialSB.ToString();
    NativeMethods.bladerf_close(_dev);
}

Post Reply