operation not valid due to current state of object

Apr 5, 2011 at 7:30 PM

Thanks for this great work.....

here is where I get the above exception.. Any Ideas? I am running OOB with elevated permissions.

 I have selected the com port from a combo and am trying to oconnect on button click

 

 

 

 

 

 

 

public void button1_Click(object sender, RoutedEventArgs e)
        {
            if (App.Current.HasElevatedPermissions)
            {
                try
                {
                    com.Close();
                    com = new SerialPort(comboBox1.SelectedItem.ToString(), 4800, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                    com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived);
                    com.Open();
                    if (!com.IsOpen)
                    {
                        MessageBox.Show("Port not opened");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("ex" + ex.Message); <=====here is the above error message

               }
            }

  Will the code below display my GPS stream in a textbox?

  

        void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (!com.IsOpen)
                com.Open();
            try
            {
                Thread.Sleep(500);
                string data = com.ReadLine();
                Dispatcher.BeginInvoke(() => textBox1.Text = data);
            }
            catch (Exception f) { MessageBox.Show("f"+f.ToString()); }
        }

 

 

 

Coordinator
Apr 5, 2011 at 8:30 PM

Hi pcrafton.

At first sight I would suggest, that your attempt closing the port is provocating the exception, as it was not open before. You should verify that by setting breakpoints in each line inside the try-block and have a look, which ones are reached before the exception is thrown.

For your question regarding the display of the gps-stream: There is no need for the sleep. The method is only invoked by the data-received-event -- which is only fired after data was received.

You should consider running a while-loop until the buffer is empty instead of just reading one line. Your message-box would lag behind the signals from gps, because you are waiting and then only fetching one line.

 

Regards,

Paul

Apr 5, 2011 at 9:00 PM


Thanks for the reply, actually it was the changing of parity, databits etc. raising the exception

I can only change the baud rate, which is okay and all works well.

Thanks for the tip on the data received event.

Regards

Paul Crafton



PTS Line-Up Add On:

Are you looking for a more efficient and effective Line-Up tool?

Check out this video tour of our new Line-Up Add On Solution-PTS LineUp

PTS LINEUP



PRIVILEGE AND CONFIDENTIALITY STATEMENT: This e-mail, and any attachments thereto, is intended only for use by the addressee(s) named herein and may contain legally privileged and/or confidential information. If you are not the intended recipient of this e-mail (or the person responsible for delivering this document to the intended recipient), you are hereby notified that any dissemination, distribution, printing or coping of this e-mail, and any attachment thereto, is strictly prohibited. If you have received this e-mail in error, please respond to the individual sending the message, and permanently delete the original and any copy of any e-mail and printout thereof.

From: gotthelastid [email removed]
Sent: Tuesday, April 05, 2011 2:31 PM
To: Paul Crafton
Subject: Re: operation not valid due to current state of object [interopcom:252638]

From: gotthelastid

Hi pcrafton.

At first sight I would suggest, that your attempt closing the port is provocating the exception, as it was not open before. You should verify that by setting breakpoints in each line inside the try-block and have a look, which ones are reached before the exception is thrown.

For your question regarding the display of the gps-stream: There is no need for the sleep. The method is only invoked by the data-received-event -- which is only fired after data was received.

You should consider running a while-loop until the buffer is empty instead of just reading one line. Your message-box would lag behind the signals from gps, because you are waiting and then only fetching one line.

Regards,

Paul