我有一個應用程序與USB-GPS通話。如果沒有什麼不尋常的事情,它就會變成一種魅力。但是我有一個很大的問題。如果USB被拔出,我的程序(有時)會崩潰。我有Try/Catch在哪裏需要它們,但是這個IOExeption不會被捕獲。我只是得到「設備無法識別命令」,程序停止。下面是啓動該端口的代碼:IOException我無法捕捉到
public LatLongFromGPS(Form1 parent)
{
this.parent = parent;
String port;
this.SPort = new SerialPort(port, 4800);
this.SPort.ReadTimeout = 500;
this.SPort.DataReceived += new SerialDataReceivedEventHandler(dataReceived);
}
public bool checkIfPortsOpen()
{
return (this.SPort.IsOpen);
}
public void openPort()
{
try
{
if (!this.SPort.IsOpen)
{
this.SPort.Open();
}
}
catch(Exception ex)
{
parent.LoggIt.WriteLogg("OPENPORT " + ex.ToString(), Logger.LoggType.Debug);
}
}
public void dataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (SPort.IsOpen)
{
String GPGGAString;
Thread.CurrentThread.Join(200);
buffert = new char[this.SPort.BytesToRead];
this.SPort.Read(buffert, 0, buffert.Length);
GPGGAString = findStringFromGPS();
if (GPGGAString != null)
{
getLatitudefromString(GPGGAString);
getLongitudefromString(GPGGAString);
getTimeFromString(GPGGAString);
this.newData = true;
}
}
}
catch(Exception ex)
{
parent.LoggIt.WriteLogg("GPSERROR " + ex.ToString(), Logger.LoggType.Debug);
}
}
然後,我有這樣的一個定時器來檢查信息
if (this.LatLong.newDataReceived())
{
//DOING STUFF
}
if (!this.LatLong.checkIfPortsOpen())
this.LatLong.openPort();
任何人有任何建議,如何停止崩潰?
[編輯]堆棧:
at System.IO.Ports.InternalResources.WinIOError(Int32, System.String)
at System.IO.Ports.InternalResources.WinIOError()
at System.IO.Ports.SerialStream.Dispose(Boolean)
at System.IO.Ports.SerialStream.Finalize()
這聽起來像是在工作線程上發生異常;屏幕上出現異常的'.StackTrace'是什麼? (可能是在一個醜陋的錯誤框中) – 2013-02-19 14:12:07
您是否錯誤地寫入'parent.LoggIt.WriteLogg(「GPSERROR」'line?log?「) – Artemix 2013-02-19 14:12:12
您是否在使用過之前設置過'port'? – 2013-02-19 14:17:09