您好,我在使用C#调用WCHBLEDLL库,调用打开设备接口是得不到响应,MFC Demo可以成功。在此寻求帮助,谢谢
DLL 调用:
/// /// 连接状态委托 /// /// 设备句柄 /// 连接状态,0-断开、1-连接 [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate void FunDevConnChangeCallBack(IntPtr hDev, byte ConnectStatus); /// /// 打开设备 /// /// 设备路径,可通过扫描设备获取 /// 上传设备状态的回调函数 /// [DllImport("WCHBLEDLL.dll", EntryPoint = "WCHBLEOpenDevice", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] public static extern IntPtr WCHBLEOpenDevice(string DevID, FunDevConnChangeCallBack pFunDevConnChange);
逻辑代码:
public void BLEConnect(IntPtr hDev, byte connectState) { retInptr = hDev; string hDevStr, stateStr; hDevStr = Marshal.PtrToStringAnsi(hDev); stateStr = connectState.ToString(); Console.WriteLine("设备句柄:" + Marshal.PtrToStringAnsi(hDev)); Console.WriteLine("连接状态:" + connectState); if (stateStr == "1") MessageBox.Show("设备句柄:" + hDevStr + ",连接状态:" + connectState); else MessageBox.Show("设备句柄:" + hDevStr + ",断开状态:" + connectState); return; } [ThreadStatic] private static BLE.FunDevConnChangeCallBack ConnChangeCallBack; /// /// 设备句柄 /// IntPtr retInptr; //连接蓝牙 private void button3_Click(object sender, EventArgs e) { retInptr = Marshal.AllocHGlobal(100); try { //设备路径,可通过扫描设备获取 string devInfo = listBox1.SelectedItem.ToString().Split(',')[1]; ConnChangeCallBack = new BLE.FunDevConnChangeCallBack(BLEConnect); retInptr = BLE.WCHBLEOpenDevice(devInfo, ConnChangeCallBack); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
不知道是否有C# Demo,或者可以给予帮助,谢谢
热门产品 :
CH32V317: 互联型青稞RISC-V MCU