Hidkomponente using read-write HID Devices

Source: Internet
Author: User



Source: Hidkomponente Using read-write HID devices at a glance



Hidkomponente is a third-party HID Control Library used in Delphi to detect and control HID devices connected to your computer. In general, more USB devices. The use of Hidkomponente is actually very simple, just because of the first use, encountered some problems. Its demo is very rich, very practical. To use the Hidkomponente library, you only need to install HIDCONTROLLER.DPK and add Reference jvhidcontrollerclass to the cell. Use example: Place a tjvhidcontroller on the form. The events we are going to use are:


 
 OnArrival
        OnDeviceChange
        OnDeviceData
        OnRemoval


Other events, which are not used here.          Of course, this is a prerequisite: you want to make sure that the data you transmit is correct, that is, the invalid data cannot appear. In the Hidkomponente Library, Tjvhiddevice represents a device, a HID device. With this object you can get details about the device. For example, HidDev.Attributes.VendorID represents the device's VID, HidDev.Attributes.ProductID represents the device's PID, and so on In the arrival event, you can specify the devices we need


if (HidDev.Attributes.VendorID = USB_VID) and (HidDev.Attributes.ProductID = USB_PID) then
Begin
Addlog (format ('device installed. [% s;% s] ', [hiddev.productname, hiddev. Serialnumber]));
End;


Checkout the device in the Ondevicechange event.


if HidDevs.CheckOutByID(FHidDev, USB_VID, USB_PID) then
    begin FHidDev.NumInputBuffers := SizeOf(TReport);
        FHidDev.NumOverlappedBuffers := SizeOf(TReport); end;


What is checkout? Checkout has 7 overloaded methods to view Jvhidcontrollerclass source or Help files. The function of this method is to let your application control the device. After checkout, Tjvhiddevice (Fhiddev in the example) turns on the read thread tjvhiddevicereadthread. The Tjvhiddevicereadthread thread is used to read the report in Tjvhiddevice. You can see that Tjvhiddevice is an asynchronous operation. Now that you are reading the data asynchronously, look at the Execute method of Tjvhiddevicereadthread, and you can see that if you read the data, the Dodata method is called, and this method calls the device's OnData property event. The OnData is assigned a value of dataevent (see: Procedure tjvhiddevicecontroller.setdevdata (const dataevent:tjvhiddataevent)), so the receipt of the data          (read), we use the Ondevicedata event directly. The Onremoval event is triggered after the HID device is unplugged. Here we want to release the controls on the device, CheckIn.


 
 
if (HidDev.Attributes.VendorID = USB_VID) and (HidDev.Attributes.ProductID = USB_PID) then
Begin
if (Assigned(FHidDev)) and (not FHidDev.IsPluggedIn) then
Begin
HidDevs.CheckIn(FHidDev);
End;
FHidDev := nil;
DeviceEnabled;
Addlog ('device removed ', lterror);
End;


Basically, almost. Next, write the data.     Writing data using Tjvhiddevice's WriteFile method, of course, there are other methods. WriteFile (var report; Towrite:dword; var byteswritten:dword) The first parameter is a byte array, typically set to 65 bytes. Such as:


 TReport = packed record
            ReportID: byte;
            Data: array[0..64] of byte;
        end;


ReportID is generally 0. The second parameter is towrite to the length of the data that needs to be written. The Device.Caps.OutputReportByteLength third parameter is generally used to byteswritten the byte data that is actually written.          This value is generally ignored. After writing is read, using Tjvhiddevice's ReadFile (or other method) read with Tjvhidcontroller Ondevicedata read the difference is that the Ondevicedata event reads the data to ignore the first byte,     That is ReportID. If you use ReadFile (or other methods) with Ondevicedata, the first is ReadFile and then Ondevicedata. The ReadFile method does a read, and Ondevicedata continuously reads the data.          If the report returned after sending a command is too large (greater than the set inputreportbytelength), it is best to read the data using the Ondevicedata event. Note: The size of the report needs to be defined for data communication, which is provided by the device. It is necessary to use packets of this size when entering or outputting data, otherwise tjvhidcontroller or tjvhiddevice will fail to read the data when it is read.



HID Device Data Detection Tool Bus hound






Hidkomponente using read/write HID devices (RPM)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.