Page 1 of 2

Re: C# and BladeRF

Posted: Wed Jul 01, 2015 4:23 am
by dave0160
on4bhm, just sent you a PM to answer your question...

The code above should work for you, but you'll have to modify to meet your requirements to sen the data over tcpip. I'm not familiar with the websdr data format.

The example just save the samples in a text based csv file, but you can use the "BinaryWriter" class in C# to write the samples in binary format which will make you files sizes more manageable over tcpip. I've switched to binary format since my recordings are around 10s @ 5MHz sample rate => 200MB per file.

Dave

Re: C# and BladeRF

Posted: Sat Jun 18, 2016 8:03 pm
by ksmiller99
Thanks dave160 I'm just getting started on a C# bladeRF project. I used your code and because the SDR# repo is closed, I found NativeMethods.cs elsewhere (I'd prefer not to say where because there're some copyright issues involved) and it is running! Did you do anything with transmitting yet?

Kevin

Re: C# and BladeRF

Posted: Sun Jun 19, 2016 2:12 am
by jump
If you took it from my repository (https://github.com/jmichelp/sdrsharp-bladerf), there's no copyright issue at all.

I think I've never found what kind of license is compatible with SDRSharp now that they are closed-source and then I was focused on development and forgot to set one.
You can safely assume it's a BSD license :) I will add corresponding headers and files on the next release.

Re: C# and BladeRF

Posted: Wed Aug 10, 2016 10:38 am
by ksmiller99
Thanks jump. That is the NativeMethods.cs I have been using, and the project is coming along nicely, but there's one strange problem. When I try to get the serial number using bladerf_get_serial(), the application just crashes and disappears with no messages. I'm running a debug build from MSVS 2013. Have you seen this before, or do you have any ideas on how to troubleshoot it?

Re: C# and BladeRF

Posted: Wed Aug 10, 2016 1:01 pm
by jump
Have looked at the way I use it in my code in order to compare with your code?
https://github.com/jmichelp/sdrsharp-bl ... ce.cs#L321

When it crashes, it basically means that something wrong happened in the native code.
This can happen in 2 cases I think:
  • Passing wrong arguments to a native call
  • Non-initialized buffers / null objects when the API is expected to write into the memory
If you're using similar code to what I did, that could mean that I got lucky with my code and that the wrapper has an issue.
No black magic to debug/trace that as far as I can tell: just use Visual Studio and its amazing debugger to run your code in debug mode and look at the crash or step into your code.

As a disclaimer, I made this wrapper to make my life easier and tried to wrap as many functions from libbladerf as I can but I never claimed it was safe to use it or that it was bug free ;)
I'm not an expert in C# by the way so I could have made big mistakes here. Exchanging data from/to native and managed code is sometimes a bit tricky.

Re: C# and BladeRF

Posted: Thu Aug 11, 2016 7:28 am
by ksmiller99
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);
        }

Re: C# and BladeRF

Posted: Thu Aug 11, 2016 10:03 am
by jynik
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).

Re: C# and BladeRF

Posted: Thu Aug 11, 2016 12:26 pm
by ksmiller99
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);
}