我创建了一个通信模块,其中一个模块的职责是检测发送某个签名的端口,我通过设置一个识别器委托从OnDataRecive为所有端口调用来实现,当我等待一段时间。之后,我从所有端口删除识别器代理,关闭并处理不相关的端口,相关端口接收替换代表,以便在收到的数据上调用。
public override bool fSearchPortsForSilDevices(dPortChangedHandler p_hPortAdded, dPortChangedHandler p_hPortRemoved, cSilPort.dDataReceivedHandler p_hSilDevRecognaizer){
string[] l_strPortNames = SerialPort.GetPortNames();
cSilUARTport l_oUART_Port = null;
bool l_isSilDevFound = false;
try
{
foreach (cSilUARTport l_oPort in m_oPortsDictionary.Values)
{
l_oPort.fClose();
}
m_oPortsDictionary.Clear();//AmitTODO: figure out if clear also destroy the instances, so the serial ports will be closed.
foreach (var l_strPortName in l_strPortNames)
{
l_oUART_Port = new cSilUARTport(l_strPortName);
m_oPortsDictionary.Add(l_strPortName, l_oUART_Port);
l_oUART_Port.DataReceived = p_hSilDevRecognaizer;
}
Thread.Sleep(100);//Amit: we must wait and allow each port to send a realtime message.
foreach (var l_strPortName in l_strPortNames)//Amit: close all none Sil related ports and remove them from the dictionary.
{
if (m_oPortsDictionary.TryGetValue(l_strPortName, out l_oUART_Port))
{
if (!l_oUART_Port.SilDev)
{
m_oPortsDictionary.Remove(l_strPortName);
p_hPortRemoved(l_oUART_Port);
l_oUART_Port.fClose();
}
else
{
p_hPortAdded(l_oUART_Port);//AmitTODO: will detecte which device is it, and will set a a reading function for it.
l_isSilDevFound = true;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
return l_isSilDevFound;
}
如果是通讯下拉,我再次调用相同的功能。
测试后:插入和拔出我收到的USB: ObjectDisposedException:安全句柄已关闭
我设法捕获异常并打印堆栈跟踪,但没有追溯到我的代码()
SilUARTPort:
public class cSilUARTport : cSilPort
{
#region Members
private SerialPort m_oSerPort = null;
private bool m_isSilDev = false;
#endregion
#region CTOR/DTOR
private cSilUARTport() { }
public cSilUARTport(string p_strPortName){
try{
m_strPortType = "UART";
m_oSerPort = new SerialPort(p_strPortName);
if (m_oSerPort != null && m_oSerPort.IsOpen == false)
fOpen();
}
catch
{}
}
~cSilUARTport(){
try{
if (m_oSerPort.IsOpen == true) fClose();
}
catch
{ }
}
#endregion
#region Accessories
public bool SilDev
{
get { return m_isSilDev; }
set { m_isSilDev = value; }
}
public string PortName
{
get { return m_oSerPort.PortName; }
set { m_oSerPort.PortName = value; }
}
#endregion
#region Methods
public override void fOpen()
{
try
{
m_oSerPort.BaudRate = cSilConstants.Com.UARTport.c_iBaudRate;
m_oSerPort.DataBits = 8;
m_oSerPort.StopBits = StopBits.One;
m_oSerPort.Parity = Parity.None;
m_oSerPort.Handshake = Handshake.None;
m_oSerPort.ReadTimeout = cSilConstants.Com.UARTport.c_iTimeOut;
m_oSerPort.WriteTimeout = cSilConstants.Com.UARTport.c_iTimeOut;
m_oSerPort.DataReceived += fDataReceived;
m_oSerPort.ReadTimeout = 100;
m_oSerPort.WriteTimeout = 100;
m_oSerPort.Open();
}
catch
{}
}
public override void fClose(){
try{
m_oSerPort.Close();
m_oSerPort.Dispose();
}
catch
{}
}
public override void fWrite(byte[] p_bData){
m_oSerPort.Write(p_bData, 0, p_bData.Count());
}
#endregion
#region Handlers
private void fDataReceived(object sender, SerialDataReceivedEventArgs e){
try{
byte[] l_Data = Encoding.ASCII.GetBytes(m_oSerPort.ReadExisting());
if (m_hDataReceived(l_Data) == 1) m_isSilDev = true;
}
catch
{}
}
#endregion
#region Assist Functions
public override bool Equals(object obj){
var other = obj as cSilPort;
if (other == null) return false;
return Equals(other);
}
public override bool Equals(cSilPort obj){
var other = obj as cSilUARTport;
return (m_oSerPort.PortName == other.m_oSerPort.PortName);
}
#endregion
}
有任何想法或建议吗? 提前谢谢!