c#老化测试程序

来源:互联网 发布:下载淘宝网到桌面手机 编辑:程序博客网 时间:2024/04/29 21:18
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;
namespace Comm
{
    public partial class testform : Form
    {


        Serial AVR = new Serial(); 
        public testform(string strText)
        {
            InitializeComponent();
            AVR.OpenPort(strText + ":", 57600, 0, 8, 0);
            comboBox1.Items.Add("65535");
            comboBox1.Items.Add("0");
            comboBox1.SelectedIndex = 0;
            textBox17.Text = "0";
            textBox16.Text = "0";


          //  threadSerial = new Thread(new ThreadStart(Read_ServialThread));
          //  threadSerial.Start();
/*
           DataBase bu = new DataBase();
           
          DataTable bu1 = new DataTable();


           string cs1;
           string cs2;
         //  MessageBox.Show("1");
           cs2 = "insert into  Swatch,mName,mResult,BatchID,Unit,CheckDate,CheckTime ,ProName,BedNo,ProNo,mResultDetail values ('svs','deng,'1','2','3,'2145-05-05','16:20:21,'1','2','3,'svs') ";
           string cs = "select Swatch,mName,mResult,BatchID,Unit,CheckDate,CheckTime ,ProName,BedNo,ProNo,mResultDetail from Result where Checkdate = '" + "2145-05-05" + "' order by xlh ";
           // bu.SetTable(cs2);// MessageBox.Show("1");
           bu.ExecSQLForSQLCE(cs2.Trim(),1);
            bu1 = bu.GetTable(cs);
          cs1 = bu1.Rows[0]["mName"].ToString(); //MessageBox.Show("1");
          MessageBox.Show(cs1);
*/
        }
        string ls_RevceDate = "";
        #region 启动读串口数据线程
        public void Read_ServialThread()
        {
            while (true)
            {
                if (AVR.BytesToRead > 0)
                {
                  
                    int Count = AVR.BytesToRead;
                    byte[] tmpbuf = new byte[Count];
                    AVR.Read(tmpbuf, 0, Count);
                    ls_RevceDate = BitConverter.ToString(tmpbuf).Trim();


                    if (ls_RevceDate.Substring(0, 2) == "55")
                    {
                        
                        //MessageBox.Show(ls_RevceDate);


                    }
                    else
                    {
                       
                       // MessageBox.Show(ls_RevceDate);
                    }


                }
                Thread.Sleep(300);
            }
        }
        #endregion
        private void write_ser()
        {
            Byte[] Send = new Byte[1];
            Send[0] = 0xAA;
            string ls_RevceDate;
            do
            {
            AVR.Write(Send, 0, 1);
            byte[] tmpbuf = new byte[24];
            AVR.Read(tmpbuf, 0, 1);
            ls_RevceDate = BitConverter.ToString(tmpbuf).Trim();


            }
            while (!(ls_RevceDate != "")&&(ls_RevceDate.Substring(0, 2) != "55"));
        }
        private void write_DAT(Byte[] buuff)
        {
            


            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x01;
            buuff[23] = 3;
            //MessageBox(_T("123"));


            AVR.Write(buuff, 0, 24);
        }
        private void write_OUT()
        {
            Byte[] buuff = new Byte[24];


            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x03;
            buuff[23] = 4;
            //MessageBox(_T("123"));


            AVR.Write(buuff, 0, 24);
        }
        private void write_IN()
        {
            Byte[] buuff = new Byte[24];


            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x04;
            buuff[23] = 5;
            //MessageBox(_T("123"));


            AVR.Write(buuff, 0, 24);
        }
        public void Read_Servial()
        {
            string ls_RevceDate = "";
            int Count = AVR.BytesToRead;
            byte[] tmpbuf = new byte[Count];
            AVR.Read(tmpbuf, 0, Count);
            ls_RevceDate = BitConverter.ToString(tmpbuf).Trim();
        }
        private void 选择2_Click(object sender, EventArgs e)
        {


        }


