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',且无数据读入。由于之前没有做过串口通讯,本人实属菜鸟,还请高手多多指教!!!谢谢!
通过调用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',且无数据读入。由于之前没有做过串口通讯,本人实属菜鸟,还请高手多多指教!!!谢谢!
解决方案 »
- 请教一个问题:拖放到Form上的控件,在Form的Destroy事件中,调用了这个控件的Free
- DELPHI2010 启动时提示 '''' is not a valid integer value.
- 加载图片到Canvas后尺寸打印比例问题
- 谁会用tinydb?
- 给大傻付广告费
- 高分请教:自己觉得有点难,解决不了就当是散分!
- 如何给panel分组
- 急!300分求解如何直接运行TMEMORYSTREAM中的程序而不用先保存再运行?
- 如何禁用或活动系统菜单中的某一项?高手请助我啊
- 在使用Delphi中遇到的一个问题。(对象的变量是什么类型?)
- 如何在一个文本文件里追加新的字符串,在最后一行中插入.谢了,急急,加急
- 请问谁能待做计算机系毕业设计(哈尔滨),价钱面议
现在就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依然存在,请大虾们多多指教!!!
function SI_READ(Handle:THANDLE;Buffer:PBYTEARRAY; NumBytesToRead: DWORD; NumBytesReturned: PDWORD; o: POVERLAPPED = nil): SI_STATUS; stdcall;
//
SI_Read(h_TmpDevice,@Buffer,BytestoRade,@BytesReturn);
再次运行的时候,就可以读取数据了!
由于读数据缓存有延时,我是使用while循环判断,缓存内字节大小不再增大为止,不管0或其他大小,只要设别中断后,发送数据没有变化为止,我都能收到。
因为我也计算过,我的缓存数组有64KB,设备最多只能发32KB,应该都能收到。
对了,今天刚遇到问题,读完数据没有清零,结果二次读取发生了很多错误。
最后,想听听你的解决办法。是怎么回调的?
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);立刻就好了
1、“Error reading from USB.”
2、分析后,是因为On_timer函数里调用
status = SI_Read(m_hUSBDevice, &m_IObuffer, dwBytesReadRequest, &dwBytesSucceed)出现的;我想曾经做过这个开发板的大虾,应该也遇到这个问题,不知道怎么解决的。【要求用VC开发上位机程序】
最后能否做到连续采集12小时以上不死机。