Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've 2 buttons. Button Connect(open com port) & Disconnect(close port). When I click on the connect button, the timer will trigger and display the data on Listbox from the com port every few seconds. When I click disconnect and click connect button again, the data will not be displayed on the Listbox and the timer is not triggered. Why is that so?

C#
 private void btnConnect_Click(object sender, EventArgs e)
    {

        if (serialPortN.IsOpen)
            serialPortN.Close();
        try
        {
            {

                serialPortN.PortName = "COM8";
                serialPortN.BaudRate = 9600;
                serialPortN.Parity = Parity.None;
                serialPortN.DataBits = 8;
                serialPortN.StopBits = StopBits.One;
                serialPortN.Encoding = System.Text.Encoding.ASCII;
                serialPortN.ReadTimeout = 500;
            }

            serialPortN.Open();
            label1.Text = "COM8" + " is opened";


            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 5000;
            timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
            timer.Start();

            serialPortN.DataReceived += new SerialDataReceivedEventHandler(datareceived);


            btnDisconnect.Enabled = true;
            btnConnect.Enabled = false;

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
     void datareceived(object sender, SerialDataReceivedEventArgs e)
    {
        myDelegate d = new myDelegate(update);
        listBox1.Invoke(d, new object[] { });

    }

    public void update()
    {

        while (serialPortN.BytesToRead > 0)
            buffer.Add((byte)serialPortN.ReadByte());
        // Call a routine to process the data.          
        ProcessBuffer(buffer);
     }

  private void ProcessBuffer(List<byte> buffer)
    {
       int numberOfBytesToRead = 125;
        if (buffer.Count >= numberOfBytesToRead)
        {
            this.Invoke(new Action(() =>
             Console.WriteLine(string.Format("SPO = {0}, PulseRate = {1}, Time = {2}",
                                                    buffer[43].ToString(),
                                                    buffer[103].ToString(),
                                                    DateTime.Now.ToString()
            ))));

            buffer.RemoveRange(0, numberOfBytesToRead);
        }

    }

   void TimerElapsed(object sender, ElapsedEventArgs e)
    {

        int numberOfBytesToRead = 125;
        if (buffer.Count >= numberOfBytesToRead)
        {
            this.Invoke(new Action(() =>
            listBox1.Items.Add(string.Format("SPO = {0}, PulseRate = {1}, Time = {2}",
                                                    buffer[43].ToString(),
                                                    buffer[103].ToString(),
                                                    DateTime.Now.ToString()
            ))));
            buffer.RemoveRange(0, numberOfBytesToRead);

        }

  private void btnDisconnect_Click(object sender, EventArgs e)
        {
           
            listBox1.Items.Clear();
            serialPortN.Close();
            buffer.Clear();
}

Thanks all.
Posted
Updated 30-Jan-12 21:22pm
v2
Comments
Sergey Alexandrovich Kryukov 29-Jan-12 22:27pm    
Where the buffer is declared? Why 43 and 103?
--SA
fahfahfah 30-Jan-12 0:12am    
List<byte> buffer = new List<byte>(125);
buffer is declared on top.
fahfahfah 31-Jan-12 3:24am    
43 and 103 is the data from the com port.
Addy Tas 31-Jan-12 1:34am    
You have not posted the disconnect code, this may very well give a clue.
fahfahfah 31-Jan-12 3:23am    
Hi Addy, I've already update my codes, with the disconnect button in it.. Thanks

1 solution

you should declare your timer in the class, not in the method
C#
System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 5000;
            timer.Elapsed += new ElapsedEventHandler(TimerElapsed);

in btnConnect_Click you do the timer.Start();
in btnDisconnect_Click you do the Timer.Stop();
 
Share this answer
 
v2
Comments
fahfahfah 31-Jan-12 4:34am    
Hi digi, I've already edited my codes as your suggestion, but the problem still persists... hm..

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