Hi all,,
I am writing a program that can communicate through serial port. I have manage the port connection, I try to send a string from my phone, and show the string on the textbox. I have successfully done that.

What I want to achieve is,when i receive the data from my phone, I want to control others windows control, such as, enabling the timer.

my code look like this.

private void Form1_Load(object sender, EventArgs e)
{
      _serialport.DataReceived += new   SerialDataReceivedEventHandler(_serialport_DataReceived);
      _serialport.PortName = "COM4";
      _serialport.BaudRate = 9600;
      _serialport.DataBits = 8;
      _serialport.Parity = Parity.None;
      _serialport.StopBits = StopBits.One;
      _serialport.Open();
}

private delegate void SetTextDeleg(string text);

private void _serialport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{          
      string data = _serialport.ReadExisting();            
      this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
      timer1.Enabled = true;          
}

private void si_DataReceived(string data) 
{
      textBox1.Text = data.Trim();
}
private void timer1_Tick(object sender, EventArgs e)
{
      if (sec > 58)
      {
          min++;
          sec = 0;
      }
      else
      {
           sec++;
      }
      if (min == 2 && sec > 29)
      {
           lblTime.ForeColor = Color.Red;
      }
      if (min == 3)
      {
           min = 3;
           sec = 0;
           lblTime.Text = string.Format("{0:00} : {1:00}", min, sec);
           timer1.Enabled = false;
      }
      lblTime.Text = string.Format("{0:00} : {1:00}", min, sec);
}

I also have try to enabled the timer in si_DataReceived, but also failed. How is the correct way to do it? Thanks.

on DataReceived event you can do timer.Start() or timer.Stop(), what ever you want. Did you asked this?

yes,I have tried that. I dont know why it didnt work.. The timer is not active.

Does the text box get its' text set in the data received event? You should also call .Invoke from the Textbox and not the form since that is the control you are manipulating.

private SerialPort port;
    public frmInvoke()
    {
      InitializeComponent();
    }

    private void frmInvoke_Load(object sender, EventArgs e)
    {
      port = new SerialPort();
      port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    }

    void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
      SetMessageText(port.ReadExisting());
    }

    public delegate void SetText(string s);
    public void SetMessageText(string s)
    {
      if (textBox1.InvokeRequired)
      {
        textBox1.Invoke(new SetText(SetMessageText), s);
      }
      else
      {
        textBox1.Text = s;
      }
    }

Yes, for the text I can get it correctly,,its just I try to start the timer when I get the data from the serial port..

I think its also because it has different thread.. N i dont actually understand about thread.. I read somewhere that I need to create another delegate function and using the invoke to enabled the timer,,

maybe anyone can show me how can I do the delegate function for the timer..

thank you..

So is the timer never raising the Tick event? Or are you getting an exception? I just tried starting the timer in another thread and it doesn't throw an error but it doesn't work either.

OK, replace your timer1.Enabled line with this:

this.Invoke(new MethodInvoker(
                      delegate()
                      {
                        this.timer1.Start();
                      }));

ok thanks..will try that..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.