none
代码注释 RRS feed

  • 问题

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

    namespace RealTimeCurve
    {
        public partial class RealTimeCurve : Form
        {

            private clsDrawThread myDrawThread = null;
            private Thread thdDraw = null;
            System.IO.Ports.SerialPort ComPort = new SerialPort();

            public RealTimeCurve()
            {
                InitializeComponent();
            }

            private void RealTimeCurve_Load(object sender, EventArgs e)
            {
                Rectangle rt1 = new Rectangle(50, 0, 640, 250);
                // Create thread class
                //   myDrawThread = new clsDrawThread(this, new DrawHandler(RefreshPictureBox), this.ClientSize.Width, this.ClientSize.Height);
                myDrawThread = new clsDrawThread(this, new DrawHandler(RefreshPicture), rt1.Width, rt1.Height);
                // Start sub thread to draw
                thdDraw = new Thread(new ThreadStart(myDrawThread.DrawGraph));
                thdDraw.Start();

            }

            public void Draw(double xAxis,double yAxis,double temperature,double heartRate)
            {
                myDrawThread.X = xAxis;
                myDrawThread.Y = yAxis;
                myDrawThread.Tempetature = temperature;
                myDrawThread.HeartRate = heartRate;
                Rectangle rt1 = new Rectangle(50, 0, 640, 250);
                Graphics g = CreateGraphics();
                g.DrawImage(myDrawThread.DrawImage, rt1, rt1, GraphicsUnit.Pixel);
            }
            private void RealTimeCurve_Closing(object sender, CancelEventArgs e)
            {
                // Close thread
                myDrawThread.IsStop = true;
                thdDraw.Join(1);
            }

            private void RefreshPicture()
            {
                Rectangle rt1 = new Rectangle(52, 0, 640, 250);
                // Refresh picture
                this.Invalidate(rt1);

            }

            public void DrawCoordinates()
            {
                Font font = new Font("Verdana", 7, FontStyle.Regular);
                SolidBrush blackbrush = new SolidBrush(Color.Black);
                SolidBrush bluebrush = new SolidBrush(Color.Blue);
                SolidBrush yellowbrush = new SolidBrush(Color.Yellow);
                SolidBrush redbrush = new SolidBrush(Color.Red);
                Pen blackPen = new Pen(Color.Black, 2);
                Pen bluePen = new Pen(Color.Blue, 2);
                Pen redPen = new Pen(Color.Red, 2);
                Pen yellowPen = new Pen(Color.Yellow, 2);

                Graphics g = this.CreateGraphics();
                //绘制一条水平直线和一条垂直直线
                g.DrawLine(blackPen, 50, 250, 50, 0);
                g.DrawLine(blackPen, 50, 250, 640, 250);
                //绘制X轴
                for (int i = 0; i < 10; i++)
                {
                    g.DrawString(i.ToString(), font, blackbrush, 50 + i * 60, 270);
                }
                //绘制垂直的字符串
                for (int i = 0; i < 9; i++)
                {
                    g.DrawString("|", font, blackbrush, 110 + i * 60, 250);
                }
                //绘制Y轴
                g.DrawString("3000", font, blackbrush, 0, 0);
                for (int i = 8; i >= 0; i--)
                {
                    g.DrawString(2200 + i * 100 + "-", font, blackbrush, 0, (8 - i) * 30);
                }
                g.DrawLine(blackPen, 235, 315, 265, 315);
                g.DrawString("心率", font, blackbrush, 265, 305);

                g.DrawLine(bluePen, 315, 315, 345, 315);
                g.DrawString("体温", font, bluebrush, 345, 305);

                g.DrawLine(yellowPen, 395, 315, 425, 315);
                g.DrawString("X轴", font, yellowbrush, 425, 305);

                g.DrawLine(redPen, 475, 315, 505, 315);
                g.DrawString("Y轴", font, redbrush, 505, 305);
                //释放对象
                g.Dispose();
                blackbrush.Dispose();
                bluebrush.Dispose();
                yellowbrush.Dispose();
                redbrush.Dispose();
                blackPen.Dispose();
                bluePen.Dispose();
                redPen.Dispose();
                yellowPen.Dispose();
            }

            private void ComOpen()
            {
                if (ComPort.IsOpen)
                {
                    ComPort.Close();
                    GraphTimer.Enabled = false;
                    if (CTrlThread != null)
                        CTrlThread.Abort();
                    menuItem1.Text = "打开串口";

                }
                else
                {
                    //设置串口参数
                    ComPort.BaudRate = int.Parse("115200");
                    ComPort.DataBits = int.Parse("8");
                    ComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "1", true);
                    //ComPort.Parity = (Parity)Enum.Parse(typeof(Parity), "0");
                    ComPort.PortName = "COM3";

                    // 打开串口
                    ComPort.Open();
                    menuItem1.Text = "关闭串口";
                }
            }
            private void menuItem1_Click(object sender, EventArgs e)
            {
                try
                {
                    this.ComOpen();
                }
                catch
                {
                    if (CTrlThread != null)
                        CTrlThread.Abort();
                }
            }
            private byte[] HexStringToByteArray(string s)
            {
                s = s.Replace(" ", "");
                byte[] buffer = new byte[s.Length / 2];
                for (int i = 0; i < s.Length; i += 2)
                    buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
                return buffer;
            }

            //发送数据
            private void SendData()
            {
                if (!ComPort.IsOpen)
                    return;

                //
                //string temp = textBox1.Text;

                string temp = "";
                if (radioButton1.Checked)
                    temp = "7E 42 09 00 00 00 00 00 FF FF 08 7D 5D 7D 5E 00 01 00 7D 5E 00 7D 5E 00 02 BA 14 7E";
                else if (radioButton2.Checked)
                    temp = "7E 42 09 00 00 00 00 00 FF FF 08 7D 5D 7D 5E 00 02 00 7D 5E 00 7D 5E 00 02 38 CC 7E";

                // string temp = "7E 42 09 00 00 00 00 00 FF FF 08 7D 5D 7D 5E 00 01 00 7D 5E 00 7D 5E 00 02 BA 14 7E";

                //
                Byte[] tempByte = this.HexStringToByteArray(temp);
                //
                ComPort.Write(tempByte, 0, tempByte.Length);

            }

            private void menuItem2_Click(object sender, EventArgs e)
            {
                this.BeginTime();
               
            }

            private void BeginTime()
            {
                //
                if (!ComPort.IsOpen)
                {
                    MessageBox.Show("串口未打开");
                    //
                    return;
                }
                //
                if (!GraphTimer.Enabled)
                {
                    //
                    //tickStart = Environment.TickCount;
                    //
                    GraphTimer.Enabled = true;


                }
            }

            // 声明了一个线程:
            private Thread CTrlThread;

            //  声明一个委托:
            public delegate void DelegateCTrlThr();

            //   声明了一个方法:
            private void CTrlThr()
            {
                if (this.InvokeRequired)
                {
                    DelegateCTrlThr m = new DelegateCTrlThr(CTrlThr);
                    this.Invoke(m);
                    return;
                }

                // 获取字节长度
                int bytes = ComPort.BytesToRead;

                // 创建字节数组
                byte[] buffer = new byte[bytes];

                // 读取缓冲区的数据到数组
                ComPort.Read(buffer, 0, bytes);

                //
                //listBox1.Items.Clear();

                //如果不是36位
                if (bytes == 36)
                {
                    //
                    string temp = "";
                    //
                    int index = 26;
                    //
                    int flag = 0;
                    //
                     double[] showLine = new double[4];
                    //
                    while (flag < 4)
                    {
                        //
                        byte[] showContent = new byte[2];
                        //
                        showContent[0] = buffer[index];
                        //
                        index++;
                        //
                        showContent[1] = buffer[index];
                        //
                        index++;
                        //
                        Array.Reverse(showContent);
                        UInt16 Ret = System.BitConverter.ToUInt16(showContent, 0);
                        //float Ret = BitConverter.(showContent, 0);
                        showLine[flag] = Convert.ToDouble(Ret);
                        //
                        if (temp != "")
                        {
                            temp += ", ";
                        }
                        temp += Ret.ToString();
                        //
                        //
                        flag++;
                    }

                    Draw(showLine[0], showLine[1], showLine[2], showLine[3]);
                    //
                    textBox1.Text = temp;
                }
            }

            private void GraphTimer_Tick(object sender, EventArgs e)
            {
                CTrlThread = new Thread(new System.Threading.ThreadStart(CTrlThr));
                this.CTrlThread.Start();
                //
                SendData();
            }

            private void RealTimeCurve_Paint(object sender, PaintEventArgs e)
            {
                DrawCoordinates();
               
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Drawing;
    using System.Windows.Forms;

    namespace RealTimeCurve
    {
        public delegate void DrawHandler();

        class clsDrawThread
        {
            public double X, Y, Tempetature, HeartRate;
            private Bitmap bitGraph = null;
            private bool blnStop = false;
            private int nStartY = -1;
            private int nStartY1 = -1;
            private int nStartY2 = -1;
            private int nStartY3 = -1;
            private int nEndY = -1;
            private int nEndY1 = -1;
            private int nEndY2 = -1;
            private int nEndY3 = -1;
     //       private Random ranPoint = null;
            private int nWidth = 0;
            private int nHeight = 0;
            private Form frmParent;
            private DrawHandler pHandler = null;
            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="pParent"></param>
            /// <param name="Handler"></param>
            /// <param name="Width"></param>
            /// <param name="Height"></param>
            public clsDrawThread(Form pParent, DrawHandler Handler, int Width, int Height)
            {
                // Init class member
                nWidth = Width;
                nHeight = Height;
                frmParent = pParent;//Parent form
                pHandler = Handler;//Call back delegate
                // Create double buffer
                bitGraph = new Bitmap(nWidth, nHeight);
      //          ranPoint = new Random(nHeight);//Create random

            }
            public bool IsStop
            {
                set { blnStop = value; }
            }
            public Image DrawImage
            {
                get { return bitGraph; }
            }
            /// <summary>
            /// Sub thread entry function
            /// </summary>
            public void DrawGraph()
            {
                while (!blnStop)
                {
                    Thread.Sleep(300);
                    if (blnStop) break;
                    //Redraw bitmap
                    RedrawImage();
                    //Refresh pictorebox
                    try
                    {
                        frmParent.Invoke(pHandler);
                    }
                    catch { break; }
                }
            }
            /// <summary>
            /// Re-draw image
            /// </summary>
    //        RealTimeCurve rtc = new RealTimeCurve();
            private void RedrawImage()
            {
                const int CLIP_WIDTH = 3;
                Bitmap bitNew = new Bitmap(nWidth, nHeight);
                Graphics gImage = Graphics.FromImage(bitNew);
                // Use black color to fill the entire rectangle
                gImage.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(0, 0).X, new Point(0, 0).Y, new Size(nWidth, nHeight).Width, new Size(nWidth, nHeight).Height));
                // Copy image from source image
                gImage.DrawImage(bitGraph, new Rectangle(0, 0, nWidth - CLIP_WIDTH, nHeight), new Rectangle(CLIP_WIDTH, 0, nWidth - CLIP_WIDTH, nHeight), GraphicsUnit.Pixel);
                // Draw new clip image
                if (nStartY < 0)
                    nStartY = (3030 - (int)X)* 30/ 100;
                else
                    nStartY = nEndY;
                nEndY = (3030 - (int)X) * 30 / 100;

                if (nStartY1 < 0)
                    nStartY1 = (3030 - (int)Y) * 30 / 100;
                else
                    nStartY1 = nEndY1;
                nEndY1 = (3030 - (int)Y) * 30 / 100;

                if (nStartY2 < 0)
                    nStartY2 = (3030 - (int)Tempetature) * 30 / 100;
                else
                    nStartY2 = nEndY2;
                nEndY2 = (3030 - (int)Tempetature) * 30 / 100;

                if (nStartY3 < 0)
                    nStartY3 = (3030 - (int)HeartRate) * 30 / 100;
                else
                    nStartY3 = nEndY3;
                nEndY3 = (3030 - (int)HeartRate) * 30 / 100;

                // Draw new line
                gImage.DrawLine(new Pen(Color.Yellow, 1.0f), new Point(nWidth - CLIP_WIDTH, nStartY).X, new Point(nWidth - CLIP_WIDTH, nStartY).Y, new Point(nWidth - 1, nEndY).X, new Point(nWidth - 1, nEndY).Y);
                gImage.DrawLine(new Pen(Color.Red, 1.0f), new Point(nWidth - CLIP_WIDTH, nStartY1).X, new Point(nWidth - CLIP_WIDTH, nStartY1).Y, new Point(nWidth - 1, nEndY1).X, new Point(nWidth - 1, nEndY1).Y);
                gImage.DrawLine(new Pen(Color.Blue, 1.0f), new Point(nWidth - CLIP_WIDTH, nStartY2).X, new Point(nWidth - CLIP_WIDTH, nStartY2).Y, new Point(nWidth - 1, nEndY2).X, new Point(nWidth - 1, nEndY2).Y);
                gImage.DrawLine(new Pen(Color.Black, 1.0f), new Point(nWidth - CLIP_WIDTH, nStartY3).X, new Point(nWidth - CLIP_WIDTH, nStartY3).Y, new Point(nWidth - 1, nEndY3).X, new Point(nWidth - 1, nEndY3).Y);
                // Set new image and release old image
                Bitmap bitOld = bitGraph;
                bitGraph = bitNew;
                bitOld.Dispose();
            }
        }
    }

    上面代码中有两个namespace RealTimeCurve,不知道是怎么回事,谢谢!

    2010年12月27日 6:31

答案