delphi7有关C8051F320与PC的USB通讯的问题:
    通过调用USBXPESS.DLL中的函数,实现读取flush中的数据,其中检测USB的函数正常,但是当使用SI_OPEN和SI_READ时,出现错误,
测试程序部分代码如下:
//-----------------------------------------
typefunction SI_Open(dwDevice: DWORD; cyHandle: PTHANDLE): SI_STATUS; stdcall; 
function SI_Close(Handle: THANDLE): SI_STATUS; stdcall;var
  Form1: TForm1;
  DeviceNum: DWORD;              //  SI_GetProductString()
  h_TmpDevice: THANDLE;          //  USB handleprocedure TForm1.Button1Click(Sender: TObject);
var
   _open, _close, _read:SI_STATUS;
   cyHandle: PTHANDLE;
   Buffer: PBYTE;
   BytestoRade: DWORD;
   BytesReturn: PDWORD;
begin
   //
   DeviceNum:=0;                 // SI_GetProductString(DeviceNum)  
   New(cyHandle);
   New(Buffer);
   New(BytesReturn);
   BytestoRade:=$00;
   //
   _open:=SI_Open(DeviceNum, cyHandle);       //打开
   if _open = SI_SUCCESS then
   begin
      label1.Caption:='SUCCESS';
      h_TmpDevice:=cyHandle^;
      label2.Caption:=inttostr(h_TmpDevice);
      //读数据
      _read:=SI_Read(h_TmpDevice,Buffer,BytestoRade,BytesReturn); ///,o);
      if _read = SI_SUCCESS then
      begin
         label4.Caption:='SUCCESS';
         memo1.Lines.Clear;
         memo1.Lines.Text:=inttostr(buffer^);
      end
      else if _read = SI_INVALID_REQUEST_LENGTH then
      label4.Caption:='INVALID_REQUEST_LENGTH';
      else 
      label4.Caption:='error';
      //
      _Close:=SI_Close(h_TmpDevice);          //关闭
      if _Close = SI_SUCCESS then
      label3.Caption:='SUCCESS';
   end
   else
   label1.Caption:='error';
   Dispose(cyHandle);
   //
   Dispose(Buffer);
   Dispose(BytesReturn);
end;
//------------------------------------------------------------
问题:
1 运行程序,单击按钮后,窗口点击没有反应,但是当拔掉USB后,Label显示状态'SUCCESS';
2 并获得USB的Handle,但是,SI_read的状态Lable显示为'INVALID_REQUEST_LENGTH',且无数据读入。由于之前没有做过串口通讯,本人实属菜鸟,还请高手多多指教!!!谢谢!

解决方案 »

  1.   

    SI_Read(h_TmpDevice,Buffer,BytestoRade,BytesReturn);   我认为此函数的Buffer参数应该是缓冲区的指针,试着多申请一些空间试试New(Buffer); 改成:GetMem(Buffer, 1024); //申请//...FreeMem(Buffer);//释放
      

  2.   

          BytestoRade:=$00; 看这个变量,你自己看看read函数的解释,我觉得它应该是你要读的缓冲区长度,你给0,肯定读不上来东西了。多读几个试试,我读d12 用的windows readfile 函数 你这个封装的函数应该是从这里来的 44先
      

  3.   

    上述问题,第一个已经解决,是硬件时钟的问题。
    现在就SI_READ函数状态显示:"INVALID_REQUEST_LENGTH",分析如下:
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    //SI_READ函数的定义说明:
    Prototype: 
    SI_STATUS SI_Read (HANDLE Handle, LPVOID Buffer, DWORD NumBytesToRead,DWORD *NumBytesReturned, DWORD *NumBytesReturned, OVERLAPPED* o = NULL)
    Parameters: 
    1. Handle—Handle to the device to read as returned by SI_Open.
    2. Buffer—Address of a character buffer to be filled with read data.
    3. NumBytesToRead—Number of bytes to read from the device into the buffer (0–64 kB).
    4. NumBytesReturned—Address of a DWORD which will contain the number of bytes actually read into the buffer on return.
    5. (Optional) o—Address of an initialized OVERLAPPED object that can be used for asynchronous reads.
    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    1 对于Buffer我重新定义了一下:
    var
    Buffer:array[0..4096*16] of BYTE;  
    //
    SI_Read(h_TmpDevice,@Buffer,BytestoRade,BytesReturn);
    //
    2 对于BytestoRade 我的理解应该是我要读的字节数,但是当我赋值非0时,程序就死在SI_READ了.   
      比如:BytestoRade:=16.  现在问题2依然存在,请大虾们多多指教!!!
      

  4.   

    终于在上周五解决了,原来在调用函数时,有一个参数即: OVERLAPPED* o =NULL,资料中说是可选的,所以我在声明的时候也没有加,结果虽然编译的时候不出错,但是返回的枚举变量的值是:SI_SYSTEM_ERROR_CODE,那么我就重新把函数完整的申明了一次:
    function   SI_READ(Handle:THANDLE;Buffer:PBYTEARRAY; NumBytesToRead: DWORD; NumBytesReturned: PDWORD; o: POVERLAPPED = nil):   SI_STATUS;   stdcall;
    //
    SI_Read(h_TmpDevice,@Buffer,BytestoRade,@BytesReturn); 
     
    再次运行的时候,就可以读取数据了!
      

  5.   

    这个地方有一个问题我遇到过,如果下位机没有数据上来的时候,上位机就会停在read这个函数这里一直到下边有数据上来,用一个回掉函数可以解决这个问题,你试试下位机不发数据,上位机什么状态.
      

  6.   

    确实遇到上述问题。
    由于读数据缓存有延时,我是使用while循环判断,缓存内字节大小不再增大为止,不管0或其他大小,只要设别中断后,发送数据没有变化为止,我都能收到。
    因为我也计算过,我的缓存数组有64KB,设备最多只能发32KB,应该都能收到。
    对了,今天刚遇到问题,读完数据没有清零,结果二次读取发生了很多错误。
    最后,想听听你的解决办法。是怎么回调的?
      

  7.   

    我也遇到同样问题,SI_read极慢,silab上说 For synchronous SI_Read() call,
    Check the number of bytes on the RX buffer of USBXpress using SI_CheckRXQueue(),
    and call SI_Read() just when the buffer holds the required number of bytes. In this case, SI_Read() returns immediately even on the synchronous call.
    ORFor synchronous SI_Read() call,
    Set ReadTimeout to 0 by SI_SetTimeouts()
    Check NumBytesReturned of SI_Read() after call, to know the SI_Read() actually get the data.ORUse OVERLAPPED SI_Read() and apply CancelIO() to terminate it.
    我把 原来例子上的 SI_SetTimeouts(5000, 5000);改为 SI_SetTimeouts(0, 5000);立刻就好了
      

  8.   

    购买了silabs的C8051F320开发板,用附带光盘里的UsbXpress的testpandel程序【包括下位机的 和 PC机的】,在运行一段时间后,出现下列问题:
    1、“Error reading from USB.”
    2、分析后,是因为On_timer函数里调用
      status = SI_Read(m_hUSBDevice, &m_IObuffer, dwBytesReadRequest, &dwBytesSucceed)出现的;我想曾经做过这个开发板的大虾,应该也遇到这个问题,不知道怎么解决的。【要求用VC开发上位机程序】
    最后能否做到连续采集12小时以上不死机。