0

i have an application that detects a USB 3G Dongle that will be used for sending SMS. My application queries the Dongle via AT Commands to determine if it is the RIGHT dongle, this means that that certain dongle can only be used in my application (even if the Dongle is of the same model). Sending and receiving is fine, no problems or whatsoever. If the 3G Dongle is removed from the USB port, the system detects this and executes the proper procedures.

Here's my problem. When the 3G Dongle is re-inserted, say on the same port (COM5), my application detects this and executes some AT Command to determine that the re-inserted dongle is the RIGHT dongle. But an error occurs stating:

THE RESOURCE IS IN USE

The application must be terminated or closed to be able to use the same port (say COM5). Then I encountered an application, almost with the same function, but is able to use the dongle when re-inserted.

BTW, my dongle is ZTE MF190, and the application I saw is from Huawei. I am using C#. Is there any work around on this? or better, is there a better logic on this? say using a service, etc..

EDIT: every query done to the Dongle is done in a separate thread so as to be able to use my application while sending and receiving..

Thanks!

newbie
  • 1
  • 1
  • possible duplicate of [Releasing a unplugged virtual Serial Port](http://stackoverflow.com/questions/9835881/releasing-a-unplugged-virtual-serial-port) – Hans Passant Jan 15 '13 at 01:11

1 Answers1

0

I too had a similar issue with the windows serial port component. There appears to be bugs in the C# code.

Long story short, I managed to get around this by closing the port in a background thread.

Here is my code, note that you may need to modify to fit your application:

    private bool ClosePort()
    {
        _Closing = true;
        _SerialPort.DiscardInBuffer();
        _SerialPort.DiscardOutBuffer();
        if (!_SerialPort.IsOpen) return true;
        //We run this in a new thread to avoid issue when opening and closing
        //The .NET serial port sucks apparently - and has issues such as hanging and random exceptions
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(DoClosePort));
        t.Start();
        //here we wait until is **SHOULD*** be closed - note the better way is to fire an internal event when its finished 
        //We may need to tinker with this wait time
        System.Threading.Thread.Sleep(500);
        return _SerialPort.IsOpen;
    }

    private void DoClosePort()
    {
        try
        {
            //System.Threading.Thread.Sleep(500);
            _SerialPort.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error closing " + _SerialPort.PortName + ". Error Message: " + ex.Message + "\r\n");
        }
    }

Note that if you try sending/receiving while you are closing, check the _Closing class variable before you attempt the send.

Hope this helps anyone.

Simon
  • 9,197
  • 13
  • 72
  • 115
  • thanks for your solution but it didn't work for me. Even if i try setting everything to null or dispose, the error is the same. The problem is that the user can always pull the Dongle from the USB port anytime, either while the application is sending, receiving, etc.. – newbie Jan 15 '13 at 01:24
  • You may need to catch the exception when trying to send/receive, and if so prompt the user. The code above worked for me to allow correct closing of the port and enabling you to reuse the same port number – Simon Jan 15 '13 at 01:26
  • Yes i already did that. enclosed everything relating to the serial port.. regardless of when I check for the "_closing" class, i have no control over that because the user might remove the dongle(probably for the sake for fun). – newbie Jan 15 '13 at 01:38
  • added some info on my question – newbie Jan 15 '13 at 01:43