SDRPlay APIのasrplay_api_GetDevices関数がC#からようやく使えるようになった。
C#では、固定長の配列やポインタが使えず、結局、unsafeコードを使った。 もう少しスマートなやりかたがあるかもしれないけど、今はこれが限界。
実行結果
ソース
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using System; using System.Text; using System.Windows.Forms; namespace SDRPlay_API_Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { float apiVer = 0f; sdrplay_api_DeviceT devices; uint numdevs = 0; uint maxDevs = 1; sdrplay_api_Open(); sdrplay_api_ApiVersion(ref apiVer); var rr = sdrplay_api_GetDevices(out devices, ref numdevs, maxDevs); sdrplay_api_Close(); byte[] res = new byte[64]; unsafe { for(int i = 0; i < 64; i++) res[i] = *(devices.SerNo+i); } string s = Encoding.ASCII.GetString(res).Substring(0,10); textBox1.Text = "API Ver. = " + apiVer.ToString() + "\r\n" +"Serial No. = "+ s + "\r\n" + "Hardware Ver. = " + devices.hwVer.ToString() + "\r\n" + "Tuner = " + devices.tuner.ToString() + "\r\n" + "rspDuoMode = " + devices.rspDuoMode.ToString() + "\r\n" + "Valid = " + devices.valid.ToString() + "\r\n" + "rspDuoSampleFreq = " + devices.rspDuoSampleFreq.ToString() + "\r\n" + "dev = 0x" + devices.dev.ToString("x"); textBox1.Refresh(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
using System; using System.Runtime.InteropServices; namespace SDRPlay_API_Test { partial class Form1 { public static readonly int SDRPLAY_MAX_SER_NO_LEN = 64; public enum sdrplay_api_ErrT { sdrplay_api_Success = 0, sdrplay_api_Fail = 1, sdrplay_api_InvalidParam = 2, sdrplay_api_OutOfRange = 3, sdrplay_api_GainUpdateError = 4, sdrplay_api_RfUpdateError = 5, sdrplay_api_FsUpdateError = 6, sdrplay_api_HwError = 7, sdrplay_api_AliasingError = 8, sdrplay_api_AlreadyInitialised = 9, sdrplay_api_NotInitialised = 10, sdrplay_api_NotEnabled = 11, sdrplay_api_HwVerError = 12, sdrplay_api_OutOfMemError = 13, sdrplay_api_ServiceNotResponding = 14, sdrplay_api_StartPending = 15, sdrplay_api_StopPending = 16, sdrplay_api_InvalidMode = 17, sdrplay_api_FailedVerification1 = 18, sdrplay_api_FailedVerification2 = 19, sdrplay_api_FailedVerification3 = 20, sdrplay_api_FailedVerification4 = 21, sdrplay_api_FailedVerification5 = 22, sdrplay_api_FailedVerification6 = 23, sdrplay_api_InvalidServiceVersion = 24 } public enum sdrplay_api_TunerSelectT { sdrplay_api_Tuner_Neither = 0, sdrplay_api_Tuner_A = 1, sdrplay_api_Tuner_B = 2, sdrplay_api_Tuner_Both = 3 } public enum sdrplay_api_RspDuoModeT { sdrplay_api_RspDuoMode_Unknown = 0, sdrplay_api_RspDuoMode_Single_Tuner = 1, sdrplay_api_RspDuoMode_Dual_Tuner = 2, sdrplay_api_RspDuoMode_Master = 4, sdrplay_api_RspDuoMode_Slave = 8 } [StructLayout(LayoutKind.Sequential)] unsafe public struct sdrplay_api_DeviceT { public fixed byte SerNo[64]; // Set by the API on return from sdrplay_api_GetDevices() contains the serial number of the device. public byte hwVer; // Set by the API on return from sdrplay_api_GetDevices() contains the Hardware version of the device. public sdrplay_api_TunerSelectT tuner; // Set by the API on return from sdrplay_api_GetDevices() indicating which tuners are available. // Set by the application and used during sdrplay_api_SelectDevice() to indicate which tuner(s) is to be used. public sdrplay_api_RspDuoModeT rspDuoMode; // Set by the API on return from sdrplay_api_GetDevices() for RSPduo devices indicating which modes are available. // Set by the application and used during sdrplay_api_SelectDevice() for RSPduo device to indicate which mode is to be used. public byte valid; // parameter to indicate device is ready to use public double rspDuoSampleFreq; // Set by the API on return from sdrplay_api_GetDevices() for RSPduo slaves indicating the sample rate previously set by the master. // Set by the application and used during sdrplay_api_SelectDevice() by RSPduo masters to indicate required sample rate. public IntPtr dev; // Set by the API on return from sdrplay_api_SelectDevice() for use in subsequent calls to the API. Do not alter! } [DllImport("sdrplay_api.dll", EntryPoint = "sdrplay_api_Open", CallingConvention = CallingConvention.Cdecl)] private static extern sdrplay_api_ErrT sdrplay_api_Open(); [DllImport("sdrplay_api.dll", EntryPoint = "sdrplay_api_Close", CallingConvention = CallingConvention.Cdecl)] private static extern sdrplay_api_ErrT sdrplay_api_Close(); [DllImport("sdrplay_api.dll", EntryPoint = "sdrplay_api_ApiVersion", CallingConvention = CallingConvention.Cdecl)] private static extern sdrplay_api_ErrT sdrplay_api_ApiVersion(ref float apiVer); [DllImport("sdrplay_api.dll", EntryPoint = "sdrplay_api_GetDevices", CallingConvention = CallingConvention.Cdecl)] private static extern sdrplay_api_ErrT sdrplay_api_GetDevices(out sdrplay_api_DeviceT devices, ref uint numdevs, uint maxDevs); } } |
ちなみに, dllは64bitバージョンを使用している。