www.pudn.com > USBWinTest.rar > Form1.cs, change:2013-05-28,size:2745b


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; 
 
namespace KHM32_USBHID 
{ 
    public partial class Form1 : Form 
    { 
        UsbControl usbctltest = new UsbControl(); 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void Form1_Load(object sender, EventArgs e) 
        { 
          usbctltest.MessageReceived+=new UsbControl.DataRevEventHandler(Data_event); 
        } 
 
        public void Data_event(object sender, UsbControl.DataRevEventArgs DataRevTrig) 
        { 
           // this.textBox3. 
            string aaa = "读取数据"; 
            for (int i = 0; i < DataRevTrig.iDuty.Length; i++) 
            { 
                aaa = aaa + "--" + DataRevTrig.iDuty[i].ToString(); 
 
            } 
            if (aaa != "") 
                //listBox1.Items.Add(aaa); 
                SetItemAdd(aaa); 
        } 
 
        delegate void AddListItem(string item); 
        private void SetItemAdd(string item) 
        { 
            // InvokeRequired需要比较调用线程ID和创建线程ID 
            // 如果它们不相同则返回true 
            if (this.listBox1.InvokeRequired) 
            { 
                AddListItem d = new AddListItem(SetItemAdd); 
                this.Invoke(d, new object[] { item }); 
            } 
            else 
            { 
                listBox1.Items.Add(item); 
            } 
        } 
        private void button1_Click(object sender, EventArgs e) 
        { 
            string Pid = ""; 
            string Vid = ""; 
            if (tbVid.Text == "" || tbPid.Text == "") 
            { 
                MessageBox.Show("Please set VID PID"); 
                return; 
            } 
            Pid = tbPid.Text; 
            Vid = tbVid.Text; 
            if (usbctltest.OpenKHM32(Vid, Pid) == 0) 
            { 
                MessageBox.Show("Find Device success"); 
                return; 
            } 
            else 
            { 
                MessageBox.Show("Find Device Failed"); 
                return; 
            } 
        } 
 
        private void button2_Click(object sender, EventArgs e) 
        { 
            if (usbctltest.StartCommunication() == 0) 
            { 
                MessageBox.Show("Start Communication !!"); 
                return; 
            } 
            else 
            { 
                MessageBox.Show("Start error"); 
                return; 
            } 
        } 
 
        private void button3_Click(object sender, EventArgs e) 
        { 
            usbctltest.CloseKHM32(); 
        } 
    } 
}