简体   繁体   中英

IOException I can’t catch

I have an application talking to a USB-GPS. It's working as a charm if nothing out of the ordinary happnes. But I have a big problem. If the USB gets pulled out, my program (some times) crashes. I have Try/Catch where I need them but this IOExeption doesn't get caught. I just get "The device does not recognize the command" and the program stops. Here is the code that starts the port:

        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);
        }
    }

Then I have this in a Timer to check the info

if (this.LatLong.newDataReceived())
   {
        //DOING STUFF
   }

if (!this.LatLong.checkIfPortsOpen())
       this.LatLong.openPort();

Anyone have any suggestions how to stop the crashes?

[EDIT] The stack:

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()

I'm not entirely sure if it applies here, but there are mechanisms to catch overall crashes at the appdomain level - http://msdn.microsoft.com/en-GB/library/system.appdomain.unhandledexception.aspx

(not the section on other events, eg ThreadException - these may need their own handlers depending on the situation)

Although not a best practice, top-level exception handling might solve your problem. See http://richnewman.wordpress.com/2007/04/07/top-level-exception-handling-in-windows-forms-applications-%E2%80%93-code-listing-1/ for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM