C#
获取设备端点,点击Start,启动线程不断接收USB设备传来的数据,并保存到文件中。
窗体设计
源码
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.Threading;
using System.IO;
using CyUSB;//需要添加CyUSB.dll引用
namespace USBSynDemo
{
public partial class Form1 : Form
{
//USB设备相关字段
USBDeviceList usbDevices;
CyUSBDevice MyDevice;
CyUSBEndPoint EndPoint;
const int XFERSIZE=512;
int BufSz = 512;
int QueueSz = 1;//4
int IsoPktBlockSize;
bool bRunning = false;
Thread tReceiveData = null;
string filePath = @"D:\testdata.dat";//存入字节数据
public Form1()
{
InitializeComponent();
usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
SetDevice();
}
/*Summary
Search the device with VID-PID 04b4-1003 and if found, select the end point
*/
private void SetDevice()
{
USBDevice dev = usbDevices[0x04B4, 0x1003];
if (dev != null)
{
MyDevice = (CyUSBDevice)dev;
GetEndpointsOfNode(MyDevice.Tree);
if (EndPointsComboBox.Items.Count > 0)
{
EndPointsComboBox.SelectedIndex = 0;
StartBtn.Enabled = true;
}
}
else
{
StartBtn.Enabled = false;
EndPointsComboBox.Items.Clear();
EndPointsComboBox.Text = "";
}
}
/*Summary