AI科技分享平台 2015-05-18 06:05 采纳率: 0%
浏览 4614

C# 使用LibusbDotNet实现usb热插拔遇到的异常

我想实现USB连接后,数据通信过程中插拔USB,软件自动识别USB后,能够自动连接并继续发送数据,软件不死机。
但是,往往插拔几次后会出现“ Safe handle has been closed Exception”异常

下面是识别USB的代码
我试了下,先关闭USB设备后关闭发送线程标志位,和先关闭发送线程标志位后关闭USB设备都会出现这种问题,
下面是打印的调试信息,通过调试信息发现,在识别到USB断开之前,发送线程依然会执行write函数发送数据,结果打印出发送0字节错误 Win32Error,不知道是不是由于发送错误后引发异常?
线?程¨¬ '<无T名?称?>' (0x156c) 已°?退ª?出?,ê?返¤¦Ì回?值¦Ì为a 0 (0x0)。¡ê
发送12 Success
now time is 11.6512
发送12 Success
now time is 1.1498
发送12 Success
now time is 0.4251
发送0 Win32Error
警告
警告
now time is 501.0047
发送 Win32Error
警告
断开
断开完毕
警告
now time is 499.6215
close thread

    public void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
    {
        if (e.EventType == EventType.DeviceArrival)
        {
            if (null == e.Device)
                return;
            if (e.Device.IdProduct == myProductID && e.Device.IdVendor == myVendorID)
            {
                Console.WriteLine("连¢?接¨®");
                FindTheHid(m_wnd);
                if (myDeviceDetected == true)
                {
                    OpenTheHid(m_wnd);
                    onDataSend();           //数ºy据Y发¤¡é送¨ª线?程¨¬                   
                    //onReceive();            //开a启?接¨®收º?数ºy据Y线?程¨¬
                    //onDataProcess();        //开a启?数ºy据Y处ä|理¤¨ª线?程¨¬
                    //m_dataSendThread.Resume();
                    close = true;
                    IsSendThreadRun = true;
                    Thread.Sleep(200);
                    IsRecvThreadRun = true;
                    IsProcessThreadRun = true; 
                    Thread.Sleep(200);
                    HandShakeAsk(m_wnd);  //握?手º?指?令¢?   //2011.12.7
                    Console.WriteLine("握?手º?完ª¨º毕À?");
                }

            }
        }
        else if (e.EventType == EventType.DeviceRemoveComplete)
        {
            if (null == e.Device)
                return;
            if (e.Device.IdProduct == myProductID && e.Device.IdVendor == myVendorID)
            {
                Console.WriteLine("断开");                        
                    Reader.DataReceived -= (OnRxEndPointData);
                    Reader.DataReceivedEnabled = false;
                    CloseUSB();
                    close = false;
                    closesendthread = true;
                    onShutDown();
                    myDeviceDetected = false;
                    IsHandShakeBack = false;   //新?添¬¨ª加¨®   
                    Console.WriteLine("断开完毕");


            }
        }
    } 

下面是发送线程
private void DataSendProc()
{
int success = 0;
Byte[] SendBuffer = new Byte[12];
Byte i;
//Stopwatch stoptime = new Stopwatch();
while (true)
{

if (IsSendThreadRun)
{
if (Send_RJS != Send_Done)
{
Stopwatch stoptime = new Stopwatch();
stoptime.Start();
if ((myDeviceDetected == true))
{
for (i = 0; i < 12; i++)
{
SendBuffer[i] = SendDataBuffer[Send_Done * 12 + i];
}
success = 0;
success = Write(SendBuffer, 50, 12);
if (success == 12)
{
if (Send_Done * 12 >= 43200)
Send_Done = 0;
else
Send_Done++;
}
else
{
Console.WriteLine("警¡¥告?1");
Thread.Sleep(500);
Console.WriteLine("警¡¥告?");
}
}
else
{
System.Windows.MessageBox.Show("Invalid handle. The device is probably a system mouse or keyboard.", "Alert");
System.Windows.MessageBox.Show("No attempt to write an Output report was made.", "Alert");
}
stoptime.Stop();
TimeSpan ts = stoptime.Elapsed;
Console.WriteLine("now time is {0}",ts.TotalMilliseconds);
}
}
if (closesendthread == true) {
closesendthread = false;
Console.WriteLine("close thread");
return;
}

            Thread.Sleep(10); //静2态¬?方¤?法¤¡§,ê?暂Y停ª¡ê当Ì¡À前¡ã线?程¨¬5毫¨¢秒?;ê?
        }
  • 写回答

1条回答 默认 最新

  • yedushu 2016-03-04 15:40
    关注

    LZ libusbdotnet 的HID实现吗

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