锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

C# 探测器测试系统

时间:2023-06-22 16:07:00 th矩形电连接器

运行效果:

33c6c11d5e6dbdc7d7b1e36bcca5c95f.png

重要知识点:

  1. 控件循环遍历控制;

  2. 队列数组的应用:

    private Queue[] dataQueue = new Queue[8];  //把Queue看成一种类型 int[] a=new int [8]

3. 主要用于界面实时刷新显示数据;

4. 窗体最大化而不遮挡任务栏;

5. 数据解码、标准差、平均值等.

主窗体MainForm.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Xml; using BLL;   using System.Threading; using System.Net; using System.Net.Sockets; using System.Windows.Forms.DataVisualization.Charting;     namespace ThzDataProcess {     public partial class MainForm : DevComponents.DotNetBar.Office2007Form     {         private int frameRate = 8,channel=1;             bool isStart = false, isStart1 = false;         const string stopIcon = @"icon\stop.png";         const string startIcon = @"icon\start.png";         DataProcess DPBLL = null;         object ThreadLock = new object();           private Queue[] dataQueue = new Queue[8];//把Queue看成一种类型 int[] a=new int [8]         public MainForm() {             this.DoubleBuffered = true;//设置本窗体             SetStyle(ControlStyles.UserPaint, true);             SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 背景禁止擦除.             SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲                 this.EnableGlass = false;             InitializeComponent();               InitChart();             string DeviceIP = "192.168.1.120";             int LocalPort = 8009;             DPBLL = new DataProcess(DeviceIP, LocalPort);               DPBLL.ShowEvent_zyr1 = dataShow;             DPBLL.ShowEvent_zyr2 = chartShow;             DPBLL.Start();//启动线程               radioButton1.Checked = true;             radioButton2.Checked = false;               dataQueue[0] = new Queue(100);             dataQueue[1] = new Queue(100);             dataQueue[2] = new Queue(100);             dataQueue[3] = new Queue(100);             dataQueue[4] = new Queue(100);             dataQueue[5] = new Queue(100);             dataQueue[6] = new Queue(100);             dataQueue[7] = new Queue(100);             }         // 防止闪屏                 protected override CreateParams CreateParams         {             get             {                 CreateParams cp = base.CreateParams;                 cp.ExStyle |= 0x02000000;                 return cp;             }         }               private void MainForm_Load(object sender, EventArgs e) {             this.WindowState = FormWindowState.Normal;             this.FormBorderStyle = FormBorderStyle.Sizable;             this.Top = 0;             this.Left = 0;             this.Width = Screen.PrimaryScreen.WorkingArea.Width;             this.Height = Screen.PrimaryScreen.WorkingArea.Height;         }           private void InitChart() {             Chart[] ch = new Chart[8] { chart1, chart2 , chart3, chart4, chart5, chart6, chart7, chart8 };             for (int i = 0; i < 8; i  )             {                 ch[i].ChartAreas.Clear();                 ChartArea chartArea1 = new ChartArea("C1");                 ch[i].ChartAreas.Add(chartArea1);                 ///定义存储和显示点的容器                 ch[i].Series.Clear();                 Series series1 = new Series("S1");                 series1.ChartArea = "C1";                 ch[i].Series.Add(series1);                   ch[i].ChartAreas[0].AxisY.IsStartedFromZero = false;                 ch[i].Legends[0].Enabled = false;                   ch[i].ChartAreas[0].AxisX.Interval = 5;                 ch[i].ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;                 ch[i].ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;                 //设置标题                 ch[i].Titles.Clear();                 ch[i].Titles.Add("S01");                 ch[i].Titles[0].Text = "通道"   (i   1)   " AD折线图显示";                 ch[i].Titles[0].ForeColor = Color.RoyalBlue;                 ch[i].Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);                 ///设置图显示样式                 ch[i].Series[0].Color = Color.Red;                 //this.chart1.Titles[0].Text = string.Format("{0}折线图显示",);
                ch[i].Series[0].ChartType = SeriesChartType.Line;
                ch[i].Series[0].Points.Clear();
            }
        }
        public void chartShow( Double y,int ch)
{


            Chart[] chNum = new Chart[8] { chart1, chart2, chart3, chart4, chart5, chart6, chart7, chart8 };
            if(ch <= 8)
               chartDisplay(chNum[ch-1], ch, y);


        }
        delegate void ChartDelegate(Chart chart, int ch, Double y);
        private void chartDisplay(Chart chart, int ch, Double y)
{


            if (chart.InvokeRequired)
            {
                ChartDelegate chartDelegate = chartDisplay;
                chart.Invoke(chartDelegate, new object[] { chart, ch, y });
            }
            else
            {
                if ( isStart == true )
                    UpdateQueueValue(ch,y);
                chart.Series[0].Points.Clear();
                // for (int j = 0; j < 100; j++)
                //     chart1.Series[0].Points.AddXY(j, y);
                for (int i = 0; i < dataQueue[ch-1].Count; i++)
                    chart.Series[0].Points.AddXY((i + 1), dataQueue[ch-1].ElementAt(i));
            }
        }


        private void btnStart_Click(object sender, EventArgs e)
{
            if (!isStart)
            {
                Command.CommandUp_v1(frameRate);
                btnStart.Text = @"停止采集";
                btnStart.DisabledImage = btnStart.Image;
                btnStart.Image = (Image)btnStart.PressedImage.Clone();
                isStart = !isStart;


            }
            else
            {
                Command.CommandUp_v1(0);
                btnStart.Text = @"开始采集";
                btnStart.Image = btnStart.DisabledImage;
                isStart = !isStart;
            }
        }


        private void UpdateQueueValue(int ch,Double y)
{


            if (dataQueue[ch-1].Count > 100)
                //先出列
                dataQueue[ch-1].Dequeue();
            dataQueue[ch-1].Enqueue(y);




        }
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
            Command.CommandUp_v1(0);
        }


        public void dataShow(string avg, string stdDev, string maxMin, int ch)
{
            double temperatureSensitivity = 0.0;
            double dValue = 0.0,temp1=0.0, temp2 = 0.0, tempV1 = 0.0, tempV2 = 0.0;


            //ShowMessage(dataGridViewX1, str, row, column);
            Label[] lb = new Label[48] { label_1,label_2,label_3,label_4,label_5, label_6, label_7, label_8, label_9, label_10 , label_11, label_12, label_13, label_14, label_15,label_16,
                                         label_17,label_18,label_19,label_20,label_21, label_22, label_23, label_24, label_25, label_26 , label_27, label_28, label_29, label_30, label_31,label_32,
                                         label_33,label_34,label_35,label_36,label_37, label_38, label_39, label_40, label_41, label_42 , label_43, label_44, label_45, label_46, label_47,label_48 };


            if (ch <= 8 && isStart == true)
            {
                if (radioButton1.Checked == true)
                {


                    ShowMessage(lb[(ch - 1) * 6], "V1 : " + avg);
                    ShowMessage(lb[(ch - 1) * 6 + 1], "σ1 : " + stdDev);
                    ShowMessage(lb[(ch - 1) * 6 + 2], "Max/Min :" + maxMin);
                
                }
                else
                {
                    ShowMessage(lb[(ch - 1) * 6 + 3], "V2 : " + avg);
                    ShowMessage(lb[(ch - 1) * 6 + 4], "σ2 : " + stdDev);
                    ShowMessage(lb[(ch - 1) * 6 + 2], "Max/Min :" + maxMin);
                }


                if (textBox1.Text != "" && textBox2.Text != "")
                {
                    dValue = Math.Abs(Convert.ToDouble(textBox1.Text) - Convert.ToDouble(textBox2.Text));
                    temp1 = Convert.ToDouble(lb[(ch - 1) * 6 + 1].Text.Substring(5, lb[(ch - 1) * 6 + 1].Text.Length - 5));
                    temp2 = Convert.ToDouble(lb[(ch - 1) * 6 + 4].Text.Substring(5, lb[(ch - 1) * 6 + 4].Text.Length - 5));
                    tempV1 = Convert.ToDouble(lb[(ch - 1) * 6].Text.Substring(5, lb[(ch - 1) * 6].Text.Length - 5));
                    tempV2 = Convert.ToDouble(lb[(ch - 1) * 6 + 3].Text.Substring(5, lb[(ch - 1) * 6 + 3].Text.Length - 5));
                    if (tempV1 - tempV2 != 0)
                        temperatureSensitivity = (temp1 + temp2) * dValue / Math.Abs(tempV1 - tempV2) / 2.0;
                    ShowMessage(lb[(ch - 1) * 6 + 5], "ΔT :" + temperatureSensitivity.ToString("0.00"));
                }


            }
            else
            {
                ;
            }
        }


       


        delegate void ShowMessageDelegate(Label lbl, string message);
        private void ShowMessage(Label lbl, string message)
{
            if (lbl.InvokeRequired)
            {
                ShowMessageDelegate showMessageDelegate = ShowMessage;
                lbl.Invoke(showMessageDelegate, new object[] { lbl, message});
            }
            else
            {


                lbl.Text = message;
            }
        }


       
    }
}

