//****************************************************************************************
#include <vcl.h>
#pragma hdrstop
#include "UnUSBHID.h"
//****************************************************************************************
#pragma package(smart_init)
#pragma link "JvHidControllerClass"
#pragma resource "*.dfm"
TForm1 *Form1;
//****************************************************************************************
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
// 慚摟廓鞅頑甕� 縯璀甕� OLEAUT32.DLL
// OLE_DLL = LoadLibrary("OLEAUT32.DLL");
// if (OLE_DLL == 0)
// MessageDlg("疝廖蒙 頑彙擰蒐 OLEAUT32.DLL. GetLastError = " +
// IntToStr(GetLastError), mtError, TMsgDlgButtons() << mbOK, 0);
}
//****************************************************************************************//****************************************************************************************
// 蜻影獎� 頑詭魎 詭駁癮 篛綏蝠慘�
void __fastcall TForm1::Button4Click(TObject *Sender)
{
BufOut[0] = 0x77; // � 滓瘟蝶� HID 檀隧蝗羲鈇 REPORT_ID = 0x77
if ((BufOut[1] == 0xAA) && (BufOut[2] == 0x55)){
BufOut[1] = 0x00;
BufOut[2] = 0x00;
}
else{
BufOut[1] = 0xAA;
BufOut[2] = 0x55;
BufOut[3] = 0x00;
BufOut[4] = 0x00;
}
if(CurrentDevice != NULL)
CurrentDevice->WriteFile(BufOut, ToWrite, Written);
Sleep(500);
BufOut[1] = 0x00;
BufOut[2] = 0x00;
BufOut[3] = 0x00;
BufOut[4] = 0x00;
if(CurrentDevice != NULL)
CurrentDevice->WriteFile(BufOut, ToWrite, Written);
Sleep(100);
}
//****************************************************************************************
// 蜻影獎� 頑詭魎 飼雒磋� 篛綏蝠慘�
void __fastcall TForm1::Button3Click(TObject *Sender)
{
BufOut[0] = 0x77; // � 滓瘟蝶� HID 檀隧蝗羲鈇 REPORT_ID = 0x77
if ((BufOut[3] == 0x55) && (BufOut[4] == 0xAA)){
BufOut[3] = 0x00;
BufOut[4] = 0x00;
}
else{
BufOut[3] = 0x55;
BufOut[4] = 0xAA;
BufOut[1] = 0x00;
BufOut[2] = 0x00;
}
if(CurrentDevice != NULL)
CurrentDevice->WriteFile(BufOut, ToWrite, Written);
Sleep(500);
BufOut[1] = 0x00;
BufOut[2] = 0x00;
BufOut[3] = 0x00;
BufOut[4] = 0x00;
if(CurrentDevice != NULL)
CurrentDevice->WriteFile(BufOut, ToWrite, Written);
Sleep(100);
}
//****************************************************************************************
// 蜻影獎� 頑詭魎 螂� 篛綏蝠慘滓
void __fastcall TForm1::Button1Click(TObject *Sender)
{
BufOut[0] = 0x77; // � 滓瘟蝶� HID 檀隧蝗羲鈇 REPORT_ID = 0x77
BufOut[1] = 0xAA;
BufOut[2] = 0x55;
BufOut[3] = 0x55;
BufOut[4] = 0xAA;
if(CurrentDevice != NULL)
CurrentDevice->WriteFile(BufOut, ToWrite, Written);
BufOut[1] = 0x00;
BufOut[2] = 0x00;
BufOut[3] = 0x00;
BufOut[4] = 0x00;
Sleep(100);
}
//****************************************************************************************
// 頜談窟曖� 螂瘟隋�綏 滓瘩灘 � 隄罩 HID 檀隧蝗羲鈇�, 頡溺億雛瘩贖 � 羸獗 USB
void __fastcall TForm1::JvHidDeviceController1DeviceChange(TObject *Sender)
{
if (DevList == NULL){
// 縝頌痞� 縉撇蝌 檀隧蝗羲�
DevList = new TList();
}
else{
// 褓隆菔溘靦 縉撇蝌 檀隧蝗羲� 褕 隄罩 頑震篦� 擱琦痞粱筮 熱 檀隧蝗羲鈇
for (int i = 0; i < DevList->Count; i++)
((TJvHidDevice *)DevList->Items[i])->Free();
// 諄撇錦� 縉撇蒙 檀隧蝗羲�
DevList->Clear();
}
// 諄撇錦� 蜻憫衝粵錶 ListBox 褕 羲痙贖 頑震篦� 熱頊痢慷 慣� 彰粵錚繃蒙錙曉� 檀隧蝗羲�
ListBox1->Clear();
Form1->Caption = "";
// 閱擇鞣縛粵徹 隄罩 HID 檀隧蝗羲�, 頡溺億雛瘩贖 � 羸獗 USB
JvHidDeviceController1->Enumerate();
Label1->Font->Color = clPurple;
Label1->Caption = "USB 檀隧蝗羲隆 螂慮熱 滓瘩籠� 獗 頡溺億雛瘟";
Label2->Visible = false;
}
//****************************************************************************************
// 頜談窟曖� 頡骯闐綏 滓瘩灘 � 隄罩 HID 檀隧蝗羲鈇�, 頡溺億雛瘩贖 � 羸獗 USB
bool __fastcall TForm1::JvHidDeviceController1Enumerate(
TJvHidDevice *HidDev, const int Idx)
{
AnsiString S;
// 溜菴隋粵徹 熱頊痢�� 慣� 彰粵錚繃蒙錙曉� 檀隧蝗羲鈇 � 蜻憫衝粵� ListBox
if (HidDev->ProductName != NULL)
ListBox1->Items->Add("俠溺億雛瘟 檀隧蝗羲隆: "
+ HidDev->ProductName);
else
ListBox1->Items->Add(S.sprintf("Device VID=%04X PID=%04X",
HidDev->Attributes.VendorID, HidDev->Attributes.ProductID));
// 頡骯雛璃� 擱琦痞粱� 熱 檀隧蝗羲隆
JvHidDeviceController1->CheckOutByIndex(Dev, Idx);
// 溜菴隋粵徹 擱琦痞粱� 熱 檀隧蝗羲隆 � 縉撇蝌 檀隧蝗羲�
DevList->Add(Dev);
// 慚蜥粳粵� 慚滇蜴� 頑震縊
ListBox1->ItemIndex ++;
// 頡骯雛璃� 擱琦痞粱� 熱 檀隧蝗羲隆 愿 縉撇蒙 檀隧蝗羲�
TJvHidDevice *Dev = (TJvHidDevice *)DevList->Items[ListBox1->ItemIndex];
S = "USB 檀隧蝗羲隆 螂慮熱 滓瘩籠�";
if (HidDev->ProductName == S){
CurrentDevice = Dev; // 甦錚隅蝦 HID 檀隧蝗羲隆
// 鞣縛� 菴瞄蝴 溫� 頑震縊 � HID 檀隧蝗羲隆
ToWrite = CurrentDevice->Caps.OutputReportByteLength;
Form1->Caption = HidDev->ProductName; // 熱頊痢徹 檀隧蝗羲鈇
if(CurrentDevice != NULL)
CurrentDevice->OnData = NULL;
if(CurrentDevice->HasReadWriteAccess)
CurrentDevice->OnData = ShowRead;
else
CurrentDevice->OnData = NULL;
}
return(true);
}
//****************************************************************************************
void __fastcall TForm1::JvHidDeviceController1DeviceDataError(
TJvHidDevice *HidDev, DWORD Error)
{
MessageDlg("沿池技 餾粵�� 愿 HID. GetLastError = " +
IntToStr(GetLastError), mtError, TMsgDlgButtons() << mbOK, 0);
}
//****************************************************************************************
// 餾粵徹 愿 HID 檀隧蝗羲鈇
void __fastcall TForm1::ShowRead(TJvHidDevice *HidDev,
BYTE ReportID, const void *Data, unsigned __int16 Size)
{
char *P;
P = (char *) Data;
// CurrentDevice->ReadFile(BufIn, 32, BytesRead);
if ((unsigned char)P[2] == 0x55){
Label2->Visible = true;
if (P[0] == 1)
Label1->Font->Color = clRed;
else
Label1->Font->Color = clBlue;
if (P[1] == 1)
Label2->Font->Color = clRed;
else
Label2->Font->Color = clBlue;
Label1->Caption = "�閤� 蜜複蒙 = " + IntToStr((unsigned char)P[0]);
Label2->Caption = "脊曆�� 蜜複蒙 = " + IntToStr((unsigned char)P[1]);
}
}
//****************************************************************************************