君亿道科技联系电话:0755-26830682

公司新闻

最全面及时的公司资讯、企业动态

USB-QUAD08 C# 示例

来源君亿道

此示例演示如何对USB-QUAD08 进行编程。它将一个通道配置为从正交编码器读取位置,将另一个通道配置为使用模数运算符对事件进行计数。为此,它使用 CConfigScan 配置计数器输入,并使用 CLoad32 函数加载事件计数器的模数值。程序连续读取输入。这是使用 CInScan 功能和背景连续扫描选项设置的。数据保存为与 DASYLab 读取模块兼容的 ASCII 文件。

 

该程序还使用发现接口来定位和创建设备对象。使用发现界面,必须安装 InstaCal 程序,但最终用户不需要运行它。

 

该程序是一个 32 C# 控制台应用程序。如果您开始一个新项目,您必须首先添加对 McDaq 对象的 Dot Net 引用。InstaCal 安装我们的 Dot Net UL 对象。添加引用通常是通过右键单击项目 [在项目浏览器下] 并选择添加引用来完成的。

 

完整的项目作为 zip 文件附在本文末尾。

 

免责声明:

随附的代码或示例按原样提供。它尚未作为产品进行测试或验证,未在部署的应用程序或系统中使用,或在危险环境中使用。您承担使用代码或示例的所有风险。

 

 

 

使用系统;

使用麦达克;

使用 System.IO

命名空间 Quad08

