[IronPython] Serial Port Commn: unhandled ObjectDisposedException though exception handler provided

Michael Foord fuzzyman at voidspace.org.uk
Thu Nov 20 11:01:39 CET 2008


Sheetal.Pawar at lntinfotech.com wrote:
>
> That close is kept just to ensure that the open call does not fail 
> saying "port already open". It was just for testing purpose.
>
> Yes ReadTo is not an asynch API. The program execution is blocked at 
> ReadTo till the timeout occurs. But the problem occurs when I try to 
> abort the read operation in between before the timeout.
>

The ObjectDisposedException (which you still haven't posted a full 
traceback for) indicates that under the hood it is an async API.

> Yes I had put a print in the exception handler. The problem is I get 
> that print and get "Keyboard Interrupt" as the error message but still 
> I am getting the ObjectDisposedException.
>
> This is the message which i get when i try to print the error using
> except Exception, e:
>     print "error   :  " + e.message:
>
> Output:
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "C:\Program Files\IronPython 2.0 Beta4\test.py", line 21, in 
> C:\Program Files\IronPython 2.0 Beta4\test.py
> KeyboardInterrupt
> >>>

This shows that your exception handling block is being used. I guess the 
problem is that you are closing the port after interrupting it whilst 
there is still an active callback with data waiting to come in. When 
this happens it tries to use a disposed object.

I don't think interrupting the 'ReadTo' with a KeyboardInterrupt is a 
very good pattern - but I'm not sure what the correct pattern is for 
this case. Maybe experiment with some of the async APIs (there are 
examples on the MSDN docs) so that you can explicitly provide for an 
interruption if you need it.

Michael Foord


>
>
> Thanks & regards,
> Sheetal Pawar
> *
> *
>
>
> *Michael Foord <fuzzyman at voidspace.org.uk>*
> Sent by: users-bounces at lists.ironpython.com
>
> 11/19/2008 08:49 PM
> Please respond to
> Discussion of IronPython <users at lists.ironpython.com>
>
>
> 	
> To
> 	Discussion of IronPython <users at lists.ironpython.com>
> cc
> 	
> Subject
> 	Re: [IronPython] Serial Port Commn: unhandled ObjectDisposedException 
> though exception handler provided
>
>
>
> 	
>
>
>
>
>
> Well... I know nothing about these APIs and am guessing, but the
> traceback is in an AsyncCallback happening on another thread - so an
> exception handler in this thread isn't going to catch it.
>
> Why do you close the port before opening it?
>
> ReadTo doesn't look like an Async API [1]. You snipped the top line of
> the traceback which includes any mention of the line number where it was
> raised. Have you put a print in to confirm that your exception handling
> block is never entered?
>
> Michael
>
> http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.readto.aspx
>
> Sheetal.Pawar at lntinfotech.com wrote:
> >
> >
> > Hi,
> >
> > I am writing a serial port communication application using Iron
> > Python. Following is a sample code:
> >
> >          try:
> >
> >             BaudRate = 9600
> >             DataBits = 8          
> >          
> > #create a serial port instance for the desired port as specified
> >             serialPort = System.IO.Ports.SerialPort( "COM1")
> >          
> >             serialPort.BaudRate = BaudRate
> >
> >             serialPort.DataBits = DataBits
> >
> > #open the port
> >             serialPort.Close()
> >             serialPort.Open()
> >
> >             serialPort.ReadTimeout = 30000
> >
> >             returnValue = serialPort.ReadTo( "xx" )
> >         except:
> >             print "error"
> >             returnValue = ""
> >
> >         serialPort.Close()
> >         serialPort.Dispose()
> >
> >  
> >
> > Here if i abort my application using Cntrl + C on the iron python
> > console, then i get ObjectDisposedException unhandled though i have
> > provided an exception handler.
> >
> > I tried to abort the application while it was waiting for some data to
> > be read and the timeout had not occured. The application exits and I
> > get the exception after it exits.
> >
> > Stack Trace:
> >
> >    at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
> >    at System.Threading.EventWaitHandle.Set()
> >    at System.IO.Ports.SerialStream.AsyncFSCallback(UInt32 errorCode,
> > UInt32 numBytes, NativeOverlapped* pOverlapped)
> >    at
> > 
> System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32
> > errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
> >
> > Please let me know what can be the issue.
> >
> > Thanks & regards,
> > Sheetal
> >
> >
> >
> >
> >
> >
> > ______________________________________________________________________
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Users mailing list
> > Users at lists.ironpython.com
> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >  
>
>
> -- 
> http://www.ironpythoninaction.com/
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
> ______________________________________________________________________
>
>
> ______________________________________________________________________
> ------------------------------------------------------------------------
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>   


-- 
http://www.ironpythoninaction.com/




More information about the Ironpython-users mailing list