0
votes

In my application I'm using a 9600 baud rate serial connection and I want to use a 115200 baud rate connection for data transfer.

I've disconnected from the old connection and set it to be null value, and set my serial connection to new connection with different baud rate.

The connection is unstable and I sometimes get a System.ObjectDisposedException - what did I miss?

The connection code

   public string startConnection()
   {
        if (serial != null)
        {
            serial.Dispose();

        }

        foreach (string portname in SerialPort.GetPortNames())
        {
           serial = new SerialPort(portname, 9600, Parity.None, 8, StopBits.One);
           serial.ReadTimeout = 5000;
           serial.WriteTimeout = 5000;
           serial.Handshake = System.IO.Ports.Handshake.None;
           serial.NewLine = "\n";
           string received = "";

           try
           {
               serial.Open();
               serial.DiscardInBuffer();
                serial.Write(":09;BATTERY;");
               Thread.Sleep(500);
               received = serial.ReadLine();

               if (received.Contains(";BATTERY;V="))
               {
                   status = SERIAL_CONNECTED;
                   return portname;
               }
            }
            catch (Exception err)
            {
               try
               {
                   serial.Close();
                   status = DISCONNECTED;
               }
               catch (Exception)
               {
                 //  throw;
               }
           }
       }

       throw new Exception("couldn't connect to coms");
       //return "couldn't connect to coms";

       //this.Close();
   }

Disconnect function:

    public void disconnect ()
    {

        if (serial == null || serial.IsOpen==false ||status == DISCONNECTED)
            return;

        status = DISCONNECTED;
        serial.Close();
        serial = null; 
    }

The main program is:

  private async void BurnOFP_click(object sender, RoutedEventArgs e)
    {
            startConnection();
            some actions.............

            disconnect();

            var t = new Task(() =>
                {
                    try
                    {
                     myUswm.startModemConnection(); // same but with different baud rate
                    }
                    catch (Exception e2) { MessageBox.Show(e2.Message); }
                });
            t.Start();
            t.Wait();

            modem = new XMODEM_FullDotNET(myUswm.getSerialPort(), XMODEM_FullDotNET.Variants.XModemCRC);



            buff = File.ReadAllBytes(softwareFilePath_Text.Text);
            if (buff.Length < 1)
            {
                MessageBox.Show("ERROR : wrong OFP file");
                return;
            }

            if (myUswm.prepareOFPBurning()) // sends u to start transfer
            {

                if (isBurning == false)
                {
                    isBurning = true;

                    modem._ProgressSent = 0;
                    myProgBar = new myProgressBar(modem);
                    myProgBar.StartTransfer(modem, buff.Length);
                    myProgBar.Show(); // show window

                   // got the Exception here!!!!!!!!!!
                    var t3 = new Task(() =>
                    {
                         modem.Send(buff);
                    });
                   ............

                }
                else
                    MessageBox.Show("burning in progress..");
            }
        }
        catch (Exception e1)
        {
            MessageBox.Show(e1.Message);
        }

    }

Thanks for any help

1
Do you want to communicate with your device in both 9600 baud and also 115200 baud? did you changed the device UART to support 115200? What does it means "unstable" and where do you get System.ObjectDisposedException? BTW - why did you used asynchronous Task for myUswm.startModemConnection() if you just wait for it to finish? - idanp
1. I did changed the UART support. 2. unstable means that sometimes i got the Exception and somtimes not. 3. I get the exception after closing the Serial connection, and reopen it with new Serial connection on the same port. 4.it is an old implemantion for the connect method. there is no point doing it on async Task anymore. - Michael Gabbay

1 Answers

0
votes

RESOLVED

my problem was A bad timing caused by closing and reopen the same port.

I've found the information in MSDN Serial class:

The best practice for any application is to wait for some amount of time after calling the Close method before attempting to call the Open method, as the port may not be closed instantly.

my solution was keeping the connection alive and change the baud rate and update the connection status in my application manually.