        public static void Delay(int milliSecond)
        {
            int start = Environment.TickCount;
            while (Math.Abs(Environment.TickCount - start) < milliSecond)
            {
                Application.DoEvents();
            }
        }
        private void button1_Click(object sender, EventArgs e)//01-00-00-02-00-00-00-F4-EC-00-00-00-00-00-00-00-00-00-00-00-00-00-00-E3
        {
            Byte[] buuff = new Byte[24];
            int i,j;
            write_ser();
            for ( i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x04;
            i= int.Parse(textBox4.Text) & 0xff00 >> 0x8;
            buuff[7] = (byte)i;
            j = int.Parse(textBox4.Text) & 0xff;
            buuff[8] = (byte)j;
            buuff[23] = (byte)(i + j + buuff[0] + buuff[3]);
            AVR.Write(buuff, 0, 24);
           
        }


        private void button3_Click(object sender, EventArgs e)//01-00-00-02-01-00-00-07-D0-00-00-00-00-00-00-00-00-00-00-00-00-00-00-DB
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x01;
            buuff[7] = 0x07;
            buuff[8] = 0xD0;
            buuff[23] = 0xDB;
            AVR.Write(buuff, 0, 24);
        }


        private void button2_Click(object sender, EventArgs e)//01-00-01-02-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-04
        {
            Byte[] buuff = new Byte[24];
             write_ser();
           // for (int j = 0; j < 3;j++ )
            {
              
                    if (AVR.BytesToRead > 0)
                    {


                        int Count = AVR.BytesToRead;
                        byte[] tmpbuf = new byte[Count];
                        AVR.Read(tmpbuf, 0, Count);
                        ls_RevceDate = BitConverter.ToString(tmpbuf).Trim();


                        if (ls_RevceDate.Substring(0, 2) == "55")
                        {
                            for (int i = 0; i < 24; i++)
                            {
                                buuff[i] = 0;
                            }
                            buuff[0] = 0x01;
                            buuff[2] = 0x01;
                            buuff[3] = 0x02;
                            buuff[23] = 0x04;
                            AVR.Write(buuff, 0, 24);
                            //MessageBox.Show(ls_RevceDate);


                        }
                        else if (AVR.BytesToRead > 20)
                        {


                            //break;
                        }


                    }
                    Thread.Sleep(300);
                }
            /*
            string ls_Receve1 = ls_RevceDate.Replace("-", "").Substring(16, 4);


            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(ls_Receve1);


            if(bytes[0]>=65)
            {
                bytes[0]=(byte)(bytes[0]-'A'+0xa);
            }
            else
            {
                bytes[0]=(byte)(bytes[0]-'0');
            }


            if (bytes[1] >= 65)
             {
                 bytes[1] = (byte)(bytes[1] - 'A' + 0xa);
             }
             else
             {
                 bytes[1] = (byte)(bytes[1] - '0');
             }


             if (bytes[2] >= 65)
             {
                 bytes[2] = (byte)(bytes[2] - 'A' + 0xa);
             }
             else
             {
                 bytes[2] = (byte)(bytes[2] - '0');
             }


             if (bytes[3] >= 65)
             {
                 bytes[3] = (byte)(bytes[3] - 'A' + 0xa);
             }
             else
             {
                 bytes[3] = (byte)(bytes[3] - '0');
             }
             int ii = (int)(bytes[0]*16*16*16+bytes[1]*16*16+bytes[2]*16+bytes[3]);
            ls_Receve1 = ii.ToString();
          //  MessageBox.Show(ls_Receve1);
            /*          byte[] bytes = System.Text.Encoding.ASCII.GetBytes(ls_Receve1);.Substring(0,24)
                     byte[] bytes1=new byte[2];
                     bytes1[0] = bytes[7];
                     bytes1[1] = bytes[8];
                     ls_RevceDate = System.Text.Encoding.Default.GetString(bytes1, 0, 2);
                     textBox5.Text = ls_RevceDate; //textBox5.GotFocus;
          */
            textBox5.Text = textBox4.Text;// ls_Receve1;
        }


        private void button4_Click(object sender, EventArgs e)//01-00-01-02-01-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-05
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x01;
            buuff[23] = 0x05;
            AVR.Write(buuff, 0, 24);
            textBox7.Text = textBox6.Text;
        }


