kibr987 0 Newbie Poster

Hi All,

I am having problem in connecting the serial port in c#. I have got a PIC microcontroller which is connected to a temperature sensor. It gives me temperature which I am writing in serial port and trying to read it through c#. I have a nice looking meter - I want to show the value in the meter according to the temperature as well as showing it on a textbox.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace temp_sen
{
    public partial class temp_sen : Form
    {
        public string data;
        public double temp; //int value from serial port as temperature
        public double angle; //convert value as angle
        double radian;
        public int x;
        public int y;
        Pen mypen = new Pen(Color.Red, 3);
        SerialPort sp = new SerialPort();
        public temp_sen()
        {
            InitializeComponent();
            cmBox.Items.AddRange(SerialPort.GetPortNames());
        }

        private void temp_sen_Paint(object sender, PaintEventArgs e)
        {
            
            angle = 6.0 * temp;
            if (angle >= 360)
            {
                angle = angle - 360;
            }

            if (angle < 0)
            {
                angle += 360;
            }
            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:\\riad\\temp_sen_serial\\drawing\\meter.JPG");
            e.Graphics.DrawImage(img, 0, 0);
            e.Graphics.DrawLine(mypen, x, y, 192, 202);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            
            sp.BaudRate = 9600;
            sp.DataBits = 8;
            sp.StopBits = StopBits.One;
            sp.Parity = Parity.None;

            try
               {
                   sp.PortName = cmBox.Text;
                    
               }
            catch
                {
                   MessageBox.Show("Please choose a Port");
                }
            sp.DataReceived += new SerialDataReceivedEventHandler(sp_datarecieved);
            sp.Open();
            btnStart.Text = "Connected";
            btnStart.BackColor = Color.Green;
            txtTemp.Text = Convert.ToString(temp);
            btnStart.Enabled = false;
 }

        public delegate void stxtdele(string text);
        void sp_datarecieved(object sender, SerialDataReceivedEventArgs e)
        {
            data = sp.ReadLine(); 
            if (data != null)
            {
                for (int i = 0; i < 5000; i++)
                {
                    CrossThreadOperation.Invoke(txtTemp, delegate { txtTemp.Text = data.Trim(); });
                    temp = double.Parse(data.Trim());                }
            }
        }
            private void txtTemp_TextChanged(object sender, EventArgs e)
        {
            

        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            
            sp.Close();
            
            
        }

            }
}

I am reading the string variable called 'data' from the serial port and converting it to double called 'temp' then changing it to angle and doing the maths afterwards to show it on the meter.

My problem is sometime it works and shows the value in the textbox but the meter doesn't work at all. And some time the textbox doesn't work either and I get this message that the 'Input string was not in a correct format.' in highlighted area in code.

Can u please help me - before I pull all my hair out.

Thanks in advance.

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.