基恩士CL3000激光的测试验证程序(C#)

CL3000是基恩士推出的同轴激光位移传感器。

相对于旧有型号的H025,H028红色点激光来说,还是有其优势的。具体可以参考使用手册。

这里勇哥只是附上测试用的代码,供大家参考。


image.png

using CL3_IF_DllSample;
using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public MotionCard dmc = new MotionCard();
        private int CurrentDeviceId = 0;
        bool netIsConnect = false;
        private const int MaxRequestDataLength = 512000;
        private const int MaxMeasurementDataCountPerTime = 100;
        private const int MaxSequenceMeasurementData = 100000;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CL3IF_ETHERNET_SETTING ethernetSetting = new CL3IF_ETHERNET_SETTING();
            ethernetSetting.ipAddress = new byte[4];
            var ipstr = textBox1.Text.Split('.');
            if (!byte.TryParse(ipstr[0], out ethernetSetting.ipAddress[0]))
            {
                MessageBox.Show(this, "ipAddress firstSegment is Invalid Value");
                return;
            }
            if (!byte.TryParse(ipstr[1], out ethernetSetting.ipAddress[1]))
            {
                MessageBox.Show(this, "ipAddress secondSegment is Invalid Value");
                return;
            }
            if (!byte.TryParse(ipstr[2], out ethernetSetting.ipAddress[2]))
            {
                MessageBox.Show(this, "ipAddress thirdSegment is Invalid Value");
                return;
            }
            if (!byte.TryParse(ipstr[3], out ethernetSetting.ipAddress[3]))
            {
                MessageBox.Show(this, "ipAddress fourthSegment is Invalid Value");
                return;
            }
            if (!ushort.TryParse(textBox2.Text, out ethernetSetting.portNo))
            {
                MessageBox.Show(this, "ipAddress Port number is Invalid Value");
                return;
            }

            ethernetSetting.reserved = new byte[2];
            ethernetSetting.reserved[0] = 0x00;
            ethernetSetting.reserved[1] = 0x00;
            int returnCode = NativeMethods.CL3IF_OpenEthernetCommunication(CurrentDeviceId, ref ethernetSetting, 10000);

            var f1 = dmc.Init();
            if (returnCode == 0)
            {
                netIsConnect = true && f1;
            }
            else
                netIsConnect = false;
            richTextBox1.AppendText (returnCode.ToString());
        }

        private void CommunicationClose(int deviceId)
        {
            int returnCode = NativeMethods.CL3IF_CloseCommunication(deviceId);

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            CommunicationClose(CurrentDeviceId);
        }

      


        private void button3_Click(object sender, EventArgs e)
        {
            //批量采集数据
            if (!netIsConnect) MessageBox.Show("先初始化激光!");
            var avg = getData(10).Average();
            richTextBox1.AppendText("\r\n"+avg.ToString("0.00000"));
        }

        private double calThick(double 陶瓷片A系数,double 陶瓷片标准值, double 上下激光头的和)
        {
            return 上下激光头的和 + 陶瓷片标准值 - 陶瓷片A系数;
        }

        /// <summary>
        /// 取激光数据
        /// </summary>
        /// <returns></returns>
        private List<double> getData(uint dataSum)
        {
            CL3IF_MEASUREMENT_DATA[] trendDataList = new CL3IF_MEASUREMENT_DATA[MaxSequenceMeasurementData];
            byte[] buffer = new byte[MaxRequestDataLength];
            uint nextIndex = 0;
            uint obtainedDataCount = 0;
            uint indexGet = 0;
            uint sum = dataSum;
            var resData = new List<double>();

            uint index = 0;
            int res1 = NativeMethods.CL3IF_GetTrendIndex(CurrentDeviceId, out index);
            if (res1 != NativeMethods.CL3IF_RC_OK)
            {
                throw new ArgumentNullException("error");
            }

            indexGet = index;
            CL3IF_OUTNO outTarget = 0;

            using (PinnedObject pin = new PinnedObject(buffer))
            {
                int res = -1;
                do
                {
                    res = NativeMethods.CL3IF_GetTrendData(CurrentDeviceId,
                        indexGet,
                        sum,
                        out nextIndex, out obtainedDataCount, out outTarget, pin.Pointer);
                    Thread.Sleep(20);
                } while (obtainedDataCount < sum);

                if (res != NativeMethods.CL3IF_RC_OK)
                {
                    throw new ArgumentNullException("error");
                }
                List<int> outTargetList = ConvertOutTargetList(outTarget);
                int readPosition = 0;
                int trendDataCount = 0;
                for (uint i = 0; i < obtainedDataCount; i++)
                {
                    CL3IF_MEASUREMENT_DATA measurementData = new CL3IF_MEASUREMENT_DATA();
                    measurementData.outMeasurementData = new CL3IF_OUTMEASUREMENT_DATA[outTargetList.Count];
                    measurementData.addInfo = (CL3IF_ADD_INFO)Marshal.PtrToStructure(pin.Pointer + readPosition, typeof(CL3IF_ADD_INFO));
                    readPosition += Marshal.SizeOf(typeof(CL3IF_ADD_INFO));
                    for (int j = 0; j < outTargetList.Count; j++)
                    {
                        measurementData.outMeasurementData[j] = (CL3IF_OUTMEASUREMENT_DATA)Marshal.PtrToStructure(pin.Pointer + readPosition, typeof(CL3IF_OUTMEASUREMENT_DATA));
                        readPosition += Marshal.SizeOf(typeof(CL3IF_OUTMEASUREMENT_DATA));
                    }

                    trendDataList[i] = measurementData;
                    var v1 = measurementData.outMeasurementData[0].measurementValue;
                    if (v1 < 0 || v1>60000)
                        resData.Add(0);
                    else
                        resData.Add(double.Parse("0." + v1.ToString()));
                    trendDataCount++;
                }
                return resData;
            }
        }

     
        private void OutputLogMessage(string msg)
        {
            richTextBox1.AppendText(msg);
        }

        private List<int> ConvertOutTargetList(CL3IF_OUTNO outTarget)
        {
            byte mask = 1;
            List<int> outList = new List<int>();
            for (int i = 0; i < NativeMethods.CL3IF_MAX_OUT_COUNT; i++)
            {
                if (((ushort)outTarget & mask) != 0)
                {
                    outList.Add(i + 1);
                }
                mask = (byte)(mask << 1);
            }
            return outList;
        }

        double 系数A = 0;
        private void button4_Click(object sender, EventArgs e)
        {
            //读陶瓷片A系数
            if (!netIsConnect) MessageBox.Show("先初始化激光!");
            系数A = 取陶瓷片系数(int.Parse(tbTcpPos.Text));
            richTextBox1.AppendText("\r\n" + 系数A.ToString("0.00000"));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //读测量物厚度
            if (!netIsConnect) MessageBox.Show("先初始化激光!");
            goSpecifyPT(int.Parse(tbClwPos.Text));
            Thread.Sleep(1000);
            var avg = getData(10).Average();
            richTextBox1.AppendText("\r\n" + calThick(系数A, 0.50, avg).ToString("0.0000"));
        }


        public double 取陶瓷片系数(int pos)
        {
            goSpecifyPT(pos);
            系数A = getData(10).Average();
            return 系数A;
        }

        public void ResetToHome()
        {
            dmc.Stop();
            //dmc.Home(0);
            dmc.Home(0, 0, 15, 3);
        }

        public void goSpecifyPT(int position)
        {
            dmc.SetMoveSpeed(0, 199000);
            dmc.A_Move(0, position);

            dmc.waitStop(0);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int pos = 0;
            int.TryParse(tbDis.Text, out pos);
            goSpecifyPT(pos);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ResetToHome();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            dmc.Stop(0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            //var ary1 = new List<double>();
            //for (int i = 0; i < 100;i++ )
            //{
            //    ary1.Add(i + 1);
            //}
            //saveSegmentData(ary1, 0.5, 10);
            //return;

            var dataAry = new List<double>();
            Stopwatch sb1 = new Stopwatch();
            float speed = 120000;
            float.TryParse(tbSpeed.Text, out speed);
            dmc.SetMoveSpeed(0, speed);
            var xs= 取陶瓷片系数(45);
            dmc.A_Move(0, 80);
            dmc.waitStop(0);
            dmc.A_Move(0, 430);

            sb1.Restart();
            for (int i = 0; i < 2; i++)
            {
                dataAry.AddRange(getData(7677));
            }
            var s1 = sb1.ElapsedMilliseconds;
            sb1.Stop();
            saveCSV(dataAry,xs,checkBox2.Checked,"go");
            dmc.waitStop(0);
            richTextBox1.AppendText("\r\n OK.");
            
        }


        /// <summary>
        /// 保存分段数据
        /// </summary>
        /// <param name="data">一个单边的数据</param>
        /// <param name="xs">系数A</param>
        /// <param name="ds">分段数量</param>
        private void saveSegmentData(List<double> data,double xs,int ds)
        {
            double d = 0;
            var ary1 = new List<double>();
            var resAry = new List<double>();
            foreach(var m in data)
            {
                d=calThick(xs, 0.5, m);
                if (d <= 0) continue;
                ary1.Add(d);
            }
            var s1 = ary1.Count / ds;
            var tmp1 = new List<double>();
            for(int i=0;i<ds;i++)
            {
                tmp1.Clear();
                for (int k = 0; k < s1; k++)
                {
                    tmp1.Add(ary1[(i*s1)+k]);
                }
                resAry.Add(getAverageNoZero(tmp1));
            }


            DateTime t = DateTime.Now;
            StringBuilder sb1 = new StringBuilder();

            var path = string.Format("D:\\segData_{0}.csv", t.Year + "_" + t.Month + "_" + t.Day);
            try
            {
                foreach (var m in resAry)
                {
                    sb1.Append(m.ToString("0.00000"));
                    sb1.Append(",");
                }
                sb1.Append(Environment.NewLine);
                File.AppendAllText(path, sb1.ToString());
            }
            catch (Exception ex)
            {

            }
        }

        private void saveCSV(List<double> data,double xs,bool f,string msg)
        {
            DateTime t=DateTime.Now;
            StringBuilder sb1 = new StringBuilder();
            
            var path = string.Format("D:\\data_{0}.csv",t.Year+"_"+t.Month+"_"+t.Day);
            try
            {
                sb1.Append(msg + ",");
                foreach (var m in data)
                {
                    if (f)
                    {
                        sb1.Append(calThick(xs, 0.5, m).ToString("0.00000"));
                     }
                    else
                    {
                        sb1.Append(m.ToString("0.00000"));
                    }
                    sb1.Append(",");
                }
                sb1.Append(Environment.NewLine);
                File.AppendAllText(path,sb1.ToString());
            }
            catch(Exception ex)
            {

            }
        }

        bool isRealDis = false;
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked && 系数A != 0)
            {
                timer1.Interval = 100;
                timer1.Enabled = true;
                isRealDis = true;
            }
            else
            {
                timer1.Enabled = false;
                label1.Text = "0";
                isRealDis = false;
            }
        }

     

        /// <summary>
        /// 计算平均值,乎略0的元素
        /// </summary>
        /// <returns></returns>
        private double getAverageNoZero(List<double> data)
        {
            var ary1 = new List<double>();
            foreach(var m in data)
            {
                if (m != 0) ary1.Add(m);
            }
            if (ary1.Count < 1) return 0;
            return ary1.Average();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isRealDis)
            {
                var realDisThickData = getAverageNoZero(getData(10));
                label1.Text = calThick(系数A,0.5, realDisThickData).ToString("0.00000");
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var m = dmc.getpos()/1000;
            int pos = 1;
            if (int.TryParse(textBox3.Text, out pos))
            {
                goSpecifyPT(m + pos);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            var m = dmc.getpos()/1000;
             int pos = 1;
             if (int.TryParse(textBox3.Text, out pos))
             {
                 goSpecifyPT(m -pos);
             }
            
        }

        private void button11_Click(object sender, EventArgs e)
        {
            timer2.Interval = 12000;
            timer2.Enabled = true;
            timer2.Start();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            
            var dataAry = new List<double>();
            Stopwatch sb1 = new Stopwatch();
            sb1.Start();
            float speed = 120000;
            float.TryParse(tbSpeed.Text, out speed);
            dmc.SetMoveSpeed(0, speed);
            var f1 = checkBox2.Checked;
          

            var xs = 取陶瓷片系数(45);
            dmc.A_Move(0, 80);
            dmc.waitStop(0);
            dmc.A_Move(0, 430);
            dataAry.Clear();
            for (int i = 0; i < 2; i++)
            {
                dataAry.AddRange(getData(7677));
            }
            var s1 = sb1.ElapsedMilliseconds;
            saveSegmentData(dataAry, xs, 10);
            saveCSV(dataAry, xs, f1,"go");
            dmc.waitStop(0);

            dataAry.Clear();
            dmc.A_Move(0, 100);
            for (int i = 0; i < 2; i++)
            {
                dataAry.AddRange(getData(7677));
            }
            dataAry.Reverse();
            saveSegmentData(dataAry, xs, 10);
            saveCSV(dataAry, xs, f1,"back");
            dmc.waitStop(0);
            
            richTextBox1.AppendText("\r\n"+sb1.ElapsedMilliseconds.ToString());
            sb1.Stop();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            timer2.Enabled = false;
        }
    }


    public sealed class PinnedObject : IDisposable
    {
        private GCHandle _Handle;

        public IntPtr Pointer
        {
            get { return _Handle.AddrOfPinnedObject(); }
        }

        public PinnedObject(object target)
        {
            _Handle = GCHandle.Alloc(target, GCHandleType.Pinned);
        }

        public void Dispose()
        {
            _Handle.Free();
            _Handle = new GCHandle();
        }

    }
}



需要源代码的朋友请下载:

https://download.csdn.net/download/suneggs/12693459



--------------------- 

作者:hackpig
来源:
www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!


#转载请注明出处 www.skcircle.com 《少有人走的路》勇哥的工业自动化技术网站。如果需要本贴图片源码等资源,请向勇哥索取。
  • 评论列表:
  •  liu3san
     发布于 2020-08-06 14:35:52  回复该评论
  • 您好,我正在学着写类似的程序,想向您请教一下,这里的 CL3_IF_DllSample 是以程序集的方式引用的还是只引用 dll 文件即可?我试着引用 CL3_IF.dll 文件时似乎引用不了。
    另外,请问您是否可以提供一下您这里的源码(邮箱:yaoyangliu@126.com)?想学习一下,谢谢!

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«   2020年9月   »
123456
78910111213
14151617181920
21222324252627
282930
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
标签列表
搜索
最新留言
最近发表
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:

Powered By Z-BlogPHP 1.6.0 Valyria

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864