Author Topic: Exception : Safe Handle Has Been Closed ?  (Read 20970 times)

dtrewren

  • Member
  • ***
  • Posts: 4
Exception : Safe Handle Has Been Closed ?
« on: October 12, 2011, 06:23:26 am »
Hi Group,

I am hoping for a little help.  I has design a PIC4550 based telescope focuser using USB for control and a C# ASCOM compliant driver.  The driver sends simple move commands to the PIC which in tern controls a stepper motor ... you get the idea.  Control from the PC to the PIC works perfectly.  In the other direction I have a problem.  I want to the PC to poll the USB bus for communication from the PIC indicting the motor has completed it's move.  If I get the PIC to echo the USB command sent immediately the PC receives the echo and outputs to a debug file - all good.  But if I get the PC to perform a read from the USB in a loop waiting for the returned 'motor stop command' I get an exception as follows :
EXCEPTION : Safe handle has been closed
MODULE : Hid
Method : Readfile

I have no idea what is causing this exception, not sure if it is the PIC or PC.  I have searched on-line and can't really find and concrete explanation.  Just knowing what is causing the exception would be a start.

If I issue one USB read at the PC all is fine as it receives the echoed message.  But if I issue two reads and the second does not have a message to read it seems to cause the exception, as if something is waiting for a message and then times out ?  I was expecting the PC USB read to return 'false' ie nothing to read and the method then to be ready to be called again.

Hope someone can help .... really want to get this uplink going :)

Cheers,

                   Dave

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Exception : Safe Handle Has Been Closed ?
« Reply #1 on: October 12, 2011, 10:07:41 am »
Put a breakpoint at ReadFile and step through the code to see what happens.

Jan

dtrewren

  • Member
  • ***
  • Posts: 4
Re: Exception : Safe Handle Has Been Closed ?
« Reply #2 on: October 12, 2011, 05:36:58 pm »
Hi Jan,

Thanks for getting back.  You are going to have to pardon my lack of hard core softie knowledge (i'm a chip designer, VHDL). The code is a driver and so has no 'main' entry point.  I have struggled with debug is it's just a collection of methods called via the ASCOM platform API, hope that terminology is ok.  I have been putting file output into the driver to see what is going on but not sure how to debug in the traditional manner, ie breakpoints and stepping etc ?

Do you think the problem is caused by the PC or something the PIC is TXing the PC doesn't like ?

Cheers,

               Dave

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Exception : Safe Handle Has Been Closed ?
« Reply #3 on: October 12, 2011, 09:34:03 pm »
It's possible the driver responds to a lack of expected data or lack of any response from the PIC by closing the handle.

A USB protocol analyzer will tell you if the host and device are sending the expected data. See:

http://www.lvr.com/development_tools.htm#analyzers

If you don't have access to the driver's source code, whoever provided the driver might have suggestions for debugging communications.

Jan

dtrewren

  • Member
  • ***
  • Posts: 4
Re: Exception : Safe Handle Has Been Closed ?
« Reply #4 on: October 13, 2011, 05:01:18 am »
Hi Jan,

Thanks for the link.  I have got access to the source code as I wrote the driver in the first place.

I have done some more digging around and I think the problem maybe that the USB receive timeout is not being set at the PC end of the link.  So when I issue the first receive method call all is ok as there is a message to read but the second and subsequent reads don't necessarily have a message to read as I'm polling. So without a suitable timeout set an exception is finally generated.

You mention this on the web-site, quoting from site :

"Why does ReadFile hang my application when I request to read a report?
ReadFile returns when one of more reports of the expected size are available or on a timeout if implemented"

Does that sound plausible and if so how do I go about setting up a sensible timeout in the PC USB stack ?

Cheers,

              Dave

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Exception : Safe Handle Has Been Closed ?
« Reply #5 on: October 13, 2011, 10:46:35 am »
Use overlapped ReadFile with a timeout. See my HID code for an example:

http://www.lvr.com/hidpage.htm#MyExampleCode

However, a lack of a report to read with a non-overlapped call to ReadFile doesn't cause an exception. It just hangs ReadFile forever.

To debug your driver, see:

http://msdn.microsoft.com/en-us/windows/hardware/gg463009

Jan

dtrewren

  • Member
  • ***
  • Posts: 4
Re: Exception : Safe Handle Has Been Closed ?
« Reply #6 on: October 13, 2011, 11:17:39 am »
Hi Jan,

Thanks for the link to your code example.  I can see where the overlapped ReadFile is set up.  Where is the non zero timeout configured ?

Cheers,

             Dave

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Exception : Safe Handle Has Been Closed ?
« Reply #7 on: October 13, 2011, 11:46:40 am »
See WaitForSingleObject

Or search the code for "timeout"

Jan