관리 메뉴

DREAM IT, WISH IT, DO IT

MS Chart 를 이용한 실시간 그래프 그리기 본문

Programming/C#

MS Chart 를 이용한 실시간 그래프 그리기

개발일지 오세빈 2012. 6. 1. 05:30

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.IO;

using System.Threading;

using System.Windows.Forms;

using System.Windows.Forms.DataVisualization.Charting;

Using System.IO.Ports;


namespace LineChartTEST

{

    public partial class Form1 : Form

    {

        private delegate void CanIJust();

        private List<int> _valueList;

        private List<double>_timeList;

        private List<int> _customValueList;


        string InputData = String.Empty;

        delegate void SetTextCallback(string text);

        SerialPort SP = new SerialPort();

        public Form1()

        {        

InitializeComponent();

_valueList = new List<int>();

            _timeList = new List<double>();

           _customValueList = new List<int>();


            SP.PortName = "COM9";

            SP.BaudRate = 115200;

            SP.DataBits = 8;

            SP.Parity = Parity.None;

            SP.StopBits = StopBits.One;

          SP.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);

            SP.Open();

        }


        protected override void OnClosed(EventArgs e)

        {

base.OnClosed(e);

        }


        private void UpdateSecondChart()

        {

chart.Series[0].Points.AddXY(_timeList[_timeList.Count - 1], _customValueList[_customValueList.Count - 1]);

chart.Series[0].IsValueShownAsLabel = true;
chart.Legends[0].Enabled = false;

chart.Invalidate();

        }


        private void btnClear_Click(object sender, EventArgs e)

        {

           chart.Series[0].Points.Clear();

       }


        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)

        {

            InputData = SP.ReadExisting();


            if (InputData != String.Empty)

            {

                if(InputData.Length > 4)

                {

                    if (String.Compare(InputData.Substring(1, 2), "P") == 0 || String.Compare(InputData, "P")== 0)

                    {                      

SP.WriteLine("K");

                   }

               }

                else if (String.Compare(InputData, "P")== 0)

                {

                }

                else

                {

                    if (InputData.Length >= 2)

                    {                      

SetText(InputData.Substring(0, InputData.Length - 1));

                   }

                    else

                    {

                    }

                }

            }

        }

        private void SetText(string text)

        {

            if (this.InvokeRequired)

            {

                SetTextCallback d = new SetTextCallback(SetText);

                this.Invoke(d, new object[] { text });

            }

            else

            {                             

_customValueList.Add(Convert.ToInt32(text));

               UpdateSecondChart();

            }

        }

        private void button_Click(object sender, EventArgs e)

        {

            if (SP.IsOpen)

SP.WriteLine("K");

            else

                MessageBox.Show("Serial port is closed!");

        }

    }

}

2 Comments
  • 프로필사진 Timberland shops 2012.12.24 12:58 You should always remember to label both each individual ski, http://www.timberlandbaratas.com Mujer Timberland, so to ensure your skis meet you at the other side, http://www.timberlandbaratas.com outlet timberland, They started a number of domestic airline flights as well as to the various important cities of the world, http://www.timberlandbaratas.com Hombre Timberland.One excellent strategy is to simply put your laptop in a secure protected case and put it in the hold it makes sense to put in in your luggage that will got in the cargo hold, http://www.timberlandbaratas.com barato timberland.
    Related articles:


    http://philkant.tistory.com/396 http://philkant.tistory.com/396

    http://parentvine.net/forums/topic/luxury-transport-service-is-because-he-is-under-along-with-e#post- http://parentvine.net/forums/topic/luxury-transport-service-is-because-he-is-under-along-with-e#post-

    http://losermarxdr.tistory.com/937 http://losermarxdr.tistory.com/937
  • 프로필사진 요원009 2015.12.23 07:47 신고 http://yowon009.tistory.com/107

    링크 바꼈습니다.
댓글쓰기 폼