Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this following event :

 private void button1_Click(object sender, EventArgs e)
   {

       try
       {
           sPort = new SerialPort();
           sPort.PortName = comboBox1.Text;
           sPort.BaudRate = Convert.ToInt32(comboBox5.Text);
           sPort.DataBits = Convert.ToInt32(comboBox3.Text);
           sPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox2.Text);
           sPort.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox4.Text);
           sPort.Handshake = Handshake.None;
           sPort.RtsEnable = true;
           sPort.DtrEnable = true;
           sPort.DataReceived += new SerialDataReceivedEventHandler(sPort_datareceived);

           sPort.Open();
       }
       catch (Exception err)
       {
           MessageBox.Show(err.Message, MessageBoxButtons.OK.ToString());
       }
   }

    private void sPort_datareceived(object sender, SerialDataReceivedEventArgs e)
   {
       SerialPort sp = (SerialPort)sender;
       datain = sp.ReadExisting();
       this.Invoke(new EventHandler(idextraction));
   }

   public string namingid;

   private void idextraction(object sender, EventArgs e)
   {
       Match matchid = Regex.Match(datain, @"\b\d{12}\b");
       namingid = matchid.Value;
       namingid = namingid.Substring(namingid.Length - 7);
       this.Invoke(new EventHandler(writesyncdata));
   }

private void writesyncdata(object sender, EventArgs e)
   {
       try
       {
           TextWriter tw = new StreamWriter(@"C:\\intdata\\" + namingid + ".txt");
           tw.Write(datain);
           tw.Close();
       }
       catch (Exception err)
       {
           MessageBox.Show(err.Message, MessageBoxButtons.OK.ToString());
       }
   }

suppose this Event triggers X No. of times and then stops and then Triggers Again And the Cycle Goes On. The Time Interval is between 1-2 sec when the event triggers X no. of times. I want to Invoke My method once the very first time the event triggers and stop afterwards but My method should Execute once every time the Cycle Starts.

When idextraction() invokes it doesn't work because the data in buffer is less to process(it takes 1-2 sec to fill full data that but my method invokes before that :(

Exactly How do i wait for my method to be executed after all the data has been received in the buffer ? and How do check if it is enough to process using the same event ?

What I have tried:

I Know the how to execute a method once but as the event triggers many times in short period of time so my method also and i don't want that. Does anybody know how to do it ? Please Share your thoughts.
Posted
Updated 26-Mar-22 2:34am
v3
Comments
Richard MacCutchan 25-Mar-22 9:00am    
"Mymethod() should Execute once every time the Cycle Starts."
How do you determine when the cycle starts, and when it ends?
Member 15560521 26-Mar-22 1:46am    
Actually, it's an HIS instrument whenever the user transmits data from other side the cycle starts and it takes around 1-3 sec(Depends on Baud rate) to end the Cycle.
Richard MacCutchan 26-Mar-22 4:55am    
That is irrelevant. Your code needs to determine when a cycle starts, and when it ends. And without some specific indicator that is not possible.
Member 15560521 26-Mar-22 8:35am    
i shared my rest of the code and updated description
Richard MacCutchan 26-Mar-22 9:47am    
You cannot rely on data arrival times to tell you when a cycle starts or stops. The only thing you can do is set a timer and check that whenever you get some data. But timers themselves are not real-time, so it will still be largely guesswork.

1 solution

That's not random, and it's not something you want to change - the DataReceived event is fired when a character is received from the port, not a "message" and by the time the event handler is processed, there may be zero, one, or more characters in the buffer.
If the data from the other end of the connection is X characters long, you will likely get X calls you your method (but not necessarily, Windows is not a real-time OS, and there are both hardware and software buffers in there before it even gets to the OS let alone your app).

If you want to process messages, then don'T use the DataReceived event at all - set up a Background thread to look for and process incoming characters, assemble them into messages, and pass them "up the chain" to by invoking MyMethod.
 
Share this answer
 
Comments
Member 15560521 25-Mar-22 8:36am    
I appreciate your answer but i think i am not able to clearly express my problem here SoI've Improved my question Can u check again ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900