📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "CyAPI.h"
#include <math.h>
CCyUSBDevice *FX2Device;
void __fastcall FindFX2(void);
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//bArrival = false;
//AppDir = ExtractFileDir(Application->ExeName);
FX2Device = new CCyUSBDevice(Handle);
FindFX2();
}
//---------------------------------------------------------------------------
void __fastcall FindFX2(void)
{
// Disable as if no device found
int devices = FX2Device->DeviceCount();
if (devices == 0)
{
return; //找不到设备
}
//Form1->Edit1->Text = VID;
int d = 0;
int VID = 0;
while ((d < devices ) && (VID != 0x0547))
{
FX2Device->Open(d);
if (!FX2Device->IsOpen())
{ // Try a reset
FX2Device->Reset();
Sleep(1000);
FX2Device->Open(d);
}
VID = FX2Device->VendorID;
d++;
}
if (FX2Device->IsOpen())
{
if (VID == 0x0547)
{
// All the control transfers use REQ_VENDOR and TGT_DEVICE
FX2Device->ControlEndPt->ReqType = REQ_VENDOR;
FX2Device->ControlEndPt->Target = TGT_DEVICE;
Form1->Edit1->Text = FX2Device->ProductID;
}
}
//throw Exception("No USB Devices connected.");
}
void __fastcall ResetFX2(UCHAR ucStop)
{
if (!FX2Device->IsOpen()) throw Exception("No USB Devices connected.");
FX2Device->ControlEndPt->ReqCode = 0xA0;
FX2Device->ControlEndPt->Value = 0xE600;
FX2Device->ControlEndPt->Index = 0;
long len = 1;
FX2Device->ControlEndPt->Write(&ucStop,len);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CCyBulkEndPoint *BulkInEpt = NULL;
CCyBulkEndPoint *BulkOutEpt = NULL;
int eptCount = FX2Device->EndPointCount();
Form1->Edit1->Text = eptCount;
for (int i=1; i<eptCount; i++)
{
bool bIn = FX2Device->EndPoints[i]->Address & 0x80;
bool bBulk = (FX2Device->EndPoints[i]->Attributes == 2);
if (bBulk && bIn) BulkInEpt = (CCyBulkEndPoint *) FX2Device->EndPoints[i];
if (bBulk && !bIn) BulkOutEpt = (CCyBulkEndPoint *) FX2Device->EndPoints[i];
//Form1->Edit1->Text = i;
}
/**/
/* */
OVERLAPPED outOvLap, inOvLap;
outOvLap.hEvent = CreateEvent(NULL, false, false,"CYUSB_OUT");
//inOvLap.hEvent = CreateEvent(NULL, false, false,"CYUSB_IN");
char inBuf[128];
ZeroMemory(inBuf, 128);
char buffer[128];
LONG length = 128;
// Just to be cute, request the return data bef
UCHAR *inContext;
//inContext = FX2Device->BulkInEndPt->BeginDataXfer(inBuf,length,&inOvLap);
UCHAR *outContext = FX2Device->BulkOutEndPt->BeginDataXfer(buffer,length,&outOvLap);
FX2Device->BulkOutEndPt->WaitForXfer(&outOvLap,100);
//FX2Device->BulkInEndPt->WaitForXfer(&inOvLap,100);
FX2Device->BulkOutEndPt->FinishDataXfer(buffer,length,&outOvLap,outContext);
//FX2Device->BulkInEndPt->FinishDataXfer(inBuf,length,&outOvLap,outContext);
CloseHandle(outOvLap.hEvent);
//CloseHandle(inOvLap.hEvent);
Form1->Edit1->Text = eptCount;
ShowMessage("ok");
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -