串口的WriteFile問題

wingslee   2008-3-17 16:26 楼主

BOOL WINAPI WriteFile(
  __in         HANDLE hFile,
  __in         LPCVOID lpBuffer,
  __in         DWORD nNumberOfBytesToWrite,
  __out_opt    LPDWORD lpNumberOfBytesWritten,
  __inout_opt  LPOVERLAPPED lpOverlapped
);

WriteFile 返回TRUE
这时候应该表示数据是已经通过串口发送出去了

可是"lpNumberOfBytesWritten"却是0
说明我传送了0 byte
倒底是有没有成功传送??

回复评论 (4)

返回TRUE表示调用WriteFile成功。以下是MSDN中对于lpNumberOfBytesWritten的说明:
lpNumberOfBytesWritten
[out] Pointer to the variable that receives the number of bytes written. WriteFile sets this value to zero before doing any work or error checking.
If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL. If lpOverlapped is not NULL, lpNumberOfBytesWritten can be NULL. If this is an overlapped write operation, you can get the number of bytes written by calling GetOverlappedResult. If hFile is associated with an I/O completion port, you can get the number of bytes written by calling GetQueuedCompletionStatus.

If I/O completion ports are used and you are using a callback routine to free the memory allocated to the OVERLAPPED structure pointed to by the lpOverlapped parameter, specify NULL as the value of this parameter to avoid a memory corruption problem during the deallocation. This memory corruption problem will cause an invalid number of bytes to be returned in this parameter.

点赞  2008-3-17 20:52
这跟打开串口的方式有关。
点赞  2008-3-17 20:53
所以~
返回TRUE表示调用WriteFile成功
并不能表示数据是已经通过串口发送出去了
可以这样解怿吗??
谢谢~

点赞  2008-3-18 08:59
点赞  2008-3-18 10:21
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复