类DataCalculate.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThzData;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Drawing;


using ThzDataProcess;
using System.Windows.Forms;




namespace BLL
{
    class DataCalculate
    {


       
        DataProcess dp = null;
        public  void tempartureData_zyr(List Zhendata, Action ShowEvent_zyr2)
        {
            StringBuilder sNeed = new StringBuilder();
           
            foreach (Byte[] match in Zhendata)
                sNeed.Append(BitConverter.ToString(match).Replace("-", "").Substring(20).ToUpper());
            double[] temparture = GetTemparture_zyr(sNeed.ToString());
            // mf.ShowlbDevTem(string.Format("设备温度:\nT1:{0:N}°\nT2:{1:N}°\nT3:{2:N}°\nT4:{3:N}°", temparture[0], temparture[1], temparture[2], temparture[3]));
            ShowEvent_zyr2(string.Format("设备温度:\nT1:{0:N}°\nT2:{1:N}°\nT3:{2:N}°\nT4:{3:N}°", temparture[0], temparture[1], temparture[2], temparture[3]));
           // dp.callBack_zyr2(string.Format("设备温度:\nT1:{0:N}°\nT2:{1:N}°\nT3:{2:N}°\nT4:{3:N}°", temparture[0], temparture[1], temparture[2], temparture[3]));
            sNeed.Clear();
            foreach (var tem in temparture)
                sNeed.Append(tem + "***");
            strWrite_zyr(sNeed.ToString(), Environment.CurrentDirectory + "\\bin", "tempartureData.txt");
            sNeed.Clear();




        }


        public double[] GetTemparture_zyr(string str)
        {
            string tepStr = str.Substring(20 * 2, 8 * 2);
            double[] Temparture = new double[4];
            if (tepStr == null)
                return Temparture;
            byte[] Wen = DataProcess.strToToHexByte(tepStr);
            if ((Wen[0] & 0xf0) >> 4 == 15)
                Temparture[0] = -(~Wen[1] + 1 + 256.0 * ~Wen[0]) / 16;
            else
                Temparture[0] = (Wen[1] + 256.0 * Wen[0]) / 16;


            if ((Wen[2] & 0xf0) >> 4 == 15)
                Temparture[1] = -(~Wen[3] + 1 + 256.0 * ~Wen[2]) / 16;
            else
                Temparture[1] = (Wen[3] + 256.0 * Wen[2]) / 16;


            if ((Wen[4] & 0xf0) >> 4 == 15)
                Temparture[2] = -(~Wen[5] + 1 + 256.0 * ~Wen[4]) / 16;
            else
                Temparture[2] = (Wen[5] + 256.0 * Wen[4]) / 16;


            if ((Wen[6] & 0xf0) >> 4 == 15)
                Temparture[3] = -(~Wen[7] + 1 + 256.0 * ~Wen[6]) / 16;
            else
                Temparture[3] = (Wen[7] + 256.0 * Wen[6]) / 16;
            return Temparture;
        }
        int zhenRows = 0;
        public void adDataCaculate_zyr(List Zhendata, Action ShowEvent_zyr1, Action ShowEvent_zyr2)
        {
            //byte[] byteData = new byte[35250];//1410*25
           
            int count1 = 0, count2 = 0,sampleCount=0;
            StringBuilder sNeed = new StringBuilder();
            StringBuilder sNeed1 = new StringBuilder();
            //MainForm mf = new MainForm();


            sampleCount = Zhendata[0][28] * 256 + Zhendata[0][29];
            byte[] byteData = new byte[Zhendata.Count()* 1410];//1410*44
            foreach (Byte[] Package in Zhendata)
            {
                count1 = 0;
                foreach (byte byt in Package)
                {
                    if (count1 >= 10)   //跳过第一个数
                    {


                        byteData[count2] = byt;
                        count2++;


                    }
                    count1++;
                }
            }
            // Console.ReadKey();
            double[] channel = new double[sampleCount];//473、465
            double[] channel1 = new double[sampleCount];
            double variance = 0.0, average_original = 0.0 , average_converted = 0.0,stdDev = 0.0,maxNum= 0.0,minNum = 0.0;
            for (int i = 0; i < 36; i++)
            {
                for (int j = 0; j < sampleCount; j++)
                {
                    byte bigByte = Convert.ToByte(byteData[j * 74 + i * 2 + 40].ToString("X"), 16);
                    if ((bigByte & 0xf0) >> 4 == 15)
                    {
                        channel[j] = -(~byteData[j * 74 + i * 2 + 41] + 1 + 256.0 * ~byteData[j * 74 + i * 2 + 40]) / 8192.0 * 5;//2^12 =4096
                        channel1[j] = -(~byteData[j * 74 + i * 2 + 41] + 1 + 256.0 * ~byteData[j * 74 + i * 2 + 40]);
                    }
                    else
                    {
                        channel[j] = (byteData[j * 74 + i * 2 + 41] + 256.0 * byteData[j * 74 + i * 2 + 40]) / 8192.0 * 5;
                        channel1[j] = (byteData[j * 74 + i * 2 + 41] + 256.0 * byteData[j * 74 + i * 2 + 40]) ;
                    }
                    sNeed.Append(channel[j] + ",");
                    sNeed1.Append(channel1[j] + ",");
                }


                Stopwatch elapsetime = new Stopwatch();
                elapsetime.Start();




                stdDev = CalculateStdDev(channel)*1000;//标准差
                average_converted = channel.Average()*1000;
                maxNum = channel.Max()*1000;
                minNum = channel.Min()*1000;
                average_original = channel.Average();


                ShowEvent_zyr1(average_converted.ToString("0.00"), stdDev.ToString("0.00") ,maxNum.ToString("0.00")+"/"+minNum.ToString("0.00"),i+1);
                ShowEvent_zyr2(average_original, i+1);


                elapsetime.Stop();
                Console.WriteLine(elapsetime.ElapsedMilliseconds.ToString("000"));


                //variance = Var_zyr(channel);//方差
                // ShowEvent_zyr1(zhenRows, 2 * i + 1, variance.ToString());
                //sNeed.Append("***variance:" + variance + "***average:");


                //ShowEvent_zyr1(zhenRows, 2 * i, average.ToString());
                // ShowEvent_zyr3(i+1,average);
                //sNeed.Append(average);
                //strWrite_zyr(sNeed.ToString(), Environment.CurrentDirectory + "\\bin", "channelData.txt");
                //sNeed.Clear();
            }
            zhenRows++;
        }


