Demo entry 5366371

22333

   

Submitted by anonymous on Jun 20, 2016 at 10:54
Language: C#. Code size: 10.6 kB.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using System.Collections.Generic;
using ImageGen;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace Com
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 变量初始定义
        /// </summary>
        const int N = 1052640;
        const int raw_N = N*5/4;
        const int real_N = wid*hei;
        public const int wid = 1024; 
        public const int hei = 1024;
        const int pnum = 20;
        List<int[]> result500 = new List<int[]>();
        List<int[]> result800 = new List<int[]>();
        SerialPort ComPort = new SerialPort();
        StreamWriter sw;
        public bool special = false;
        public double[,] imagg = new double[2*wid, 2*hei];
        public Class1 cls = new Class1();
        public MWArray pic500 = (MWNumericArray)new double[2 * wid,2 * hei];
        public MWArray pic800 = (MWNumericArray)new double[2 * wid, 2 * hei];

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 对各控件构成的初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            clb500.Items.Clear();
            cboBaudRate.Items.Add(3686400);
            cboBaudRate.SelectedIndex = 0;
            pgbData.Minimum = 0;
            pgbData.Maximum = raw_N;
        }

        /// <summary>
        /// 串口接收到数据时触发的委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (ComPort.BytesToRead >= raw_N)//图像数据充分传输完毕,开启处理模式
            {
                byte[] raw_data = new byte[raw_N];//原本的8位数据,需要进一步剪裁和拼接
                byte[] rec_data = new byte[N]; //接收到的8位图像数据,还有行号
                int[] res_i = new int[N];
                DateTime date = DateTime.Now;
                ComPort.Read(raw_data, 0, raw_N);
                sw = File.CreateText(date.Minute.ToString() + date.Second.ToString() + ".txt");
                for (int x = 0; x < raw_N / 5; x++)
                {
                    string buffer = string.Empty; //初始化缓存字符串
                    for (int y = 0; y < 5; y++)
                    {
                        buffer = string.Concat(Convert.ToString(raw_data[5 * x + y], 2).PadLeft(8, '0'), buffer);
                    }
                    string[] buf = new string[4] { buffer, buffer, buffer, buffer };//暂时存储待处理数据
                    string[] imdata = new string[4];
                    for (int im = 0; im < 4; im++)
                    {
                        imdata[im] = buf[im].Substring(30 - 10 * im, 10);//40bit字符串产生对应10bit实际数据
                        res_i[4 * x + im] = Convert.ToInt32(imdata[im], 2);//生成供Matlab处理的数据
                        sw.WriteLine(res_i[4 * x + im]);//将数据存储到txt文件,便于观察和研究
                        rec_data[4 * x + im] = Convert.ToByte(imdata[im].Substring(0, 8), 2);
                    }
                    buffer = string.Empty;
                }
                sw.Close();
                ComClose();
                if (rbn500.Checked)
                {
                    result500.Add(res_i);
                    tslStatus.Text = "500mm第" + result500.Count + "张照片已取得";
                }
                else
                {
                    result800.Add(res_i);
                    tslStatus.Text = "800mm第" + result800.Count + "张照片已取得";
                }
                clb500.BeginInvoke(new MethodInvoker(
                    delegate
                    {
                        byte[] pro_data = DataConv(rec_data);
                        Bitmap bmpp = PicConvert(pro_data,wid,hei);
                        PicSave(bmpp);
                        btnWrite.Enabled = true;
                    }));
                ComOpen();
            }
            else
            {
                pgbData.BeginInvoke(new MethodInvoker(
                    delegate
                    {
                        pgbData.Value = Math.Min(ComPort.BytesToRead,raw_N);
                    }));
            }
        }

        /// <summary>
        /// 电脑控制拍照
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWrite_Click(object sender, EventArgs e)
        {
            
            try
            {
                ComPort.Write(new byte[1] { 0 }, 0, 1);
                btnWrite.Enabled = false;
                tslStatus.Text = "拍照中……";
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 调用Maltab进行主要的图像处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChoose_Click(object sender, EventArgs e)
        {
            try
            {
                if (rbn500.Checked)
                {
                    pic500 = Transfer(clb500, result500);
                }
                else
                {
                    pic800 = Transfer(clb800, result800);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }


        /// <summary>
        /// 清空所有图片缓存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            result500.Clear();
            result800.Clear();
            clb500.Items.Clear();
            clb800.Items.Clear();
        }

        /// <summary>
        /// 获取串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmGetPort_Click(object sender, EventArgs e)
        {
            string[] ArrayComPortsNames = null; // 可识别到的硬件串口
            ArrayComPortsNames = SerialPort.GetPortNames();
            if (ArrayComPortsNames.Length == 0)
                MessageBox.Show("请连接硬件串口", "提示");
            else
            {
                cboComPort.Items.Clear();
                cboComPort.Items.AddRange(ArrayComPortsNames);
                cboComPort.SelectedIndex = cboComPort.Items.Count - 1;
                tslStatus.Text = "串口" + ArrayComPortsNames[cboComPort.SelectedIndex] + "已取得";
            }
        }

        /// <summary>
        /// 展示小组名称与成员名字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmDisplay_Click(object sender, EventArgs e)
        {
            if (special)
            {
                special = false;
            }
            else if (tsmDisplay.Text == "Display Team")
            {
                Width += 340;
                tsmDisplay.Text = "Hide Team";
            }
            else
            {
                Width -= 340;
                tsmDisplay.Text = "Display Team";
            }
        }

        /// <summary>
        /// 开启关闭串口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmOpen_Click(object sender, EventArgs e)
        {
            //开启串口,初始化参数
            if (!ComPort.IsOpen)
            {
                ComPort.BaudRate = Convert.ToInt32(cboBaudRate.Text);
                ComPort.PortName = Convert.ToString(cboComPort.Text);
                ComOpen();
            }
            //关闭串口,初始化所有数据
            else
            {
                ComClose();
            }
            //建立接收委托
            ComPort.DataReceived += ComPort_DataReceived;
        }

        /// <summary>
        /// 确定主程序界面是否置顶
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chbTop_CheckedChanged(object sender, EventArgs e)
        {
            if (chbTop.Checked)
            {
                this.TopMost = true;
            }
            else
            {
                this.TopMost = false;
            }
        }

        /// <summary>
        /// 500mm图片全选功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chbAll500_CheckedChanged(object sender, EventArgs e)
        {
            if (chbAll500.Checked)
            {
                for(int c = 0;c < clb500.Items.Count; c++)
                {
                    clb500.SetItemChecked(c,true);
                }
            }
            else
            {
                for (int c = 0; c < clb500.Items.Count; c++)
                {
                    clb500.SetItemChecked(c, false);
                }
            }
        }

        /// <summary>
        /// 800mm图片全选功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chbAll800_CheckedChanged(object sender, EventArgs e)
        {
            if (chbAll800.Checked)
            {
                for (int c = 0; c < clb800.Items.Count; c++)
                {
                    clb800.SetItemChecked(c, true);
                }
            }
            else
            {
                for (int c = 0; c < clb800.Items.Count; c++)
                {
                    clb800.SetItemChecked(c, false);
                }
            }
        }

        /// <summary>
        /// 调用Matlab进行图像拼接和导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMatsave_Click(object sender, EventArgs e)
        {
            try
            {
                MWArray left = (MWNumericArray)Parameter(txbLeft);
                MWArray right = (MWNumericArray)Parameter(txbRight);
                cls.ImageSave(pic500,pic800,left,right);
                tslStatus.Text = "最终图像存储完毕";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).