2

使用库中Delphi XE2TJvHidDeviceJedi,我设法与 USB 设备(pic32mx7板,我的代码在其上运行)成功通信。“发送请求,等待单个响应”的通常方式有效。

问题在于导致大量连续响应的命令。如果设备尽可能快地发送这些响应 - 或者即使我在它们之间添加一个小的延迟,如 5ms - 我会丢失数据包(报告?帧?)。该OnDeviceData事件似乎并没有为所有人触发。如果我在设备代码中添加更大的延迟,问题就会消失。

我使用USBPcap程序捕获 USB 数据并将其转储到一个文件中,一旦我在 WireShark 中打开它,该文件包含设备发送的所有数据(我发送 255 个数据包作为测试,全零和一个“1”移动它在每个数据包中放置 1 个位置)。所以,我认为设备和 Windows 都在做他们的工作。

为了确保我的 Delphi 代码没有错误,我尝试了 Jedi 示例项目“DevReader”(这里是main.pas 代码),它在屏幕上转储数据并且它也丢失了数据包。

我觉得网上应该有更多关于 Jedi 的 USB 课程的信息,但我很难找到它。

我可以通过聚合/压缩设备的响应来避免这个问题,但仍然想知道发生了什么。

编辑:

  1. 从控制台应用程序尝试:数据包不再丢失。
  2. 修改了 Jedi 演示应用程序以仅计算接收到的数据包并更新屏幕上的计数器标签(无强制窗口重绘) - 无丢失数据包。
  3. 在 OnData 事件中添加了 sleep(1) - 没有丢失数据包。
  4. 在 OnData 事件中添加了 sleep(2) - 再次丢失数据包。

这看起来像读取数据的绝地线程不能被任何处理延迟 - 不应该有一些数据缓冲(通过 Windows 吗?)允许这种类型的处理延迟?从数据包丢失“模式”来看,似乎有缓冲,但这还不够,因为我可以接收例如 30 个数据包然后丢失 5 个然后再接收 20 个等等。

我将修改我的代码以复制数据并尽快退出 OnData 事件,以使线程具有最小的“停机时间”并且我将报告结果。

4

1 回答 1

0

由于问题的原因似乎与USB读取线程被阻塞的时间量Synchronise,即主线程进行的数据处理有关,所以我对线程代码进行了更改,(TJvHidDeviceReadThread类,JvHidControllerClass.pas单元)。任何使用这个单元的代码和包含的类都应该在没有任何修改的情况下仍然可以工作,没有任何公共被改变。

新行为:每次读取数据时,都会将其放入线程安全列表中。它现在使用 Synchronize 而不是 Synchronize Queue,但前提是它尚未排队。Queued 方法从线程安全列表中读取,直到它为空。它为列表中的每个缓冲报告触发一个事件(与旧代码中的事件相同)。一旦列表为空,“Queued”标志被重置,下一次读取将再次导致排队。

在到目前为止的测试中,我没有遇到丢失的数据包。

线程类被扩展:


  TJvHidDeviceReadThread = class(TJvCustomThread)
  private
    FErr: DWORD;

    // start of additions
    ReceivedReports : TThreadList;
    Queued: boolean;
    procedure PushReceivedReport(const bytes: array of byte; const NumBytesRead: cardinal);
    function PopReceivedReport(var ReportID: byte; var ReportBytes: TBytes): boolean;
    procedure FlushBuffer;
    // end of additions

    procedure DoData;
    procedure DoDataError;
    constructor CtlCreate(const Dev: TJvHidDevice);
  protected
    procedure Execute; override;
  public
    Device: TJvHidDevice;
    NumBytesRead: Cardinal;
    Report: array of Byte;
    constructor Create(CreateSuspended: Boolean);
    //added destructor:
    destructor Destroy; override;
  end;

在实施部分,修改了以下内容:

constructor TJvHidDeviceReadThread.CtlCreate(const Dev: TJvHidDevice);
begin
  inherited Create(False);
  // start of changes
  ReceivedReports := TThreadList.Create; 
  // end of changes
  Device := Dev;
  NumBytesRead := 0;
  SetLength(Report, Dev.Caps.InputReportByteLength);
end;

procedure TJvHidDeviceReadThread.Execute;
...
...
...
    //replaced: Synchronize(DoData); with:
    PushReceivedReport (Report, NumBytesRead);
...

并添加了以下内容:

type

TReport = class
  ID: byte;
  Bytes: TBytes;
end;

destructor TJvHidDeviceReadThread.Destroy;
var
  l: TList;
begin
  RemoveQueuedEvents (self);
  try
    l := ReceivedReports.LockList;
    while l.Count>0 do
      begin
        TReport(l[0]).Free;
        l.Delete(0);
      end;
  finally
    ReceivedReports.UnlockList;
    FreeAndNil (ReceivedReports);
  end;

  inherited;
end;

procedure TJvHidDeviceReadThread.FlushBuffer;
var
  ReportID: byte;
  ReportBytes: TBytes;
begin
  while PopReceivedReport (ReportID, ReportBytes) do
        Device.OnData(Device, ReportID, ReportBytes, length(ReportBytes));
end;

function TJvHidDeviceReadThread.PopReceivedReport(var ReportID: byte; var ReportBytes: TBytes): boolean;
var
  l: TList;
  rep: TReport;
begin
  l := ReceivedReports.LockList;
  rep := nil;
  try
    result := l.Count>0;
    if result
      then
        begin
          rep := l[0];
          l.Delete(0);
        end
      else Queued := false;
  finally
    ReceivedReports.UnlockList;
  end;

  if result then
    begin
      ReportID := rep.ID;
      SetLength(ReportBytes, length(rep.Bytes));
      System.move (rep.Bytes[0], ReportBytes[0], length(rep.Bytes));
      rep.Free;
    end;
end;

procedure TJvHidDeviceReadThread.PushReceivedReport(const bytes: array of byte; const NumBytesRead: cardinal);
var
  rep: TReport;
begin
  rep := TReport.Create;
  setlength (rep.Bytes, NumBytesRead-1);
  rep.ID := Bytes[0];
  System.move (Bytes[1], rep.Bytes[0], NumBytesRead-1);

  // explicitely lock the list just to provide a locking mechanism for the Queue flag as well
  ReceivedReports.LockList;
  try
    if not Queued then
      begin
        Queued := true;
        Queue (FlushBuffer);
      end;
    ReceivedReports.Add(rep);
  finally
    ReceivedReports.UnlockList;
  end;
end;
于 2015-05-26T09:08:41.200 回答