资源简介

这是使用LibUsbDotNet的例程。通过该例程,你可以学会简单的使用libusb来打开和关闭设备,并通过usb来调用读取和写入数据。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibUsbDotNet.Usb;
using LibUsbDotNet.Usb.Info;
using LibUsbDotNet.Usb.Main;
using LibUsbDotNet.Usb.Internal;
namespace usblib
{
    public partial class Form1 : Form
    {
        private UsbDeviceList mDevList;
        private UsbDevice mDev;
        private UsbEndpointWriter mEpWriter;
        private UsbEndpointReader mEpReader;
        //public delegate void myDelegate();
        public Form1()
        {

            InitializeComponent();
            byte[] a = new byte[1];
            a[0] = 0xff;
            mDevList = UsbGlobals.DeviceList;
            foreach (UsbDevice device in mDevList)
            {
                string sAdd = string.Format(“Vid:0x{0:X4} Pid:0x{1:X4}“ device.Info.IdVendor device.Info.IdProduct);
                comboBox1.Items.Add(sAdd);
            }
        }

        private bool openDevice(int index)
        {
            mDev = mDevList[index];
            if (mDev.Open())
                if (mDev.SetConfiguration(1) >= 0)
                    if (mDev.ClaimInterface(0) >= 0)
                    {
                        mEpReader = mDev.OpenInterruptEndpointReader(ReadEndpoints.Ep01);
                        mEpWriter = mDev.OpenBulkEndpointWriter(WriteEndpoints.Ep01);
                    }
            return true;
        }
        private void button2_Click(object sender EventArgs e)
        {
            

        openDevice(comboBox1.SelectedIndex);

            timer1.Start();
        
        }

        private void timer1_Tick(object sender EventArgs e)
        {
            byte[] a = new byte[2];
            byte[] b = new byte[2];
            a[0] = 0x01;
            a[1] = Convert.ToByte(comboBox2.Text);
            mEpWriter.Write(a 0 2 1000);
            mEpReader.Read(b 0 2 1000);
            label1.Text = ((Single)(b[0] | b[1] << 8) / 1023 * 2.56).ToString(“F2“) + “V“;
        }

        private void button3_Click(object sender EventArgs e)
        {
            timer1.Stop();
            closeDevice();
        }
        private void closeDevice()
        {
            if (mDev != null)
            {
                mEpReader.DataReceivedEnabled = false;
                mEpReader.Dispose();
                mDev.ReleaseInterface(0);
                mDev.SetConfiguration(0);
                mDev.Close();
                mDev = null;
            }
        }

    }
}


        

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      57344  2007-10-06 00:38  usblib\usblib\bin\Debug\LibUsbDotNet.dll

     文件      74973  2007-10-06 00:38  usblib\usblib\bin\Debug\LibUsbDotNet.xml

     文件     319488  2008-08-29 17:46  usblib\usblib\bin\Debug\usblib.exe

     文件      26112  2008-08-29 17:46  usblib\usblib\bin\Debug\usblib.pdb

     文件       5632  2005-11-11 22:25  usblib\usblib\bin\Debug\usblib.vshost.exe

     文件       2637  2008-07-29 23:49  usblib\usblib\Form1.cs

     文件       6220  2008-07-28 22:18  usblib\usblib\Form1.Designer.cs

     文件       6008  2008-07-28 22:18  usblib\usblib\Form1.resx

     文件      36864  2004-12-08 09:36  usblib\usblib\ICSharpCode.USBlib.dll

     文件       5060  2008-06-22 13:05  usblib\usblib\obj\Debug\ResolveAssemblyReference.cache

     文件        842  2008-07-28 22:18  usblib\usblib\obj\Debug\usblib.csproj.GenerateResource.Cache

     文件     319488  2008-08-29 17:46  usblib\usblib\obj\Debug\usblib.exe

     文件        180  2008-07-28 22:18  usblib\usblib\obj\Debug\usblib.Form1.resources

     文件      26112  2008-08-29 17:46  usblib\usblib\obj\Debug\usblib.pdb

     文件        180  2008-06-22 12:12  usblib\usblib\obj\Debug\usblib.Properties.Resources.resources

     文件        837  2008-08-29 17:46  usblib\usblib\obj\usblib.csproj.FileListAbsolute.txt

     文件        465  2008-06-22 12:10  usblib\usblib\Program.cs

     文件       1160  2008-06-22 12:10  usblib\usblib\Properties\AssemblyInfo.cs

     文件       2868  2008-06-22 12:10  usblib\usblib\Properties\Resources.Designer.cs

     文件       5612  2008-06-22 12:10  usblib\usblib\Properties\Resources.resx

     文件       1091  2008-06-22 12:10  usblib\usblib\Properties\Settings.Designer.cs

     文件        249  2008-06-22 12:10  usblib\usblib\Properties\Settings.settings

     文件     295606  2008-08-29 17:45  usblib\usblib\Studio.ico

     文件       3597  2008-08-29 17:46  usblib\usblib\usblib.csproj

     文件        907  2008-06-22 12:10  usblib\usblib.sln

    ..A..H.     12800  2008-08-29 17:49  usblib\usblib.suo

     目录          0  2008-06-22 12:10  usblib\usblib\obj\Debug\TempPE

     目录          0  2008-06-22 13:05  usblib\usblib\bin\Debug

     目录          0  2008-06-22 12:10  usblib\usblib\bin\Release

     目录          0  2008-08-29 17:46  usblib\usblib\obj\Debug

............此处省略9个文件信息

评论

共有 条评论