dimanche 18 septembre 2016

Serial Port DataReceived Event If else not Working properly inside this event

I'm having problem with if else statement not working properly inside serialPort1_DataReceived. Problem is both if and else statement executed. But this shouldn't. If the condition for if-statement is true then it shouldn't move to else. But it executes both. In this event I'm making calculations based on Reading data from Port and then retrieve data from Database.

I have limited the data sending speed of Arduino to 1 second also tried 2 seconds still not working. Can you help me why this happening in this thread? I want to use if-else in this thread. Any solution to this problem??? I'm not familiar with getting data from port and threads. I also don't know how these statements executed.

Also guide me what if statement under this event are executing but port send new data, then what will happened? This will leave execution and reading the port again? or it finish the code execution?

Please give me the solution og if-else under this event. That is my general problem.

Code is here:

      private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            i++;

            i = 0;

            string inputStream = port.ReadLine();
            split = inputStream.Split(':');
            string data0 = split[0];//Train 1 Location
            string data1 = split[1];//Train 2 Location

            // *************************** Train 1 Code Start here *******************//
            if (Train1 != null)
            {

                string[] splitT1 = Train1.Split(':');
                if (!(data0.Equals(splitT1[2])))
                {
                    queryT1 = @"Select * from TrainInfo
                              Where Des='" + splitT1[0] + "' and Tag=" + data0;
                    dtT1 = _dbAccess.getDataTable(queryT1);
                    if (dtT1.Rows[0]["Route"].ToString() != "")
                    {

                        string pathsT1=dtT1.Rows[0]["Route"].ToString();
                        string[] splitPathsT1 = pathsT1.Split(',');

                        queryT1 = @"Select * from TrainInfo where TrackId=" + Convert.ToInt32(splitPathsT1[0]);
                        dtFillT1 = _dbAccess.getDataTable(queryT1);
                        if (splitPathsT1.Length > 1)
                        {

                            onoff1 = 1;
                            if (dtFillT1.Rows[0]["IsFill"].ToString()=="false")
                            {

                                queryT1Fill = @"Update TrainInfo Set
                                              IsFill='true'
                                              where TrackId=" +Convert.ToInt32(splitPathsT1[0])+
                                              " Update TrainInfo Set"+
                                              " IsFill='false'" +
                                              " where TrackId=" + Convert.ToInt32(dtT1.Rows[0]["TrackId"]);


                                if (data0.Equals("1")) {
                                    servo1 = 1;
                                }
                                if (data0.Equals("9"))
                                {
                                    servo4 = 1;
                                }
                                _dbAccess.executeNonQuery(queryT1Fill);

                            }

else
                            {
                              queryT1Fill = @"Update TrainInfo Set
                                            IsFill='true'
                                              where TrackId=" + Convert.ToInt32(splitPathsT1[1]) +
                                             " Update TrainInfo Set" +
                                             " IsFill='false'" +
                                            " where TrackId=" +Convert.ToInt32(dtT1.Rows[0]["TrackId"]);
                               if (data0.Equals("1"))
                                {
                                    servo1 = 0;
                                }
                                if (data0.Equals("9"))
                               {
                                    servo4 = 0;
                               }

                               _dbAccess.executeNonQuery(queryT1Fill);
                           }


                        }

                        else
                        {


                            if (dtFillT1.Rows[0]["IsFill"].ToString()=="true")
                            {
                                onoff1 = 0;
                            }
                            else
                            {
                                onoff1 = 1;
                                queryT1Fill = @"Update TrainInfo Set
                                              IsFill='true'
                                              where TrackId=" + Convert.ToInt32(splitPathsT1[0]) +
                                                                              " Update TrainInfo Set" +
                                                                              " IsFill='false'" +
                                                                              " where TrackId=" + Convert.ToInt32(dtT1.Rows[0]["TrackId"]);

                                _dbAccess.executeNonQuery(queryT1Fill);

                                if (data0.Equals("21"))
                                {
                                    servo2 = 1;
                                }
                                if (data0.Equals("12"))
                                {
                                    servo5 = 0;
                                }
                                if (data0.Equals("20"))
                                {
                                    servo5 = 1;
                                }

                            }






                        }

                    }

                    else
                    {
                       // train is on and move forward
                       // means you are not at decision points for train1 moving from Karchi to Lahore
                        onoff1 = 1;
                    }




                }

                if (data0.Equals(splitT1[2]))
                {
                    //Need to off train1 here because Train has reached its destination
                    onoff1 = 0;
                   // WriteToSerialPort("0:0:0:0:0:" + onoff1 + 1 + ":0");
                }


               // WriteToSerialPort("0:0:0:0:0:" + onoff1 + 1 + ":0");
            }
           //**************************** Train 1 Code Ends Here ********************//



 WriteToSerialPort(servo1 + ":" + servo2 + ":" + servo3 + ":" + servo4 + ":" + servo5 + ":" + onoff1 + ":" + onoff2+"\n");





        }

[Database Table Structure][1]

Aucun commentaire:

Enregistrer un commentaire