        private void button5_Click(object sender, EventArgs e)//01-00-00-02-02-00-00-07-D0-00-00-00-00-00-00-00-00-00-00-00-00-00-00-DC
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x02;
            buuff[7] = 0x07;
            buuff[8] = 0xD0;
            buuff[23] = 0xDC;
            AVR.Write(buuff, 0, 24);
        }


        private void button6_Click(object sender, EventArgs e)//01-00-01-02-02-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-06
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x02;
            buuff[23] = 0x06;
            AVR.Write(buuff, 0, 24);
            textBox9.Text = textBox8.Text;
        }


        private void button14_Click(object sender, EventArgs e)
        {
            write_ser();
            write_OUT();
        }


        private void button15_Click(object sender, EventArgs e)
        {
            write_ser();
            write_IN();
            
        }


        private void button13_Click(object sender, EventArgs e)//01-00-01-01-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-03
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x01;
            buuff[23] = 0x03;
            AVR.Write(buuff, 0, 24);
        }


        private void testform_Closing(object sender, CancelEventArgs e)
        {
            AVR.Close();
        }


        private void button7_Click(object sender, EventArgs e)//01-00-00-02-07-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0A
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[7] = 0x07;
            buuff[23] = 0x0A;
            AVR.Write(buuff, 0, 24);
        }


        private void button8_Click(object sender, EventArgs e)//01-00-01-02-07-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0B
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x07;
            buuff[23] = 0x0B;
            AVR.Write(buuff, 0, 24);
        }


        private void button9_Click(object sender, EventArgs e)//01-00-00-02-08-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0B
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x08;
            buuff[23] = 0x0B;
            AVR.Write(buuff, 0, 24);
        }


        private void button10_Click(object sender, EventArgs e)//01-00-01-02-08-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0C
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x08;
            buuff[23] = 0x0C;
            AVR.Write(buuff, 0, 24);
        }


        private void button11_Click(object sender, EventArgs e)//01-00-00-02-09-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0C
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x09;
            buuff[23] = 0x0C;
            AVR.Write(buuff, 0, 24);
        }


        private void button12_Click(object sender, EventArgs e)//01-00-01-02-09-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0D
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x09;
            buuff[23] = 0x0D;
            AVR.Write(buuff, 0, 24);
        }


        private void button17_Click(object sender, EventArgs e)//01-00-00-02-0D-00-00-FF-FF-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0E
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x0D;
            buuff[7] = 0xFF;
            buuff[8] = 0xFF;
            buuff[23] = 0x0E;
            AVR.Write(buuff, 0, 24);
        }


        private void button18_Click(object sender, EventArgs e)//01-00-01-02-0D-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-11
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x0D;
            buuff[23] = 0x11;
            AVR.Write(buuff, 0, 24);
        }


        private void button16_Click(object sender, EventArgs e)//01-00-00-07-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-08
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x07;
            buuff[23] = 0x08;
            AVR.Write(buuff, 0, 24);
            textBox16.Text = "000000";
        }


        private void button19_Click(object sender, EventArgs e)//01-00-00-02-03-00-00-07-D0-00-00-00-00-00-00-00-00-00-00-00-00-00-00-DD
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x03;
            buuff[7] = 0x07;
            buuff[8] = 0xD0;
            buuff[23] = 0xDD;
            AVR.Write(buuff, 0, 24);
        }


        private void button20_Click(object sender, EventArgs e)//01-00-01-02-03-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-07
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x03;
            buuff[23] = 0x07;
            AVR.Write(buuff, 0, 24);
        }


        private void button21_Click(object sender, EventArgs e)//01-00-00-02-04-00-00-07-D0-00-00-00-00-00-00-00-00-00-00-00-00-00-00-DE
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x04;
            buuff[7] = 0x07;
            buuff[8] = 0xD0;
            buuff[23] = 0xDE;
            AVR.Write(buuff, 0, 24);
        }


        private void button22_Click(object sender, EventArgs e)//01-00-01-02-04-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-08
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x04;
            buuff[23] = 0x08;
            AVR.Write(buuff, 0, 24);
        }


        private void button23_Click(object sender, EventArgs e)//01-00-00-02-06-00-00-07-D0-00-00-00-00-00-00-00-00-00-00-00-00-00-00-E0
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x06;
            buuff[7] = 0x07;
            buuff[23] = 0xE0;
            AVR.Write(buuff, 0, 24);
        }


        private void button24_Click(object sender, EventArgs e)//01-00-01-02-06-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0A
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x06;
            buuff[23] = 0x0A;
            AVR.Write(buuff, 0, 24);
        }


        private void button25_Click(object sender, EventArgs e)//01-00-00-02-0A-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0D
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x0A;
            buuff[23] = 0x0D;
            AVR.Write(buuff, 0, 24);
        }


        private void button26_Click(object sender, EventArgs e)//01-00-01-02-0A-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0E
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x0A;
            buuff[23] = 0x0E;
            AVR.Write(buuff, 0, 24);
        }


        private void button27_Click(object sender, EventArgs e)//01-00-00-02-0B-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0E
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x0B;
            buuff[23] = 0x0E;
            AVR.Write(buuff, 0, 24);
        }


        private void button28_Click(object sender, EventArgs e)//01-00-01-02-0B-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0F
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x0B;
            buuff[23] = 0x0F;
            AVR.Write(buuff, 0, 24);
        }


        private void button29_Click(object sender, EventArgs e)//01-00-00-02-0C-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-0F
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x0C;
            buuff[23] = 0x0F;
            AVR.Write(buuff, 0, 24);
        }


        private void button30_Click(object sender, EventArgs e)//01-00-01-02-0C-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-10
        {
            Byte[] buuff = new Byte[24];


            write_ser();
            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x02;
            buuff[4] = 0x0C;
            buuff[23] = 0x10;
            AVR.Write(buuff, 0, 24);
        }
    }

}


