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

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 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch