954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Controlling other windows control when receiveng data from serial port

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.

siEricko
Newbie Poster
7 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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

DangerDev
Posting Pro in Training
485 posts since Jan 2008
Reputation Points: 165
Solved Threads: 59
 

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

siEricko
Newbie Poster
7 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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;
      }
    }
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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..

siEricko
Newbie Poster
7 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

OK, replace your timer1.Enabled line with this:

this.Invoke(new MethodInvoker(
                      delegate()
                      {
                        this.timer1.Start();
                      }));
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

ok thanks..will try that..

siEricko
Newbie Poster
7 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You