安装HIDcontroller控件。
1、用USB设备名获取VID,PID,建立临时缓冲区。
2、checkout建立usb设备的线程
3、拔插下来checkin销毁线程
4、通过ondata事件获取数据指针
5、通过数据指针对16进制数进行转换10进制,并处理显示
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls, JvHidControllerClass;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Memo1: TMemo;
JvHidDeviceController1: TJvHidDeviceController;
procedure JvHidDeviceController1Arrival(HidDev: TJvHidDevice);
procedure JvHidDeviceController1DeviceChange(Sender: TObject);
procedure JvHidDeviceController1Removal(HidDev: TJvHidDevice);
procedure JvHidDeviceController1DeviceData(HidDev: TJvHidDevice;
ReportID: Byte; const Data: Pointer; Size: Word);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
a:TJvHidDevice;
USB_VID,USB_PID:word;
function HexToDec(const AHexString: String): Integer;
implementation
{$R *.dfm}
function HexToDec(const AHexString: String): Integer;
begin
Result := StrToInt('$' + AHexString);
end;
procedure TForm1.JvHidDeviceController1Arrival(HidDev: TJvHidDevice);
begin
if HidDev.ProductName='OMRON Blood pressure monitor' then
begin
memo1.Lines.Add(Format('设备连接成功.【%s ; %s】', [HidDev.ProductName, HidDev.SerialNumber]));
memo1.Lines.Add(Format('VID:%.4x/PID:%.4x', [HidDev.Attributes.VendorID,HidDev.Attributes.ProductID]));
USB_VID:=HidDev.Attributes.VendorID;
USB_PID:=HidDev.Attributes.ProductID;
end;
end;
procedure TForm1.JvHidDeviceController1DeviceChange(Sender: TObject);
begin
if JvHidDeviceController1.CheckOutByID(a, USB_VID, USB_PID) then
begin
a.NumInputBuffers := 128;
a.NumOverlappedBuffers := 128;
end;
end;
procedure TForm1.JvHidDeviceController1Removal(HidDev: TJvHidDevice);
begin
if (HidDev.Attributes.VendorID = USB_VID) and (HidDev.Attributes.ProductID = USB_PID) then
begin
if (Assigned(a)) and (not a.IsPluggedIn) then
begin
JvHidDeviceController1.CheckIn(a);
end;
a := nil;
//DeviceEnabled;
memo1.Lines.Add('设备已移除');
end;
end;
procedure TForm1.JvHidDeviceController1DeviceData(HidDev: TJvHidDevice;
ReportID: Byte; const Data: Pointer; Size: Word);
begin
if (HidDev.Attributes.VendorID = USB_VID) and (HidDev.Attributes.ProductID = USB_PID) then
begin
memo1.Lines.Add(Format('收到数据.【%s ; %s】', [HidDev.ProductName, HidDev.SerialNumber]));
memo1.Lines.Add(pchar(data));
edit1.Text:=floattostr(HexToDec(copy(pchar(data),6,4))/128);
edit2.Text:=floattostr(HexToDec(copy(pchar(data),10,4))/128);
edit3.text:=floattostr(HexToDec(copy(pchar(data),14,2)))
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
end;
end.