       // private static double CalculateStdDev(IEnumerable values)
             private static double CalculateStdDev(double[] values)
        {
            double ret = 0;
            if (values.Count() > 0)
            {
                //  计算平均数   
                double avg = values.Average();
                //  计算各数值与平均数的差值的平方,然后求和 
                double sum = values.Sum(d => Math.Pow(d - avg, 2));
                //  除以数量,然后开方
                ret = Math.Sqrt(sum / values.Count());
            }
            return ret;
        }


        public double Var_zyr(double[] v)
        {
            double sum1 = 0;
            for (int i = 0; i < v.Length; i++)
            {
                double temp = v[i] * v[i];
                sum1 = sum1 + temp;
            }


            double sum = 0;
            foreach (double d in v)
            {
                sum = sum + d;
            }
            double var = sum1 / v.Length - (sum / v.Length) * (sum / v.Length);
            return var;
        }
        private int rowCount = 0;
        private void strWrite_zyr(string str, string filePath, string fileName)
        {


            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            if (!File.Exists(filePath + "\\" + fileName))
                File.Create(filePath + "\\" + fileName).Close(); //.Close 很关键,不然会有问题
            if (rowCount < 3600)
            {
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, true);//true 追加数据
                sw.WriteLine(str);
                sw.Close();
                rowCount++;
            }
            else
            {
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, false);
                sw.WriteLine(str);
                sw.Close();
                rowCount = 0;
            }
        }
    }
}

类DataProcess.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThzData;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Drawing;


using ThzDataProcess;
using System.Windows.Forms;




