Pages: [1]   Go Down
  Print  
Author Topic: Bizarre Issue with Opening/Closing SerialPort in C#  (Read 116 times)
0 Members and 1 Guest are viewing this topic.
Flibberdy
On the teat!
*

Good Guy/Gal Points. 16
Offline Offline

Posts: 78



« on: November 17, 2010, 11:43:34 AM »

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:

Code:

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 here

This is .net2 and .net2 CF so any solutions have to be compatible with that.

Anyone have any ideas at all?

Flibberdy

« Last Edit: November 17, 2010, 01:17:59 PM by Flibberdy » Logged
Wooster
Wall Eyed Wanker
Administrator
Alcoholic
*

Good Guy/Gal Points. -518
Online Online

Posts: 5547


'An how faust kin it ging?'


« Reply #1 on: November 17, 2010, 02:11:08 PM »

Beyond my ken.
The only regular problem I ever had with COM ports was on XP which had a habit of locking you out of them until you unistalled them and scanned for hardware changes.
(In saying that I have a card reader that does exactly the same thing). confused

Best of luck. tongue2
Logged

gi joe
Guest
« Reply #2 on: November 17, 2010, 05:22:16 PM »

Beyond me but having used them recently, I'd suggest maybe use Process Monitor and Process Explorer from Sysinternals over at MS.  You can use those to debug what's going on at the time, may or may not help.

Used them both recently to troubleshoot a annoying issue at work where a laptop kept pausing for about 10 mins before doing anything.  Seems to have pointed to a MobileConnect Vodafone dongle hammer the registry for 7 mins for no reason.

So maybe of some help in debugging.
Logged
Ctulu
Server Admin
Alcoholic
*****

Good Guy/Gal Points. 126
Offline Offline

Posts: 6969


« Reply #3 on: November 23, 2010, 03:05:44 PM »

wat happens if you try the Finalize method?

http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx


Or this initializelifetimeservice method?

http://msdn.microsoft.com/en-us/library/system.marshalbyrefobject.initializelifetimeservice.aspx
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: