c# hangs randomly in setmaterialname on data receive of serial port

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
ali_lak
Gnoblar
Posts: 13
Joined: Sun Oct 02, 2016 1:34 pm

c# hangs randomly in setmaterialname on data receive of serial port

Post by ali_lak »

Hi
I have written a Mogre (Managed Ogre) C# application.
the application hangs randomly but don't show any message just " not responding"
i'm not sure but i suggest the problem is in Data_Receive handler of serial port.
the code of this section is presented in below.
and it's may be useful that this function is in different thread of Mogre thread!

Thanks for your suggestions!

Code: Select all

       public void Open_Port()
        {
            try
            {

                Data_Comport = new SerialPort(PortName, 9600, Parity.None, 8, StopBits.One);
                Data_Comport.ReadTimeout = 5000;
                Data_Comport.WriteTimeout = 5000;
                Data_Comport.DataReceived += new SerialDataReceivedEventHandler(PortDataReceivedHandler);
                Data_Comport.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public delegate void InvokeDelegate();
        private void PortDataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            if (Data_Comport.IsOpen)
            {
                //  try
                //   {
                //var line = Data_Comport.ReadLine();
                bool success = false;
                try
                {
                    success = int.TryParse(Data_Comport.ReadLine().ToString(), out myMogre.finger_id);
                }
                catch (InvalidOperationException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                catch (TimeoutException ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                if (success)
                {
                    //if (Data_Comport.ReadLine().Contains("-") && myMogre.finger_id == 1)
                    //    myMogre.finger_id = -myMogre.finger_id;
                    Data_Comport.DiscardInBuffer();
                    Data_Comport.DiscardOutBuffer();
                    myMogre.finger_id = myMogre.finger_id - 2;

                    try
                    {
                        if (myMogre.Verified)
                        {
                            for (int i = 0; i < 5; i++)
                            {
                                if (myMogre.finger_id + 2 == i)
                                {
                                    myMogre.Ok_Coin_Entity[i].SetMaterialName("coinsmat/Ok_coin_defok" + i.ToString());
                                }
                                else
                                {
                                    myMogre.Ok_Coin_Entity[i].SetMaterialName("coinsmat/Ok_coin_def" + i.ToString());
                                }
                            }

                            for (int i = 0; i < myMogre.N_of_Coins; i++)
                            {
                                if (myMogre.finger_id == myMogre.Random_Pos[i] && myMogre.Out_coins[i])
                                {
                                    if (!myMogre.long_lines_isselected[i])
                                    {
                                        myMogre.long_lines_Entities[i].SetMaterialName("Long/long_" + (myMogre.Random_Pos[i] + 2).ToString());
                                        myMogre.long_lines_isselected[i] = true;
                                    }
                                }
                                else
                                {
                                    myMogre.long_lines_Entities[i].SetMaterialName("Long/long" + (myMogre.Random_Pos[i] + 2).ToString());
                                }
                            }
                        }
                    }
                    catch (NullReferenceException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                /*                }
                                catch (System.IO.IOException error)
                                {
                                    return;
                                }
                                catch (System.InvalidOperationException error)
                                {
                                    return;
                                }*/
            }
        }
Post Reply