We are having a problem with the software we sell at work. A Customer is reporting that our app is failing if they close a connection to a GPS device and then re-open it the operation will fail.
This is in a product being used by a lot of different customers - so it's clearly something odd about the hardware. Thing is, I can reproduce this bug in a GPS device we have here, so it's a bit of an issue.
The device in question is a Bluetooth device which we communicate with via the standard Virtual Serial Port object Windows creates.
I've boiled down the code to the bare basics and we're still getting the issue:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Reflection;
namespace Scratchpad
{
class gpsTest
{
SerialPort gpsPort;
public gpsTest()
{
gpsPort = new SerialPort();
if (gpsPort != null)
{
gpsPort.ReadTimeout = 2000;
gpsPort.BaudRate = 9600;
}
}
internal bool openPort(string _portName, ref string error)
{
error = string.Empty;
gpsPort.PortName = _portName;
try
{
if (!gpsPort.IsOpen)
{
gpsPort.Open();
}
}
catch (Exception ex)
{
error = ex.Message;
}
return gpsPort.IsOpen;
}
internal bool closePort()
{
try
{
if (gpsPort.IsOpen)
{
gpsPort.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return gpsPort.IsOpen;
}
public List<string> findAvailablePorts()
{
List<string> ports = new List<string>();
foreach (string str in SerialPort.GetPortNames())
ports.Add(str);
return ports;
}
}
}
The problem is this. If I run the openPort() method then the device connects fine. When I run closePort() then the port appears to close fine. Then, if I re-run openPort() I get an exception thrown: System.UnauthorizedAccessException with a message: "Access to Port COM6 is denied" where COM6 is the port the GPS device is listening on.
The only way to re-open the port is to reboot the GPS device itself.
Things I have tried:
I have tried closing and disposing of the BaseStream object myself before I close the gpsPort object.
I have tried the fix discussed
hereThis is .net2 and .net2 CF so any solutions have to be compatible with that.
Anyone have any ideas at all?
Flibberdy