定时扫描

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;
namespace Comm
{
    public partial class TimeTest : Form
    {
        Thread Time_threadMain;
        Thread Time_threadSerial;
        Thread Time_thread;
        string stringType;
        Serial Time_AVR = new Serial();
        // Timer Timer1 = new Timer();
        string ls_RevceDate = "";
        public TimeTest()
        {
            InitializeComponent();
        }
        public TimeTest(string strTextBox1Text, string strText)
        {
            InitializeComponent();
            bool res = Time_AVR.OpenPort(strText + ":", 57600, 0, 8, 0);
            if (!res)
            {
                MessageBox.Show("Open COM3: error");
            }


            Time_threadSerial = new Thread(new ThreadStart(Read_Servial));
            Time_threadSerial.Start();


            Time_thread = new Thread(new ThreadStart(Read_Time));
            Time_thread.Start();


            Main_States1(); 
            Time_threadMain = new Thread(new ThreadStart(Main_States));
            Time_threadMain.Start();
            this.label2.Text = strTextBox1Text;
            stringType = strTextBox1Text;
            label5.ForeColor = Color.Red;


        }
        private void Read_Time()
        {
            string m_time;
            while (true)
            {
                 m_time = DateTime.Now.ToString();
                this.Invoke(new SetMessageDelegate(setLabelText), label7, m_time);
                Thread.Sleep(1000);
            }
        }


        private void label3_ParentChanged(object sender, EventArgs e)
        {


        }


        private void Time_WriteSpeed(Serial AVR)
        {
            Byte[] buuff = new Byte[24];


            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x01;
            buuff[23] = 3;
            //MessageBox(_T("123"));


            Time_AVR.Write(buuff, 0, 24);
            Thread.Sleep(Int32.Parse(stringType) * 1000);
            //    MessageBox.Show(ls_RevceDate);
        }
    


        private void WriteSpeed(Serial AVR)
        {
            Byte[] buuff = new Byte[24];


            for (int i = 0; i < 24; i++)
            {
                buuff[i] = 0;
            }
            buuff[0] = 0x01;
            buuff[2] = 0x01;
            buuff[3] = 0x01;
            buuff[23] = 3;
            //MessageBox(_T("123"));


            Time_AVR.Write(buuff, 0, 24);
           // Thread.Sleep(Int32.Parse(stringType) * 1000);
         //    MessageBox.Show(ls_RevceDate);
        }
        int bg = 0;
        private void button1_Click_1(object sender, EventArgs e)
        {
            Time_threadMain.Abort(); ;
            Time_threadSerial.Abort(); ;
            Time_threadMain = new Thread(new ThreadStart(Main_States));
            Time_threadMain.Start(); ;
            Time_threadSerial = new Thread(new ThreadStart(Read_Servial));
            Time_threadSerial.Start(); ;
            bg = 1;
            bg = 0;
            button1.Enabled = false;
            button2.Enabled = true;
        }


        private void button2_Click_1(object sender, EventArgs e)
        {


           // Time_AVR.Close();
            Time_threadMain.Abort(); ;
            Time_threadSerial.Abort(); ; bg = 0;
            label6.Text = "停止测试...";
            bg = 0;
            button1.Enabled = true;
            button2.Enabled = false;
        }


