Serialport: Need help please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • M Kibria
    New Member
    • Nov 2010
    • 1

    Serialport: Need help please

    Hi,

    I am reading the output of a microcontroller through the serial port to show a value on a textbox and to draw a meter accordingly. I have written the datarecieved function which works OK as it follows.

    Code:
    void sp_datarecieved(object sender, SerialDataReceivedEventArgs e)
            {
              data = sp.ReadLine(); 
              temp = Convert.ToDouble(data);
              if (data != null)
                {
                    for (int i = 0; i < 5000; i++)
                    {
                        CrossThreadOperation.Invoke(txtTemp, delegate { txtTemp.Text = Convert.ToString(data.Trim()); });
                    }
                    }
                        }

    For the graphics I have written the following.

    Code:
    private void temp_sen_Paint(object sender, PaintEventArgs e)
            {
                angle = 6.0 * temp;
                radian = (Math.PI * angle/ 180);
                x = Convert.ToInt32(192 - 162* Math.Cos(radian));
                y = Convert.ToInt32(202 - 162*Math.Sin(radian));
                Image img = Image.FromFile("C:\\mohammad\\p\\tem_sen\\temp_sen_serial\\drawing\\meter.JPG");
                e.Graphics.DrawImage(img, 0, 0);
                e.Graphics.DrawLine(mypen, x, y, 192, 202);
            }
    The problem is when I check the value in 'angle = 6.0 * temp;' using a break point I can see 'temp' = 23 for example but I get 0.0 in 'angle' and that why the fraphics doesn't work as I want it to work.

    I don't know what the problem is.

    I would really be glad if someone can help me here.

    Thanks in advance.
  • Alex Bug
    New Member
    • Oct 2010
    • 3

    #2
    You can try blocking object (lock object temp)... See here http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

    Comment

    Working...