namespace BLL
{
    public class Command
    {
        public static void CommandUp(int frame)
        {
            //string sendString = null;//要发送的字符串 
            byte[] Data = new byte[8];
            switch (frame)
            {
                case 0:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 00, 16 };//停止
                    break;
                case 6:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 00, 32 };//6帧/s 启动
                    break;
                case 8:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 00, 48 };//仅上传AD值
                    //Data = new byte[] { 22, 144, 87, 235, 00, 00, 00, 33 };//8帧/s 启动
                    break;
                case 10:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 00, 34 };//10帧/s 启动
                    break;
                case 12:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 00, 35 };//12帧/s 启动
                    break;
                case -1:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 00, 48 };//仅上传AD值
                    break;
                default:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 00, 33 };//8帧/s 启动
                    break;
            }
            UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 9001));
            //广播发送指令
            IPAddress remoteIP = IPAddress.Parse("255.255.255.255");
            int remotePort = 8010;
            //实例化广播
            IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);


            //sendString = Console.ReadLine();
            //sendData = Encoding.Default.GetBytes(sendString);


            //client = new UdpClient();
            //将数据发送到远程端点 
            client.Send(Data, Data.Length, remotePoint);
            //关闭连接
            client.Close();


        }


        public static void CommandUp_v1(int frame, byte tair = 00)                     //看不懂
        {
            //string sendString = null;//要发送的字符串 
            byte[] Data = new byte[8];
            switch (frame)
            {
                case 0:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 16, tair };//停止
                    break;
                case 6:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 32, tair };//6帧/s 启动
                    break;
                case 8:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 48, tair };//仅上传AD值
                    //Data = new byte[] { 22, 144, 87, 235, 00, 00, 33, tair };//8帧/s 启动
                    break;
                case 10:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 34, tair };//10帧/s 启动
                    break;
                case 12:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 35, tair };//12帧/s 启动
                    break;
                case 1:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 97, tair };//自动获取编码器值范围
                    break;
                case 60:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 80, tair };//6帧/s 校准参数
                    break;
                case 80:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 81, tair };//8帧/s 校准参数
                    break;
                case 100:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 82, tair };//10帧/s 校准参数
                    break;
                case 120:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 83, tair };//12帧/s 校准参数
                    break;
                case -1:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 48, tair };//仅上传AD值
                    break;
                case 128:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 128, tair };//仅上传AD值
                    break;
                default:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 33, tair };//8帧/s 启动
                    break;
            }


            UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 9001));
           // IPAddress remoteIP = IPAddress.Parse("255.255.255.255");  //以广播方式发送
            IPAddress remoteIP = IPAddress.Parse("192.168.1.255");
            int remotePort = 8008;
            IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);






            //sendString = Console.ReadLine();
            //sendData = Encoding.Default.GetBytes(sendString);


            //client = new UdpClient();
            //将数据发送到远程端点 
            client.Send(Data, Data.Length, remotePoint);
            //关闭连接
            client.Close();
            


        }


        public static void CommandUp_v2(int frame)
        {
            //string sendString = null;//要发送的字符串 
            byte[] Data = new byte[8];


            switch (frame)
            {
                case 41:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 65, 00 };//停止
                    break;
                case 42:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 66, 00 };//6帧/s 启动
                    break;
                case 43:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 67, 00 };//8帧/s 启动
                    break;
                default:
                    Data = new byte[] { 22, 144, 87, 235, 00, 00, 66, 00 };//8帧/s 启动
                    break;
            }


            UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 9001));
            //广播发送指令
            IPAddress remoteIP = IPAddress.Parse("255.255.255.255");
            int remotePort = 8008;
            //实例化广播
            IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);


            //sendString = Console.ReadLine();
            //sendData = Encoding.Default.GetBytes(sendString);


            //client = new UdpClient();
            //将数据发送到远程端点 
            client.Send(Data, Data.Length, remotePoint);
            //关闭连接
            client.Close();


        }
    }


    public class DataProcess
    {
        string deviceIp = "";                                       //太赫兹设备IP
        const int devicePort = 8008;                                //太赫兹设备发送端口
        int localPort = new int();                                  //本机接收端口


        Queue> DataQueue = new Queue>();  //数据缓存队列
        Semaphore TaskSemaphore = new Semaphore(0, 2560);           //数据缓存队列缓存区
        object ThreadLock = new object();                           //线程


        public Action ShowEvent;
        public Action ShowEvent_zyr1;
        public Action ShowEvent_zyr2;
       // public Action ShowEvent_zyr3;
        //MainForm mf = new MainForm();




        DataCalculate dc = new DataCalculate();




        #region 线程开启
        /// 
        /// 所有线程开启函数
        /// 
        public void Start()
        {
            StartAllThread();
        }


        /// 
        /// 开启子线程
        /// 
        private void StartAllThread()
        {
            StartDataRevThread();
            StartDataProcessThread();
            StartShowThread();
        }


        private void StartShowThread()
        {
            Thread t = new Thread(new ThreadStart(ShowAllVariables));                  //开启DataRevThread
            t.Name = "ShowAllVariables";                                            //线程名字
            t.Start();
            t.IsBackground = true;
        }


        /// 
        /// 开启数据接收线程
        /// 
        private void StartDataRevThread()
        {


            Thread t = new Thread(new ThreadStart(DataRevThread));                  //开启DataRevThread
            t.Name = "DataRevThread";                                            //线程名字
            t.Start();
            t.IsBackground = true;                                                  //后台运行
        }


        /// 
        /// 数据处理线程开启
        /// 
        private void StartDataProcessThread()
        {
            Thread t = new Thread(new ThreadStart(DataDecodeImageProcessThread));  //开启DataDecode_ImageProcessThread
            t.Name = "DataDecode_ImageProcessThread";                                            //线程名字
            t.Start();
            t.IsBackground = true;                                                  //后台运行
        }
        #endregion


        /// 
        /// 构造函数
        /// 
        public DataProcess(string deviceip, int localport)
        {
            deviceIp = deviceip;//192.168.1.120
            localPort = localport;//8008
        }




        /*Thread t = new Thread(Start);
           t.Priority = ThreadPriority.Highest;
           t.Start();*/


        #region 数据接收线程
        /// 
        /// 数据接收线程
        /// 
        private void DataRevThread()
        {
            
            try
            {
                //IPAddress remoteIP = IPAddress.Parse("255.255.255.255");                                      //广播




                UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.1.119"), localPort));   //本机端口 一般UdpClient client = new UdpClient();
                IPAddress remoteIP = IPAddress.Parse(deviceIp);                                                   //远程IP
                int remotePort = devicePort;                                                                   //远程端口
                IPEndPoint endpoint = new IPEndPoint(remoteIP, remotePort);                                 //远程IP和端口
                client.Client.ReceiveBufferSize = 40960;//40960 默认值是8192
                //ARP触发
                client.Send(new byte[] { 00, 11 }, 2, endpoint);   //发送 00 ,11有何作用??






                while (true)
                    {
                      // MainForm f1 = new MainForm();
                      //f1.ShowThread("123");
                      List Taskbuff = new List();
                          for (int i = 0; i < 25; i++)                             //为什么是21?
                        {


                            Byte[] recv = client.Receive(ref endpoint);
                            // MainForm f1 = new MainForm();
                            // f1.ShowThread();
                            Taskbuff.Add(recv);




                       
                        DataShow_v1(recv, Environment.CurrentDirectory + "\\bin", "mydata.bin");






                        // writerFile(recv, Environment.CurrentDirectory + "\\bin\\mydata.bin");


                    }
                    // 任务队列为临界资源,需要锁 
                    lock (ThreadLock)
                        {
                            DataQueue.Enqueue(Taskbuff); //Queue> DataQueue = new Queue>();  在队列的末端添加元素


                        }
                        // 每添加一个任务,信号量加1
                        TaskSemaphore.Release(1);


                        //ChangeProducerText(String.Format("Consumer 1 take Task {0}\r\n", a));




                    }
                


                //client.Close();
            }
            catch (Exception ex)
            {
                string fileName = "Log\\debug" + localPort + "_DataDecode.txt";
                string content = DateTime.Now.ToLocalTime() + ex.Message + "\n" + ex.StackTrace + "\r\n";
                Logger(fileName, content);
            }


        }
        #endregion




        #region 数据显示
        private void DataShow_v1(Byte[] recv,string filePath,string fileName)
        {
            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            if (!File.Exists(filePath+ "\\"+fileName))
                File.Create(filePath + "\\"+fileName).Close(); //.Close 很关键,不然会有问题
            FileStream fs = new FileStream(filePath + "\\"+fileName, FileMode.Append, FileAccess.Write);
            fs.Write(recv, 0, recv.Length);
            fs.Close();
            fs.Dispose();
        }
            private void DataShow(Byte[] recv)
        {
            //MainForm f1 = new MainForm();
            // ShowMessage(f1.richTextBox1, "开始显示数据");
            //  MainForm f1 = new MainForm();
            //f1.ShowThread("123");
            /*  int nLength = recv.Length;//字节数组长度
              string sByte = "";
              if (nLength > 0)
              {
                  sByte = Convert.ToString(recv[0], 16);//转换成16进制
                  if (nLength > 1)
                  {
                      for (int k = 1; k < recv.Length; k++)
                      {
                          sByte += ",";//用逗号隔开
                          sByte += Convert.ToString(recv[k], 16);//转换成16进制


                      }
                  }
              }
              if (!Directory.Exists(Environment.CurrentDirectory + "\\bin"))
                  Directory.CreateDirectory(Environment.CurrentDirectory + "\\bin");


              //Environment.CurrentDirectory + "\\" + DateTime.Now.ToString("HHmmssfff") + "bin.txt"
              // writerFile(strToToHexByte(sNeed.ToString().Substring(0)), "ZHEN\\Port" + localPort + "_Frame" + (CountNum) % 10000 + ".bin");
              using (StreamWriter file = new StreamWriter(Environment.CurrentDirectory + "\\bin\\" + DateTime.Now.ToString("HHmmss") + ".txt", true))
              {
                  file.WriteLine(sByte);
                  file.WriteLine("\r\n");


              }*/


            //if (!Directory.Exists(Environment.CurrentDirectory + "\\bin"))
            //    Directory.CreateDirectory(Environment.CurrentDirectory + "\\bin");


            //Stream flstr = new FileStream(Path.GetDirectoryName(Application.ExecutablePath))
            // BinaryWriter sw = new  BinaryWriter(new FileStream(Environment.CurrentDirectory + "\\bin\\mydata.bin", FileMode.Append, FileAccess.ReadWrite)); //创建文件  bin目录下
            //  sw.Write(recv);
            //  sw.Close();


            //writerFile(recv, Environment.CurrentDirectory + "\\bin\\mydata.bin");
            int nLength = recv.Length;//字节数组长度
             string sByte = "";
             if (nLength > 0)
             {
                 sByte = Convert.ToString(recv[0], 16);//转换成16进制
                 if (nLength > 1)
                 {
                     for (int i = 1; i < recv.Length; i++)
                     {
                         sByte += ",";//用逗号隔开
                         sByte += Convert.ToString(recv[i], 16);//转换成16进制


                     }
                 }
             }
            if (!File.Exists(Environment.CurrentDirectory +"\\bin.txt"))
                File.Create(Environment.CurrentDirectory + "\\bin.txt");
            //Environment.CurrentDirectory + "\\" + DateTime.Now.ToString("HHmmssfff") + "bin.txt"
            using (StreamWriter file = new StreamWriter(Environment.CurrentDirectory + "\\bin.txt", true))
            {
                file.WriteLine(DateTime.Now.ToString("HHmmssfff")+sByte);


            }


            //File.WriteAllBytes(Environment.CurrentDirectory+"bin.txt", recv);
            //ShowMessage(f1.richTextBox1, sByte);


        }
        // 向RichTextBox中添加文本
        delegate void ShowMessageDelegate(RichTextBox txtbox, string message);
        private void ShowMessage(RichTextBox txtbox, string message)
        {
            if (txtbox.InvokeRequired)
            {
                ShowMessageDelegate showMessageDelegate = ShowMessage;
                txtbox.Invoke(showMessageDelegate, new object[] { txtbox, message });
            }
            else
            {
                txtbox.Text += message + "\r\n";
            }
        }
        #endregion


        #region 数据解码图像处理线程
        private int CountNum = 0;
        //private int ErrorNum = 0;
        public bool getMeanMatFlag;
        //private string WenduC = "";
        //private string str2 = "";
        Stopwatch elapsetime = new Stopwatch();




        /// 
        /// 数据解码图像处理
        /// 
        private void DataDecodeImageProcessThread()
        {
            try
            {
                List GetTask = new List();  //数据缓存队列
                List listBuff = new List(); //多余数据缓存区
                THZData thzdata = new THZData();               //Thz数据


                while (true)
                {
                    //接收数据
                    TaskSemaphore.WaitOne();                //等待接收队列




                    lock (ThreadLock)                       //锁线程  
                    {
                        GetTask = DataQueue.Dequeue();   //Queue> DataQueue = new Queue>();
                    }


                    string[] duration = new string[3] { "", "", "" };
                    elapsetime.Restart();//计时开始
                    //数据解析解码
                    //DataDecode(GetTask, ref listBuff, ref thzdata);
                    


                    DataDecode_v1(GetTask, ref listBuff, ref thzdata);
                    //Console.WriteLine("123");




                    elapsetime.Stop();//计时结束
                    duration[0] = elapsetime.ElapsedMilliseconds.ToString("000");
                    //背景校准
                    //if (getMeanMatFlag)
                    //{
                    //    if (frame.UserData != null)
                    //        MeanMatList.Add(frame);


                    //    if (MeanMatList.Count == 10)
                    //    {
                    //        MeanMat = getMeanMat(MeanMatList);
                    //        MeanMatList.Clear();
                    //        getMeanMatFlag = false;
                    //    }
                    //    ImageClass.MeanMat = MeanMat;
                    //}
                    elapsetime.Restart();//计时开始




                    thzdata.StartImageProcess();
                    if (thzdata.FilterImage != null)   ///FilterImage滤波后图像
                    {
                        if (!Directory.Exists("ZHEN"))
                            Directory.CreateDirectory("ZHEN");


                        thzdata.FilterImage.Mat.Bitmap.Save("ZHEN\\" + DateTime.Now.ToString("HHmmssfff") + ".bmp");
                    }


                    elapsetime.Stop();//计时结束
                    duration[1] = elapsetime.ElapsedMilliseconds.ToString("0000");
                    duration[2] = (Convert.ToInt32(duration[0]) + Convert.ToInt32(duration[1])).ToString("0000");
                    //温度显示
                    double[] temparture = thzdata.GetTemparture();
                    if (thzdata.FinalImage != null)
                    {
                        ShowData SD = new ShowData(thzdata.FinalImage.Bitmap, temparture, duration, thzdata.isPeople, thzdata.isHidden);//thzdata.isPeople


                        // 任务队列为临界资源,需要锁 
                        lock (ThreadLock1)
                        {
                            ShowQueue.Enqueue(SD);


                        }
                        // 每添加一个任务,信号量加1
                        TaskSemaphore1.Release(1);
                    }
                    //if (ShowEvent != null && thzdata.FinalImage != null)
                    //    ShowEvent(thzdata.FinalImage.Bitmap, duration, Temparture, thzdata.isPeople);
                    //ShowEvent(PB1Image, ImageClass.lImage, ImageClass.isPeople, elapsetime.ElapsedMilliseconds.ToString(), ImageClass.HideGoods, "错帧数:" + ErrorNum);
                    //ShowTextEvent.Invoke(ImageClass.OutArray.Bitmap);




                }
            }
            catch (Exception ex)
            {
                string fileName = "Log\\debug" + localPort + "_DataDecode.txt";
                string content = DateTime.Now.ToLocalTime() + ex.Message + "\n" + ex.StackTrace + "\r\n";
                Logger(fileName, content);
            }


        }


        Queue ShowQueue = new Queue();  //数据缓存队列
        Semaphore TaskSemaphore1 = new Semaphore(0, 2560);  //数据缓存队列缓存区 
                                                           //第一个就是信号量的内部整数初始值,也就是初始请求数,第二个参数就是最大请求数。


        object ThreadLock1 = new object();
        private void ShowAllVariables()
        {
            ShowData GetTask = new ShowData();  //数据缓存队列
            while (true)
            {
                //接收数据
                TaskSemaphore1.WaitOne();                //等待接收队列
                lock (ThreadLock1)                       //锁线程  
                {
                    GetTask = ShowQueue.Dequeue();
                }


                if (GetTask.ThzImage != null)
                {
                    if (!Directory.Exists("ZHEN"))
                        Directory.CreateDirectory("ZHEN");




                }


                if (ShowEvent != null && GetTask.ThzImage != null)
                    ShowEvent(GetTask.ThzImage, GetTask.Duration, GetTask.Temparture, GetTask.IsPeople, GetTask.IsHidden);


            }
        }


        public void Correct()
        {
            THZData.Recorrect = true;
        }


        //public void SetMirror(bool mirrorFlag)
        //{
        //    thzdata.MirrirFlag = mirrorFlag;
        //}
        #endregion


        /// 
        /// 数据解码
        /// 
        /// List
        /// List
        /// 输出PartData
        /// FrameLength
        /// PackageNum
        void DataDecode(List GetTask, ref List listBuff, ref THZData frame)
        {
            int PackageNum = 25;
            //if (listBuff.Count >= 100)
            //{
            //    listBuff.Clear();
            //    return;
            //}
            //排除异常项
            listBuff.RemoveAll(s => s.Count() < 1000);


            StringBuilder sNeed = new StringBuilder();


            GetTask.AddRange(listBuff);
            listBuff.Clear();


            foreach (var item in GetTask.OrderBy(s => 256 * s[6] + s[7]).GroupBy(s => 256 * 256 * 256 * s[2] + 256 * 256 * s[3] + 256 * s[4] + s[5]))
            {
                //PackageNum = 256 * item.ToList()[0][0] + item.ToList()[0][1];
                if (item.Count() == PackageNum)
                {


                    var Zhendata = item.OrderBy(s => s[6] * 256 + s[7]).ToList();


                    if (Zhendata[0].Count() != 1420)
                        continue;


                    foreach (Byte[] match in Zhendata)
                        sNeed.Append(BitConverter.ToString(match).Replace("-", "").Substring(20).ToUpper());
                    //if (!Directory.Exists("ZHEN"))
                    //    Directory.CreateDirectory("ZHEN");
                    //writerFile(strToToHexByte(sNeed.ToString().Substring(0)), "ZHEN\\Port" + localPort + "_Frame" + (CountNum) % 10000 + ".bin");
                    CountNum++;
                    frame.Init(sNeed.ToString());//???
                    //DecodeFrame(sNeed, ref frame, indexHead, indexTair); 


                    sNeed.Clear();
                }
                else
                {
                    listBuff.AddRange(item.ToList());
                }
            }


        }
        /// 
        /// 数据解码
        /// 
        /// List
        /// List
        /// 输出PartData
        /// FrameLength
        /// PackageNum
        void DataDecode_v1(List GetTask, ref List listBuff, ref THZData frame)   //ref作用?
        {
            int PackageNum = 25;
            //if (listBuff.Count >= 100)
            //{
            //    listBuff.Clear();
            //    return;
            //}
            //排除异常项
            listBuff.RemoveAll(s => s.Count() < 1000);


            StringBuilder sNeed = new StringBuilder();


            GetTask.AddRange(listBuff);
            listBuff.Clear();
           


            foreach (var item in GetTask.OrderBy(s => 256 * s[6] + s[7]).GroupBy(s => 256 * 256 * 256 * s[2] + 256 * 256 * s[3] + 256 * s[4] + s[5])) //按照包排序、按照帧分组,每次取出一帧
            {


                PackageNum = 256 * item.ToList()[0][0] + item.ToList()[0][1];//获取一帧前两个字节的值,即单帧包数
                if (item.Count() == PackageNum)//如果一帧的包数量和PackageNum相等则是一个完整的包
                {


                    var Zhendata = item.OrderBy(s => s[6] * 256 + s[7]).ToList();//一帧数据按照包排序后存放到Zhendata


                    if (Zhendata[0].Count() != 1420)//第一包不是1420则退出
                        continue;


                    //dc.tempartureData_zyr(Zhendata, ShowEvent_zyr2);
                    dc.adDataCaculate_zyr(Zhendata, ShowEvent_zyr1,ShowEvent_zyr2);


                    foreach (Byte[] match in Zhendata)  //每次取出一个包也就是1420字节,取一帧的数据
                        sNeed.Append(BitConverter.ToString(match).Replace("-", "").Substring(20).ToUpper());
                    //if (!Directory.Exists("ZHEN"))
                    //    Directory.CreateDirectory("ZHEN");
                    writerFile(strToToHexByte(sNeed.ToString().Substring(0)), "ZHEN\\Port" + localPort + "_Frame" + (CountNum) % 10000 + ".bin");
                    CountNum++;
                    frame.Init(sNeed.ToString());
                    //DecodeFrame(sNeed, ref frame, indexHead, indexTair); 


                    sNeed.Clear();
                }
                else
                {
                    listBuff.AddRange(item.ToList());
                }
            }


        }
        void DataDecode_v2(List GetTask, ref List listBuff, ref THZData frame)
        {
            int PackageNum = 25;
            //int ChannelCount = 36;
            //int SampleCount = 473;
     
            listBuff.RemoveAll(s => s.Count() < 1000);


            StringBuilder sNeed = new StringBuilder();


            GetTask.AddRange(listBuff);
            listBuff.Clear();


            foreach (var item in GetTask.OrderBy(s => 256 * s[6] + s[7]).GroupBy(s => 256 * 256 * 256 * s[2] + 256 * 256 * s[3] + 256 * s[4] + s[5])) //按照包排序、按照帧分组
            {
                PackageNum = 256 * item.ToList()[0][0] + item.ToList()[0][1];
                if (item.Count() == PackageNum)
                {


                    var Zhendata = item.OrderBy(s => s[6] * 256 + s[7]).ToList();


                    if (Zhendata[0].Count() != 1420)
                        continue;


                    /*mycode*/
                    /* List frameByteData = new List();
                     double[] ChannelData = new double[438] ;
                     double avg = 0, Variance;
                     foreach (Byte[] Package in Zhendata)
                         foreach (Byte byt in Package)
                             frameByteData.Add(byt);
                     MessageBox.Show(frameByteData.Count().ToString());
                     ChannelCount = 256 * item.ToList()[0][26] + item.ToList()[0][27];
                     SampleCount = 256 * item.ToList()[0][28] + item.ToList()[0][29];
                     for (int i = 0; i < ChannelCount; i++)    // 36
                     {
                         for (int j = 0; j < SampleCount; j++)  // 438
                             ChannelData[j]= frameByteData[j * 74 + 51+i] * 256 + frameByteData[j * 74 + 52+i];
                         avg = Var(ChannelData);
                         Variance=ChannelData.Average();
                         strWrite(ChannelData+"--"+avg+"--"+Variance, Environment.CurrentDirectory + "\\bin","ad.txt");


                     }*/


                    




                    foreach (Byte[] match in Zhendata)
                        sNeed.Append(BitConverter.ToString(match).Replace("-", "").Substring(20).ToUpper());


                    strToToHexByte(sNeed.ToString().Substring(0));




                    //if (!Directory.Exists("ZHEN"))
                    //    Directory.CreateDirectory("ZHEN");
                    writerFile(strToToHexByte(sNeed.ToString().Substring(0)), "ZHEN\\Port" + localPort + "_Frame" + (CountNum) % 10000 + ".bin");
                    CountNum++;
                    frame.Init(sNeed.ToString());
                    //DecodeFrame(sNeed, ref frame, indexHead, indexTair); 


                    sNeed.Clear();
                }
                else
                {
                    listBuff.AddRange(item.ToList());
                }
            }


        }




        /* public void callBack_zyr1(int row, int column, string str)
         {


            ShowEvent_zyr1(row, column, str);
          }
     public void callBack_zyr2(string str)
     {


         ShowEvent_zyr2(str);
     }*/
        #region mycode_zyr
        // DataProcess dp = null;
        //public void tempartureData_zyr(List Zhendata)
        //{
        //    StringBuilder sNeed = new StringBuilder();


        //    foreach (Byte[] match in Zhendata)
        //        sNeed.Append(BitConverter.ToString(match).Replace("-", "").Substring(20).ToUpper());
        //    double[] temparture = GetTemparture_zyr(sNeed.ToString());
        //    // mf.ShowlbDevTem(string.Format("设备温度:\nT1:{0:N}°\nT2:{1:N}°\nT3:{2:N}°\nT4:{3:N}°", temparture[0], temparture[1], temparture[2], temparture[3]));
        //    ShowEvent_zyr2(string.Format("设备温度:\nT1:{0:N}°\nT2:{1:N}°\nT3:{2:N}°\nT4:{3:N}°", temparture[0], temparture[1], temparture[2], temparture[3]));
        //   // dp.callBack_zyr2(string.Format("设备温度:\nT1:{0:N}°\nT2:{1:N}°\nT3:{2:N}°\nT4:{3:N}°", temparture[0], temparture[1], temparture[2], temparture[3]));
        //    sNeed.Clear();
        //    foreach (var tem in temparture)
        //        sNeed.Append(tem + "***");
        //    strWrite_zyr(sNeed.ToString(), Environment.CurrentDirectory + "\\bin", "tempartureData.txt");
        //    sNeed.Clear();




        //}


        public double[] GetTemparture_zyr(string str)
        {
            string tepStr = str.Substring(20 * 2, 8 * 2);
            double[] Temparture = new double[4];
            if (tepStr == null)
                return Temparture;
            byte[] Wen = DataProcess.strToToHexByte(tepStr);
            if ((Wen[0] & 0xf0) >> 4 == 15)
                Temparture[0] = -(~Wen[1] + 1 + 256.0 * ~Wen[0]) / 16;
            else
                Temparture[0] = (Wen[1] + 256.0 * Wen[0]) / 16;


            if ((Wen[2] & 0xf0) >> 4 == 15)
                Temparture[1] = -(~Wen[3] + 1 + 256.0 * ~Wen[2]) / 16;
            else
                Temparture[1] = (Wen[3] + 256.0 * Wen[2]) / 16;


            if ((Wen[4] & 0xf0) >> 4 == 15)
                Temparture[2] = -(~Wen[5] + 1 + 256.0 * ~Wen[4]) / 16;
            else
                Temparture[2] = (Wen[5] + 256.0 * Wen[4]) / 16;


            if ((Wen[6] & 0xf0) >> 4 == 15)
                Temparture[3] = -(~Wen[7] + 1 + 256.0 * ~Wen[6]) / 16;
            else
                Temparture[3] = (Wen[7] + 256.0 * Wen[6]) / 16;
            return Temparture;
        }
        int zhenRows = 0;
       /* public void adDataCaculate_zyr(List Zhendata)
        {
            byte[] byteData = new byte[35250];//1410*25
            int count1 = 0, count2 = 0;
            StringBuilder sNeed = new StringBuilder();
            MainForm mf = new MainForm();


            foreach (Byte[] Package in Zhendata)
            {
                count1 = 0;
                foreach (byte byt in Package)
                {
                    if (count1 >= 20)   //跳过第一个数
                    {


                        byteData[count2] = byt;
                        count2++;


                    }
                    count1++;
                }
            }
            // Console.ReadKey();
            double[] channel = new double[473];
            double variance = 0.0, average = 0.0;
            for (int i = 0; i < 36; i++)
            {
                for (int j = 0; j < 473; j++)
                {
                    byte bigByte = Convert.ToByte(byteData[j * 74 + i * 2].ToString("X"), 16);
                    if ((bigByte & 0xf0) >> 4 == 15)
                        channel[j] = -(~byteData[j * 74 + i * 2 + 30] + 1 + 256.0 * ~byteData[j * 74 + i * 2 + 31]) / 8192.0 * 10.0;//2^12 =4096
                    else
                        channel[j] = (byteData[j * 74 + i * 2 + 30] + 256.0 * ~byteData[j * 74 + i * 2 + 31]) / 8192.0 * 10.0;
                    sNeed.Append(channel[j] + ",");
                }
                variance = Var_zyr(channel);
                //mf.dataShow(zhenRows, 2 * i + 1, variance.ToString());
                ShowEvent_zyr1(zhenRows, 2 * i + 1, variance.ToString());
                //dp.callBack_zyr1(zhenRows, 2 * i + 1, variance.ToString());
                sNeed.Append("***variance:" + variance + "***average:");
                average = channel.Average();
                // mf.dataShow(zhenRows, 2 * i , average.ToString());
                 ShowEvent_zyr1(zhenRows, 2 * i, average.ToString());
                //dp.callBack_zyr1(zhenRows, 2 * i, average.ToString());
                sNeed.Append(average);
                strWrite_zyr(sNeed.ToString(), Environment.CurrentDirectory + "\\bin", "channelData.txt");
                sNeed.Clear();
            }
            zhenRows++;
        }*/


        public double Var_zyr(double[] v)
        {
            double sum1 = 0;
            for (int i = 0; i < v.Length; i++)
            {
                double temp = v[i] * v[i];
                sum1 = sum1 + temp;
            }


            double sum = 0;
            foreach (double d in v)
            {
                sum = sum + d;
            }
            double var = sum1 / v.Length - (sum / v.Length) * (sum / v.Length);
            return var;
        }
        private int rowCount = 0;
        private void strWrite_zyr(string str, string filePath, string fileName)
        {


            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            if (!File.Exists(filePath + "\\" + fileName))
                File.Create(filePath + "\\" + fileName).Close(); //.Close 很关键,不然会有问题
            if (rowCount < 3600)
            {
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, true);//true 追加数据
                sw.WriteLine(str);
                sw.Close();
                rowCount++;
            }
            else
            {
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, false);
                sw.WriteLine(str);
                sw.Close();
                rowCount = 0;
            }
        }
        #endregion 
        private void strWrite(string str, string filePath, string fileName)
        {
            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            if (!File.Exists(filePath + "\\" + fileName))
                File.Create(filePath + "\\" + fileName).Close(); //.Close 很关键,不然会有问题


            //方法一
            StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, true);
            sw.WriteLine(str);
            sw.Close();


            //方法2
           /* string path = "D\1.txt";//文件的路径,保证文件存在。
            FileStream fs = new FileStream(path, FileMode.Append);
            SteamWriter sw = new StreamWriter(fs);
            sw.WriteLine(要追加的内容);
            sw.Close();
            fs.Close();*/
        }


       
        /// 
        /// 字符串转16进制Byte字节
        /// 
        /// 输入字符串
        /// 转化的Byte字节
        public static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace("-", "");
            if ((hexString.Length % 2) != 0)
                hexString += "20";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }


        /// 
        /// 帧数据读取,存放
        /// 
        /// 帧数据数组
        /// 存放文件
        private void writerFile(byte[] array, string strPath)
        {
            //string content = this.txtContent.Text.ToString();


            if (string.IsNullOrEmpty(strPath))
            {
                return;
            }


            //将string转为byte数组
            //byte[] array = Encoding.UTF8.GetBytes(content);


            //string path = Server.MapPath("/test.txt");
            //创建一个文件流
            FileStream fs = new FileStream(strPath, FileMode.Create);


            //将byte数组写入文件中
            fs.Write(array, 0, array.Length);
            //所有流类型都要关闭流,否则会出现内存泄露问题
            fs.Close();
            //Response.Write("保存文件成功");
        }


        private void Logger(string fileName, string content)
        {
            //StreamWriter sw = new StreamWriter(fileName, true);
            //sw.Write(content);
           // sw.Close(); sw.Dispose();


        }
    }


    class ShowData
    {
        public Bitmap ThzImage;
        public string[] Duration;
        public double[] Temparture;
        public bool IsPeople;
        public bool IsHidden;
        public ShowData() { }
        public ShowData(Bitmap bitmap, double[] temparture, string[] duration, bool peopleFlag, bool hidFlag)
        {
            ThzImage = bitmap;
            IsPeople = peopleFlag;
            IsHidden = hidFlag;
            Duration = duration;
            Temparture = temparture;
        }
    }
}

