SerialPort对象和FTDI [英] SerialPort object and FTDI

查看:109
本文介绍了SerialPort对象和FTDI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序是否可以通过FTDI USB到串行转换器芯片在.net 2.0中使用SerialPort对象,并在断开设备连接的情况下正常生存?当尝试从设备读取失败时,我的线程退出并尝试关闭SerialPort对象.关闭失败并显示访问被拒绝"异常,但这是可以生存的.不幸的是,该程序后来在我无法控制的某个线程中崩溃,并显示安全句柄已关闭" ObjectDisposedException. Google搜索发现该问题已得到Microsoft支持,但他们似乎并不认为这是一个问题.有解决方案吗?

Is there any way for an application to use the SerialPort object in .net 2.0 with FTDI USB-to-serial converter chips and survive gracefully when the device is disconnected? When an attempt to read from the device fails, my thread exits and tries to close the SerialPort object. The close fails with an Access Denied exception, but that''s survivable. Unfortunately, the program later crashes with a "Safe Handle was Closed" ObjectDisposedException in some thread I don''t control. A Google search found that the issue has been raised to Microsoft support, but they don''t seem to think it''s a problem. Is there a solution?

推荐答案

我们也使用FTDI驱动程序,并且存在相同的问题.不幸的是,您对此无能为力.一种策略是在不使用端口时将其关闭.我们还指示用户切勿在程序运行期间卸下设备,除非得到指示.

您可能会受益于使用可检测USB连接/断开事件的对象. WMI为此提供了功能.我使用它来确保在连接后不立即尝试连接到FTDI设备.您可以调整代码以在断开连接时触发事件,这可能会允许更正常的关闭.请参见下面的代码示例以获取想法:

We also use the FTDI driver and have the same issue. Unfortunately, there isn''t much you can do about it. One strategy is to close the port whenever it''s not in use. We also instruct our users to never remove the device during program operation unless instructed to do so.

You may benefit from using an object that detects USB connect/disconnect events. WMI provides functionality for this. I use it to make sure I don''t try to connect to the FTDI device immediatley after is has been connected. You could tweak the code to fire events on disconnect, which will possibly allow a more graceful close. See the code example below for ideas:

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace DiagnosticControllerComm
{
    class USBInsertRemoveDetect : IDisposable
    {
        public delegate void EventArrivedDelegate(object sender, EventArrivedEventArgs e);
        /// <summary>
        /// A disconnect or connect event was detected
        /// </summary>
        public event EventArrivedDelegate EventArrived;
        ManagementEventWatcher m_watcher = null;
        
        public void Start()
        {             
  
            WqlEventQuery query;               
                         
            // Bind to local machine 
              
            ManagementScope scope = new ManagementScope("root\\CIMV2"); 
              
            scope.Options.EnablePrivileges = true; //sets required privilege    
            query = new WqlEventQuery();                
            query.EventClassName = "__InstanceOperationEvent";                
            query.WithinInterval = new TimeSpan(0,0,0,0,500);
            query.Condition = @"TargetInstance ISA ''Win32_USBControllerDevice''"; 
            
            m_watcher = new ManagementEventWatcher(scope, query);                
            m_watcher.EventArrived += new EventArrivedEventHandler(UsbEventArrived);                
            m_watcher.Start();
            
  
        }
        
       
  
        public void UsbEventArrived(object sender, EventArrivedEventArgs e)            
        {
            if (EventArrived != null)
            {
                EventArrived(sender, e);
            }
          
            //Get the Event object and display it  
              
            //foreach(PropertyData pd in e.NewEvent.Properties)                
            //{  
              
            //    ManagementBaseObject mbo = null;  
                  
            //    if(( mbo = pd.Value as ManagementBaseObject) != null)
            //    {  
                  
            //        foreach(PropertyData prop in mbo.Properties)  
                      
            //        Console.WriteLine("{0} - {1}", prop.Name, prop.Value);  
                  
            //    }  
              
            //}  
  
        }

        #region IDisposable Members

        public void Dispose()
        {
            
            m_watcher.Dispose();
        }

        #endregion
    }
}


这篇关于SerialPort对象和FTDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