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

通过测试,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.


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值