I am having a problem with OpenNETCF.IO.Serial

  • Thread starter Thread starter jayderk
  • Start date Start date
J

jayderk

Hello All,

I seem to be having a problem with my reOpenPort function
my problem is EVERY time this function is called for the second time..
(first time I call it everything seems to go well, the second time it bombs
out).

I am just testing a loop that looks for data on the port every 10 seconds,
if it finds data great it displays the data and cycles again. If it does not
find any data for over a minute it calles this function ( I took out the
try/catch to see exactly what error was thrown).

on the second time around it failes with this error message

//
A managed CommPortException occured at Application::Run+0xf
CreateFile Failed:55

then some reference to my app and Main form

//


so, there is no communication going on. it is simply calling this function
on Minute 1 successfully and it is failing on the Minute 2 iteration every
time.


public bool reOpenPort()
{
Library.CfDebugger.WriteDebugLine("in reOpenPort");
if(port != null)
{
if(port.IsOpen)
port.Close();

Library.CfDebugger.WriteDebugLine("Closed Port");


// try
// {
if(port.Open())
{
Library.CfDebugger.WriteDebugLine("port.Open");
return true;
}
// }
// catch(Exception myex)
// {
// Library.CfDebugger.WriteDebugLine("port.Open FAILED");
// }

return false;
}

return false;
}
 
would it be possible to rewrite your code so that reOpen() disposed of the
com port isntance and recreated it?
 
Hello Alex,

I did that yesterday while I was waiting for a response and it worked like a
charm. Is there any upside or downside to disposing/recreating it?
 
Hi,

Opening and closing the port is a very expensive process.

IMO, unless you need to share the port with another application, you should
simply keep it open until your app ends.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
 
Back
Top