Serial Port In C#
<p>I am working with a program the reads data from a file and plots it to a graph in real time. My stream is coming from a microcontroller output and I am building an interface to display the data. I am using stream reader in my routine, but I have a problem.</p>

<p>I have decided to use the data from the serial port directly. I get an error when I try to use the variable line that I have just read from the port. I do not know what I am doing wrong.</p>

<p>Thanks.</p>

<pre><code>        int tickStart = 0;
        System.IO.Ports.SerialPort port;
        public string portname;
        public Parity parity;
        public int BaudRate;
        public StopBits stopbits;
        public int databits;
        int count;

        String line;

        public string PortName
        {
            get { return portname; }
            set { portname = value; }
        }

        private void Form1_Load( object sender, EventArgs e )
        {
        //graphing stuff    
            count = 0;
            portname = "COM1";
            parity = Parity.None;
            BaudRate = 9600;
            stopbits = StopBits.Two;
            databits = 8;
            port = new System.IO.Ports.SerialPort(portname);
            port.Parity = parity;
            port.BaudRate = BaudRate;
            port.StopBits = stopbits;
            port.DataBits = databits;
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
            count = 0;
            }


        void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
          try
            {
             line = port.ReadLine(); 
              count++;
              }
          catch (Exception ex)
             {
              MessageBox.Show(ex.Message);
          }
         }



        private void timer1_Tick( object sender, EventArgs e )
        {
            //graphing stuff
            if (list == null)
                return;

            // Time is measured in seconds
            double time = (Environment.TickCount - tickStart) / 1000.0;

            double value = double.Parse(line);
            list.Add(time, value);
           //graphing stuff

          }
</code></pre>
