欢迎光临散文网 会员登陆 & 注册

C#学习笔记02:Led灯点亮、闪烁、跑马灯效果

2023-07-26 09:26 作者:学编程的小设计  | 我要投稿

1.首先打开VS,创建一个新项目。

2.窗体应用程序新建之后,先点击启动按钮,生成应用程序。然后关闭弹出窗口(这一步不要忽略,不然后面一步bin-debug里面没有exe应用程序,我们需要把采集卡的dll库放置在这个带exe应用程序的文件夹里);之后右侧解决方案,01-led点亮单击鼠标右键选择:在文件资源管理器中打开文件夹。


找到bin-Debug,这个文件夹里面有exe应用程序文件。

我们需要把使用板卡的二次开发库文件,放置到这个有exe应用程序的文件夹里面。(找到采集卡配套资料包里面的dll,复制粘贴到这里,在这里需要注意的是,系统默认选择的是any-cpu,也可以自己设定X64,还是x86;


例如:点击配置管理器增加X64平台,再点击启动,就可以生成X64平台的exe程序,这个时候,复制采集卡资料包里的64位dll文件,就要放到刚生成的X64文件里面,bin-Debug里面。

3.选择左侧的工具箱,公共控件,放置两个button控件。并修改一下text文本名字和name属性。Button1,text修改为:开始、name修改为start_bt。Button2:text修改为退出、name改成exit_bt。

修改过后双击开始按钮,可以跳转到代码界面,首先复制:using System.Runtime.InteropServices;放置到命名空间这个位置。


下面就是要加载采集卡的库,以及需要使用的库函数;

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int OpenUsbV12();

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int CloseUsbV12();

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int DoSetV12(byte chan, byte state);

 

此处需要注意的是,采集卡的库是C库,C#在调用函数库的时候,需要进行数据类型转换。C语言的unsignedchar8位无符号整型数据,对应的C#数据类型为Byte.所以说明书,DOset V12这个函数的两个参数,我们要改成Byte类型.

 

4.下面在开始按钮控件的函数前定义一个整形变量 w,赋值为 0。在函数

内写入一个 if 判断。

当 w 值等于 0 时,将 w 重新赋值为 1,用 OpenUsbV12()启动数据采集卡,然后用 DoSetV12()将端口 OUT1 设置输出 5V 电压并将控件的 Text 属性设置为“暂停”。

当 w 值不等于 0 时,将 w 重新赋值为 0,将端口 OUT1 设置为输出 0V,用 CloseUsbV12()关闭数据采集卡,并将控件的 Text 属性设置为“继续”。

 

整体代码如下:

 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.Runtime.InteropServices;

 

namespace led点亮

{

    public partial class Form1 : Form

    {

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int OpenUsbV12();

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int CloseUsbV12();

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int DoSetV12(byte chan, byte state);

        public Form1()

        {

            InitializeComponent();

        }

 

        int w = 0;

        private void start_bt_Click(object sender, EventArgs e)

        {

            if (w == 0)

            {

                w = 1;

                OpenUsbV12();

                DoSetV12(0, 1);

                start_bt.Text = ("暂停");

             }

            else

            {

                w = 0;

                CloseUsbV12();

                start_bt.Text = ("继续");

            }

 

        }

 

        private void exit_bt_Click(object sender, EventArgs e)

        {

            if (w == 0)

            {

                this.Close();

             }

            else

            {

                DoSetV12(0, 0);

                CloseUsbV12();

                this.Close();

            }

        }

    }

}

 

 

单个Led灯闪烁代码如下:

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.Runtime.InteropServices;

using System.Threading;

 

 

namespace _01_led点亮

{

    public partial class Form1 : Form

    {

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int OpenUsbV12();

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int CloseUsbV12();

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int DoSetV12(byte chan, byte state);

        public Form1()

        {

            InitializeComponent();

           

        }

        int w = 0;

 

        private void start_bt_Click(object sender, EventArgs e)

        {

             

                if (w == 0)

                {

                    w = 1;

                    OpenUsbV12();

                    timer1.Interval = 1000;

                    timer1.Start();

                     start_bt.Text = ("暂停");

 

                }

                else

                {

                    w = 0;

                    CloseUsbV12();

                    timer1.Stop();

                    start_bt.Text = ("继续");

                }

             

        }

 

        private void exit_bt_Click(object sender, EventArgs e)

        {

            if (w == 0)

            {

                this.Close();

            }

            else

            {

                DoSetV12(0, 0);

                CloseUsbV12();

                this.Close();

            }

        }

        static int bitt = 0;

        private void timer1_Tick(object sender, EventArgs e)

        {

            if (bitt==0)

            {

                bitt = 1;

                DoSetV12(0, 1);

 

            }

            else

            {

                bitt = 0;

                DoSetV12(0, 0);

            }

        }

    }

}

 

 

 

LED跑马灯效果代码如下:

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.Runtime.InteropServices;

using System.Threading;

 

 

namespace _01_led点亮

{

    public partial class Form1 : Form

    {

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int OpenUsbV12();

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int CloseUsbV12();

        [DllImport("USBDAQ_DLL_V12.dll")]

        static extern int DoSetV12(byte chan, byte state);

        public Form1()

        {

            InitializeComponent();

           

        }

        int w = 0;

 

        private void start_bt_Click(object sender, EventArgs e)

        {

             

                if (w == 0)

                {

                    w = 1;

                    OpenUsbV12();

                    timer1.Interval = 1000;

                    timer1.Start();

                    start_bt.Text = ("暂停");

 

                }

                else

                {

                    w = 0;

                    CloseUsbV12();

                    timer1.Stop();

                    start_bt.Text = ("继续");

                }

             

        }

 

        private void exit_bt_Click(object sender, EventArgs e)

        {

            if (w == 0)

            {

                this.Close();

            }

            else

            {

                DoSetV12(0, 0);

                CloseUsbV12();

                this.Close();

            }

        }

        static int bitt = 0;

        private void timer1_Tick(object sender, EventArgs e)

        {

            bitt = bitt + 1;

            if (bitt >3)

            {

                bitt = 0;

            }

            switch(bitt)

            {

                case 0:

                    DoSetV12(0, 1);

                    DoSetV12(1, 0);

                    DoSetV12(2, 0);

                    DoSetV12(3, 0);

                    break;

                case 1:

                    DoSetV12(0, 0);

                    DoSetV12(1, 1);

                    DoSetV12(2, 0);

                    DoSetV12(3, 0);

                    break;

                case 2:

                    DoSetV12(0, 0);

                    DoSetV12(1, 0);

                    DoSetV12(2, 1);

                    DoSetV12(3, 0);

                    break;

                case 3:

                    DoSetV12(0, 0);

                    DoSetV12(1, 0);

                    DoSetV12(2, 0);

                    DoSetV12(3, 1);

                    break;

            }

        }

    }

}

 




C#学习笔记02:Led灯点亮、闪烁、跑马灯效果的评论 (共 条)

分享到微博请遵守国家法律