在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#网络编程 → 数据采集卡读取(USB)

数据采集卡读取(USB)

C#网络编程

下载此实例
  • 开发语言:C#
  • 实例大小:0.08M
  • 下载次数:51
  • 浏览次数:813
  • 发布时间:2020-09-18
  • 实例类别:C#网络编程
  • 发 布 人:happylife
  • 文件格式:.rar
  • 所需积分:3
 相关标签: 数据采集 数据 采集

实例介绍

【实例简介】对电压进行数据多路采集,USB多通道数据采集

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; 

namespace WindowsFormsApplication1
{  
    public partial class Form1 : Form
    {
        [DllImport("usb-1000.dll")]
        public static extern int FindUSBDAQ();
        [DllImport("usb-1000.dll")]
        public static extern int OpenDevice(int DevIndex);
        [DllImport("usb-1000.dll")]
        public static extern int ResetDevice(int DevIndex);
        [DllImport("usb-1000.dll")]
        public static extern void CloseDevice(int DevIndex);
        [DllImport("usb-1000.dll")]
        public static extern int SetUSB1AiRange(int DevIndex, float Range);
        [DllImport("usb-1000.dll")]
        public static extern int SetSampleRate(int DevIndex, uint SampleRate);
        [DllImport("usb-1000.dll")]
        public static extern int SetChanSel(int DevIndex, UInt16 ChSel);
         [DllImport("usb-1000.dll")]
        public static extern int SetChanMode(int DevIndex, byte ChanMode);
        [DllImport("usb-1000.dll")]
        public static extern int SetTrigSource(int DevIndex, byte TrigSource);
        [DllImport("usb-1000.dll")]
        public static extern int SetSoftTrig(int DevIndex, byte Trig);
        //public static extern int SetTrigEdge(int DevIndex, byte TrigEdge);
        [DllImport("usb-1000.dll")]
        public static extern int ClearTrigger(int DevIndex);
        //public static extern int SetDioOut(int DevIndex, byte DioChanSel, byte DioOut);
        //public static extern int TransDioIn(int DevIndex, byte TransDioSwitch);
        //public static extern int SetCounter(int DevIndex, byte CtrNum, byte CtrMode, byte CtrEdge);
        //public static extern int StartCounter(int DevIndex, byte CtrNum, byte OnOff);
        //public static extern int InitDA(int DevIndex);
        //public static extern int SetDA(int DevIndex, byte DANum, float DAVolt);
        //public static extern int SetWavePt(int DevIndex, byte DANum, float DAVolt);
        //public static extern int ClrWavePt(int DevIndex, byte DANum);
        //public static extern int SetWaveSampleRate(int DevIndex, uint WaveSampleRate);
        //public static extern int WaveOutput(int DevIndex, byte DANum);

        [DllImport("usb-1000.dll")]
        public static extern int ClearBufs(int DevIndex);
        //public static extern int ClearCounter(int DevIndex, byte CtrNum);
        [DllImport("usb-1000.dll")]
        public static extern int StartRead(int DevIndex);
        [DllImport("usb-1000.dll")]
        public static extern int StopRead(int DevIndex);
        [DllImport("usb-1000.dll")]
        public static extern int GetAiChans(int DevIndex, int Num, UInt16 ChSel, float[] Ai, int TimeOut);
        public Form1()
        {
            InitializeComponent();
        }

        private void Read_Click(object sender, EventArgs e)
        {
            int temp , i;
            int QTYperCH , Dev_Index;
            uint SampleRate ;
            UInt16 Channel_Select = 0; 
            byte ChannelMode ;
            Dev_Index = Convert.ToInt32(Dev_Index_Input.Text);
            QTYperCH = Convert.ToInt32(QTYperCH_Input.Text);
            SampleRate = Convert.ToUInt32(SampleRate_Input.Text);
            ChannelMode = Convert.ToByte(ChannelMode_Input.Text);
            
            listView1.Clear();
            listView1.Columns.Add("Num");
            int QTYofCH = 0;
            for (i = 0; i < 16; i  )
            {
                if (Channel_Select_Input.GetItemChecked(i))
                {
                    Channel_Select = (UInt16)(Channel_Select   (UInt16)Math.Pow(2, i));
                    listView1.Columns.Add("ai"   i.ToString() );
                    QTYofCH  ;
                }
            }
            float[] ai = new float[QTYperCH * QTYofCH];
            temp = OpenDevice(Dev_Index);
            temp = ResetDevice(Dev_Index);
            temp = SetUSB1AiRange(Dev_Index, 5);			                // 设置量程
            temp = SetSampleRate(Dev_Index, SampleRate);		            // 设置采样率
            temp = SetChanMode(Dev_Index, ChannelMode);
            temp = SetChanSel(Dev_Index, Channel_Select);			        // 设置需要采集的通道,此处选择ai0~ai7

            temp = StartRead(Dev_Index);					                // 启动读取数据线程
            //Sleep(30);							                        // 等待读数线程启动完毕
            temp = SetSoftTrig(Dev_Index, 1);				                // 设置软件触发信号
            
            // 用户读取数据
            // 连续采集时循环使用GetAiChans()
            temp = GetAiChans(Dev_Index, QTYperCH, Channel_Select, ai, 4000);		// 每通道读取1000点,超时设置1000ms

            listView1.Items.Clear();
            for (i = 0; i < QTYperCH; i  )
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = i.ToString();
                int j = 0 , k = 0;
                for (j = 0; j < 15; j  )
                {
                    if (Channel_Select_Input.GetItemChecked(j))
                    {
                        lvi.SubItems.Add(ai[k * QTYperCH   i].ToString());
                        k  ;
                    }
                }
                lvi.SubItems.Add(ai[i].ToString());
                this.listView1.Items.Add(lvi); 
            }
            temp = StopRead(Dev_Index);					    // 结束读取数据线程
            temp = SetSoftTrig(Dev_Index, 0);				// 释放软件触发信号
            //temp = ClearTrigger(Dev_Index);				// 清空触发标志
            temp = ClearBufs(Dev_Index);					// 清空所有缓存数据
            CloseDevice(Dev_Index);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int temp;
            temp = FindUSBDAQ();
            Dev_QTY.Text = temp.ToString();
        }
    }
}

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警