{

    课程计划

    {

 

        公共常量 int CHANCOUNT = 2;

        公共常量 int FIRSTCHANNEL = 0;

        public const int LASTCHANNEL = CHANCOUNT-1;

        公共常量 int FREQ = 100;

        公共常量 int NUMOFPACKETS = 100;

        公共常量 int PACKETSIZE = 100;

        public const int BUFFERSIZE = PACKETSIZE * CHANCOUNT;

        public const int HALFBUFFSIZE = BUFFERSIZE / 2;

        public const string DEVICE = " USB-QUAD08 ";

 

 

        公共静态 StreamWriter fStream;

 

        公共静态 MccBoard daq

 

        公共静态MccDaq.CounterMode 模式;

 

        公共静态 McDaq.ScanOptions 选项;

 

        static void Main(string[] args)

        {

 

            McDaq.ErrorInfo RetVal;

           

            整数速率 = 频率;

            bool ReadLower = true;

 

 

            daq = 发现(设备);

 

            如果(daq == null

            {

                Console.WriteLine("未检测到{0}", DEVICE);

                等待键();

                返回;

            }

           

            IntPtr 缓冲区 = MccService.WinBufAlloc32Ex(BUFFERSIZE);

 

            if (buffer == IntPtr.Zero)

            {

                Console.WriteLine("坏句柄");

                返回;

            }

 

            //将计数器0设置为编码器模式

            mode = CounterMode.Encoder|CounterMode.EncoderModeBit32|CounterMode.EncoderModeX1;

            IsError( daq.CConfigScan( 0,

                                        模式,

                                        CounterDebounceTime.Debounce500ns,

                                        CounterDebounceMode.TriggerAfterStable,

                                        CounterEdgeDetection.RisingEdge

                                        CounterTickSize.Tick208pt3ns,

                                        0));

 

            //将计数器 1 设置为带模数运算的累加模式

            mode = CounterMode.Totalize|CounterMode.Bit32|CounterMode.ModuloNOn;

            IsError( daq.CConfigScan( 1,

                                        模式,

                                        CounterDebounceTime.Debounce500ns,

                                        CounterDebounceMode.TriggerAfterStable,

                                        CounterEdgeDetection.RisingEdge

                                        CounterTickSize.Tick20pt83ns,

                                        1));

 

            //用模数加载寄存器1

            IsError(daq.CLoad32(CounterRegister.MaxLimitReg1, 2000));

 

            //开始读取计数器通道

            opts = ScanOptions.Continuous|ScanOptions.Ctr32Bit|ScanOptions.Background;

            IsError(daq.CInScan(FIRSTCHANNEL, LASTCHANNEL, BUFFERSIZE, ref Rate, buffer, opts));

           

            //定时器0输出(DIO6)用作计数器1的测试信号

            tmrRate = 100;

            tmrDuty = 0.5;

            tmrDelay = 0.0;

            daq.PulseOutStart( 0,

                                参考 tmrRate

                                参考 tmrDuty

                                0,

                                参考 tmrDelay

                                0,

                                PulseOutOptions.Default);

 

            fStream = new StreamWriter(@"C:\Users\Public\Documents\DataFile.asc");

            创建文件头();//将基本信息写入文件的开头

 

            整数计数 = 0;

            整数索引 = 0;

            短数据状态;

 

            int[] intArray = new int[BUFFERSIZE];

 

            //循环直到按键

           

            {

                RetVal = daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.CtrFunction);

                if ((Index >= HALFBUFFSIZE) & ReadLower) //检查 50% 以上的数据

                {

                    //获取缓冲区的下半部分

                    IsError(MccService.WinBufToArray32(buffer, intArray, 0, HALFBUFFSIZE));

 

                    DisplayData(intArray, HALFBUFFSIZE / CHANCOUNT);

                    ReadLower = 假;//控制下一次读取的标志

                }

                否则 if ((Index < HALFBUFFSIZE) & !ReadLower)

                {

                    //获取上半部分

                    IsError(MccService.WinBufToArray32(buffer, intArray, HALFBUFFSIZE, HALFBUFFSIZE));

 

                    DisplayData(intArray, HALFBUFFSIZE / CHANCOUNT);

                    ReadLower = true;//控制下一次读取的标志

                }

 

                if (Console.KeyAvailable == true) 中断;

 

            } while ((Count < (PACKETSIZE * NUMOFPACKETS)) || Console.KeyAvailable);

 

 

            //将所有缓冲数据刷新到磁盘

            fStream.Close();

 

            //停止获取

            RetVal = daq.StopBackground(FunctionType.AiFunction);

 

            //释放内存

            MccService.WinBufFreeEx(buffer);

 

            daq.PulseOutStop(0);

 

            等待键();

 

        }

        public static int IsError(ErrorInfo e)

        {

            如果 (e.Value != 0)

            {

                Console.WriteLine(e.Message);

                等待键();

                返回 1

            }

            返回0

        }

 

        /*//////////////////////////////////////////////// ///////////////////////////////////////

        public static MccDaq.MccBoard Discover(string dev)

        {

 

            //这会忽略 InstaCal 实用程序中列出的设备

            McDaq.DaqDeviceManager.IgnoreInstaCal();

 

            //获取MCC USB设备清单

            MccDaq.DaqDeviceDescriptor[]inventory = MccDaq.DaqDeviceManager.GetDaqDeviceInventory(MccDaq.DaqDeviceInterface.Usb);

 

            int numDevDiscovered =inventory.Length;

 

            if (numDevDiscovered > 0)

            { // 遍历库存以寻找 USB-1808

                for (int boardNum = 0; boardNum < numDevDiscovered; boardNum++)

                {

 

                    尝试

                    {

 

                        if (inventory[boardNum].DevString.Contains(dev))

                        {

                            MccDaq.MccBoard daqBoard = MccDaq.DaqDeviceManager.CreateDaqDevice(boardNum,inventory[boardNum]);

                            返回daqBoard

                        }

 

                    }

                    捕获(ULException ule

                    {

                        Console.WriteLine("发生错误:" + ule.Message);

                    }

                }

            }

            返回空;

 

        }

 

 

        /*//////////////////////////////////////////////// ///////////////////////////////////////

 

        public static void WaitForKey()

        {

            Console.WriteLine("\n按任意键继续...");

           

            {

                System.Threading.Thread.Sleep(20);

 

            } while (!Console.KeyAvailable);

            ConsoleKeyInfo cki = Console.ReadKey();

 

        }

 

        public static void DisplayData(int[] datArray, int rows)

        {

 

            int i = 0;

 

            for (int row = 0; row < rows; row++)

            {

 

                for (int c = 0; c < CHANCOUNT; c++)

                {

 

                    Console.Write("{0}\t", Convert.ToInt32(datArray[i]).ToString("D").PadLeft(10));

                    fStream.Write("{0}\t", Convert.ToInt32(datArray[i]).ToString("D"));

                    ++;

                }

 

                Console.Write("\r\n");

                fStream.Write("\r\n");

            }

            fStream.Flush();

        }

        /*//////////////////////////////////////////////// ///////////////////////////////////////

 

        public static void CreateFileHeaders()

        {

            Console.WriteLine("读取通道 {0} {1}\n", FIRSTCHANNEL, LASTCHANNEL);

 

            //'''''''''''''''''''''创建文本文件头字符串''''''''''''''''

            字符串[] hdr = 新字符串[7];

            hdr[0] = "录制日期:{0}";

            hdr[1] = "块长度:{0}";

            hdr[2] = "数据包数量:{0}";

            hdr[3] = "增量:{0} 秒。";

            hdr[4] = "运行时间:{0} 秒。";

            hdr[5] = "通道数:{0}";

 

 

            //'''''''''''''''''''''' 构建列标题字符串'''''''''''''''''''

 

            for (int j = FIRSTCHANNEL; j < CHANCOUNT; j++)

            {

                hdr[6] = hdr[6] + String.Format("In{0} [V]", j);

                hdr[6] = hdr[6] + "\t";

 

            }

            hdr[4].Trim('\t');

 

            //'''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''

            DateTime MyDate = DateTime.Now;

 

            fStream.WriteLine(hdr[0], MyDate);

            fStream.WriteLine(hdr[1], PACKETSIZE);

            fStream.WriteLine(hdr[2], NUMOFACKETS);

            fStream.WriteLine(hdr[3], (1.0f / FREQ).ToString("0.000"));

            fStream.WriteLine(hdr[4], (NUMOFPACKETS * PACKETSIZE) / FREQ);

            fStream.WriteLine(hdr[5], CHANCOUNT);

            fStream.WriteLine(hdr[6]);

 

            Console.WriteLine(hdr[0], MyDate);

            Console.WriteLine(hdr[1], PACKETSIZE);

            Console.WriteLine(hdr[2], NUMOFPACKETS);

            Console.WriteLine(hdr[3], (1.0f / FREQ).ToString("0.000"));

            Console.WriteLine(hdr[4], (NUMOFPACKETS * PACKETSIZE) / FREQ);

            Console.WriteLine(hdr[5], CHANCOUNT);

            Console.WriteLine(hdr[6]);

 

        }

 

    }

}


热门新闻

浅析直流电流DC测量方法
时间 2018/9/10 16:47:35 查看 2871
示波器高压探头的使用技巧
时间 2018/9/10 16:47:34 查看 2560
漏电流传感器使用分类装置
时间 2018/9/10 16:47:33 查看 2415
主营:电流探头,电压探头,罗氏线圈,电流传感器,高压探头,电流互感器,功率电子测量产品
粤ICP备14003389号深圳君亿道科技有限公司 © https://www.pemch.com