类THZData.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.Util;
using System.Drawing;
using System.Threading.Tasks;


namespace ThzData
{
    public class THZData
    {
        private FrameMsg frameMsg;
        private Byte[] frameData;
        private int frameLength;
        private int ImageHeight;
        private int oriImageWidth;


        public bool isPeople;                   //人物判断
        public bool isHidden;
        private Matrix backgroundframe;    //判定中间背景帧
        private List> multiImageList;//多帧平均列表
        public Matrix PreProcessImage;  //预处理后图像
        public Matrix AlignImage;         //校准后图像
        public Matrix MeanMat;             //输入的背景均值矩阵
        public Matrix FilterImage;        //滤波后图像
        public Mat FinalImage;                  //最终图像
        //public Dictionary dictRects;//可疑块目标框架
        //public Dictionary dictCoutours;//可疑轮廓内部信息
        public List listRects;
        public bool MirrirFlag;
        public static bool Recorrect = false;
        int count = 0;


        const int JBianNum = 13;
        const int iHumanThreshold = 35;
        const int resizeImageWidth = 183;




        public THZData()
        {
            this.frameMsg = new FrameMsg();
            this.isPeople = false;
            this.isHidden = false;
            this.multiImageList = new List>();


        }


        public void Init(string allMsg)
        {
            if (allMsg.Count() > 0 && frameMsg != null)
            {
                //this.frameMsg.Init(allMsg.Substring(8, 36 * 2));
                this.frameMsg.Init_v1(allMsg.Substring(8, 36 * 2));
                this.ImageHeight = Convert.ToInt32(frameMsg.SampNum, 16); //BitConverter.ToInt32(Encoding.Default.GetBytes(frameMsg.SampNum), 0);
                this.oriImageWidth = Convert.ToInt32(frameMsg.ChannelNum, 16);//16进制str转int
                this.frameLength = Convert.ToInt32(frameMsg.DataLength, 16);// BitConverter.ToInt32(Encoding.Default.GetBytes(frameMsg.DataLength), 0);
                this.frameData = strToToHexByte(allMsg.Substring(80, frameLength * 2));//frameLength,28860


                if (MeanMat == null)
                    this.MeanMat = new Matrix(new Size(1, ImageHeight * oriImageWidth));


                this.backgroundframe = new Matrix(new Size(1, ImageHeight * oriImageWidth));
                this.PreProcessImage = new Matrix(new Size(1, (ImageHeight - JBianNum) * oriImageWidth));
                this.AlignImage = new Matrix(new Size(resizeImageWidth * 2, (ImageHeight - JBianNum) * 2));
                this.FinalImage = new Mat();
                this.FilterImage = new Matrix(new Size(resizeImageWidth * 2, (ImageHeight - JBianNum) * 2));
                //this.dictRects = new Dictionary();
                //this.dictCoutours = new Dictionary();
                this.listRects = new List();


            }
        }


