C#學(xué)習(xí)筆記02:Led燈點(diǎn)亮、閃爍、跑馬燈效果
1.首先打開VS,創(chuàng)建一個(gè)新項(xiàng)目。




2.窗體應(yīng)用程序新建之后,先點(diǎn)擊啟動(dòng)按鈕,生成應(yīng)用程序。然后關(guān)閉彈出窗口(這一步不要忽略,不然后面一步bin-debug里面沒有exe應(yīng)用程序,我們需要把采集卡的dll庫(kù)放置在這個(gè)帶exe應(yīng)用程序的文件夾里);之后右側(cè)解決方案,01-led點(diǎn)亮單擊鼠標(biāo)右鍵選擇:在文件資源管理器中打開文件夾。


找到bin-Debug,這個(gè)文件夾里面有exe應(yīng)用程序文件。
我們需要把使用板卡的二次開發(fā)庫(kù)文件,放置到這個(gè)有exe應(yīng)用程序的文件夾里面。(找到采集卡配套資料包里面的dll,復(fù)制粘貼到這里,在這里需要注意的是,系統(tǒng)默認(rèn)選擇的是any-cpu,也可以自己設(shè)定X64,還是x86;

例如:點(diǎn)擊配置管理器增加X64平臺(tái),再點(diǎn)擊啟動(dòng),就可以生成X64平臺(tái)的exe程序,這個(gè)時(shí)候,復(fù)制采集卡資料包里的64位dll文件,就要放到剛生成的X64文件里面,bin-Debug里面。
3.選擇左側(cè)的工具箱,公共控件,放置兩個(gè)button控件。并修改一下text文本名字和name屬性。Button1,text修改為:開始、name修改為start_bt。Button2:text修改為退出、name改成exit_bt。




修改過后雙擊開始按鈕,可以跳轉(zhuǎn)到代碼界面,首先復(fù)制:using?System.Runtime.InteropServices;放置到命名空間這個(gè)位置。

下面就是要加載采集卡的庫(kù),以及需要使用的庫(kù)函數(shù);
????????[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);
?

此處需要注意的是,采集卡的庫(kù)是C庫(kù),C#在調(diào)用函數(shù)庫(kù)的時(shí)候,需要進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換。C語(yǔ)言的unsignedchar8位無符號(hào)整型數(shù)據(jù),對(duì)應(yīng)的C#數(shù)據(jù)類型為Byte.所以說明書,DOset V12這個(gè)函數(shù)的兩個(gè)參數(shù),我們要改成Byte類型.

?
4.下面在開始按鈕控件的函數(shù)前定義一個(gè)整形變量 w,賦值為 0。在函數(shù)
內(nèi)寫入一個(gè) if 判斷。
當(dāng) w 值等于 0 時(shí),將 w 重新賦值為 1,用 OpenUsbV12()啟動(dòng)數(shù)據(jù)采集卡,然后用 DoSetV12()將端口 OUT1 設(shè)置輸出 5V 電壓并將控件的 Text 屬性設(shè)置為“暫?!薄?/p>
當(dāng) w 值不等于 0 時(shí),將 w 重新賦值為 0,將端口 OUT1 設(shè)置為輸出 0V,用 CloseUsbV12()關(guān)閉數(shù)據(jù)采集卡,并將控件的 Text 屬性設(shè)置為“繼續(xù)”。
?
整體代碼如下:
?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點(diǎn)亮
{
????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 = ("繼續(xù)");
????????????}
?
????????}
?
????????private?void?exit_bt_Click(object?sender, EventArgs e)
????????{
????????????if?(w == 0)
????????????{
????????????????this.Close();
?????????????}
????????????else
????????????{
????????????????DoSetV12(0, 0);
????????????????CloseUsbV12();
????????????????this.Close();
????????????}
????????}
????}
}

?
?
單個(gè)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點(diǎn)亮
{
????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 = ("繼續(xù)");
????????????????}
?????????????
????????}
?
????????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點(diǎn)亮
{
????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 = ("繼續(xù)");
????????????????}
?????????????
????????}
?
????????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;
????????????}
????????}
????}
}
?