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.

相关阅读 >>

Delphi截取字符串的方法

Delphi 固定保持datetime日期格式

Delphi分离汉字和英文字母

Delphi sysem.json 链式写法

base64.pas

Delphi的tstreamreader逐行读取文本文件

Delphi 缓冲文件流-tbufferedfilestream tfilestream 性能测试

Delphi安卓动态切换本地主题

Delphi 刷新系统桌面图标缓存

字符串提取算法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...