        /// 
        /// 开始处理数据
        /// 
        /// PartData
        public void StartImageProcess()
        {
            if (this.frameData == null)
                return;


            this.isHidden = false;
            //获取畸变矫正后初始矩阵
            getInitMatrix();


            //加权归一化
            Normalization();


            if (isPeople)
            {
                //滤波,分割,识别
                Filter();
                Seg_Reg();
            }
            else
            {
                CvInvoke.CvtColor(AlignImage, this.FinalImage, ColorConversion.Gray2Bgr);//Outputtemp,BoxArray
            }
            //CvInvoke.CvtColor(AlignImage, this.FinalImage, ColorConversion.Gray2Bgr);//Outputtemp,BoxArray
        }


        /// 
        /// 获取畸变矫正后初始矩阵
        /// 
        /// Matrix
        private void getInitMatrix()
        {
            Matrix Data = new Matrix(this.frameData);    //Byte转Matrix
            //临时变量temp1,temp2和temp3
            int Height = this.ImageHeight;
            int Width = this.oriImageWidth;
            Matrix temp1 = new Matrix(new Size(1, Height * Width));
            Matrix temp2 = new Matrix(new Size(1, Height * Width));
            //Matrix temp3 = new Matrix(new Size(1, Height * Width));


            //获取矩阵,temp1
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    //temp[i * n + j, 0] = (256 * msImg[i * 2 * (n + 1) + 2*j, 0] + msImg[i * 2 * (n + 1) + 2*j + 1, 0])/16;
                    temp1[i + j * Height, 0] = (256 * Data[i * 2 * (Width + 1) + 2 * j, 0] + Data[i * 2 * (Width + 1) + 2 * j + 1, 0]);
                }
            }


            //判断是否有人
            double MMMAX = 0;
            for (int j = 0;
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章