        long m_cal = 1;
        #region 启动主线程
        public void Main_States1()
        {




            this.Invoke(new SetMessageDelegate(setLabelText), label6, "正在扫描...,");
            Byte[] Send = new Byte[1];
            Send[0] = 0xAA;
            Time_AVR.Write(Send, 0, 1);








        }
        #endregion
        #region 启动主线程
        public void Main_States()
        {


            string cal = m_cal.ToString(); //MessageBox.Show(cal);


            if (bg == 0)
            {
                this.Invoke(new SetMessageDelegate(setLabelText), label5, cal); m_cal++;
                Thread.Sleep(Int32.Parse(stringType) * 1000);
            }
            int intType = Int32.Parse(stringType);
            while (true)
            {
                string ls_RevceDate = ""; this.Invoke(new SetMessageDelegate(setLabelText), label6, "正在扫描...,");
                Byte[] Send = new Byte[1];
                Send[0] = 0xAA;
                do
                {
                    Time_AVR.Write(Send, 0, 1);


                    byte[] tmpbuf = new byte[24];
                    Time_AVR.Read(tmpbuf, 0, 1);
                    ls_RevceDate = BitConverter.ToString(tmpbuf).Trim();


                }
                while (!(ls_RevceDate != "") && (ls_RevceDate.Substring(0, 2) != "55"));


                 cal = m_cal.ToString(); m_cal++; //MessageBox.Show(cal);
                this.Invoke(new SetMessageDelegate(setLabelText), label5, cal);
                Thread.Sleep(intType * 1000);
            }
        }
        #endregion
        string ls_date;
        #region 启动读串口数据线程
        public void Read_Servial()
        {
            while (true)
            {
                if (Time_AVR.BytesToRead > 0)
                {
                    ls_RevceDate = "";
                    int Count = Time_AVR.BytesToRead;
                    byte[] tmpbuf = new byte[Count];
                    Time_AVR.Read(tmpbuf, 0, Count);
                    ls_RevceDate = BitConverter.ToString(tmpbuf).Trim();
                    if (ls_RevceDate.Substring(0, 2) == "55")
                    {
                        WriteSpeed(Time_AVR);
                        //MessageBox.Show(ls_RevceDate);


                    }
                    else
                    {
                        if (ls_RevceDate.Trim() != "")
                        {
                            if (ls_RevceDate.Trim() != "FE")
                            {
                                OutText(ls_RevceDate);
                                if (ls_date == "2")
                                {
                                    this.Invoke(new SetMessageDelegate(setLabelText), label6, "正在扫描...,没有插入ID芯片,请检查设备!");
                                    
                                }
                                else if (ls_date == "3")
                                {
                                    this.Invoke(new SetMessageDelegate(setLabelText), label6, "正在扫描...,试剂卡没有插到正确位置!");
                                  
                                }
                                else if (ls_date == "4")
                                {
                                    this.Invoke(new SetMessageDelegate(setLabelText), label6, "正在扫描...,试剂卡插反,请重新插入!");
                                   
                                }
                                else
                                {
                                    this.Invoke(new SetMessageDelegate(setLabelText), label6, "测试正常!");
                                    
                                }


                            }
                            
                               // MessageBox.Show(ls_RevceDate);
                        }
                    }


                  


                    // MessageBox.Show(ls_RevceDate);


                }
                Thread.Sleep(300);
            }
        }
        #endregion


        private delegate void SetMessageDelegate(Label control, string msg);
        private void setLabelText(Label myControl, string myArg)
        {
            myControl.Text = myArg;
        }


        #region 读串口有效数据
        public void OutText(string text)
        {
            string ls_Receve = text.Replace("-", "");


            if (ls_Receve.Substring(0, 2) == "FE")
            {
                ls_Receve = ls_Receve.Substring(2, ls_Receve.Length - 2);
            }


            string ls_StrOne = ls_Receve.Substring(0, 8);// 第1个字节
            // MessageBox.Show(ls_Receve);
            if (ls_Receve.Trim().Length == 48 && ls_StrOne.Trim() == "01000101")
            {
                //返回有问题的包
                ls_date = Convert.ToInt32(ls_Receve.Substring(8, 2), 16).ToString();// 第4个字节
                if (ls_date == "2")
                {
                    //label6.Text += "没有插入ID芯片,请检查设备!";
                }
                else
                {
                    if (ls_date == "3")
                    {


                        //label6.BeginInvoke(new SetMessageDelegate(this.setLabelText), "试剂卡没有插到正确位置!");
                        // this.label6.Text = "试剂卡没有插到正确位置!";
                    }
                    else
                    {
                        if (ls_date == "4")
                        {
                            // label6.Text += "试剂卡插反,请重新插入!";
                        }
                    }
                }
            }
            else
            {
            }
        }
        #endregion


        private void TimeScan1_Closing(object sender, CancelEventArgs e)
        {
            Time_AVR.Close();
            Time_threadMain.Abort();
            Time_threadSerial.Abort();
            Time_thread.Abort();


        }


     
    }
}

0 0
原创粉丝点击