频道分类

D10环境下调用HidController1.0.35获取数据乱码问题

作者:admin 来源: 日期:2019/4/13 10:59:30 人气: 标签:

 
通过测试,HidController1.0.35在delphi10.2下可以正常编译。使USB HID设备的联机不再困难,但ONDATA事件中,DATA数据字符串为单字节,D10的widestring显示会出现乱码
因此,应在使用前使用shortstring进行强制转换,变为单字节,问题解决


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,StdCtrls, JvHidControllerClass, JvComponentBase;

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(shortstring(pchar(data)));
        edit1.Text:=floattostr(HexToDec(copy(shortstring(pchar(data)),6,4))/128);
        edit2.Text:=floattostr(HexToDec(copy(shortstring(pchar(data)),10,4))/128);
        edit3.text:=floattostr(HexToDec(copy(shortstring(pchar(data)),14,2)));
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Clear;
end;

end.