ReadFile()问题。。。

leonguo_1986 2008-04-24 11:48:44
小弟我想做个串口通讯小程序。现在碰点问题

//向COM写入数据
void COMWriter(string * _strTemp){

DWORD wCount = (DWORD)strlen(_strTemp->data());

cout<<"the Data is: "<<_strTemp->data()<< " size:"<<wCount<<endl;
BOOL SendState = WriteFile(hCom, _strTemp, wCount, &wCount, NULL);

SetFilePointer(hCom, 0, 0, FILE_BEGIN);

cout<<"SendState: " << SendState << " (ErrorNO:"<< (DWORD)GetLastError()
<< ") " << endl;

}//以上都没有问题的。

然后是读取

void COMLister(){
char c_str[100] = {0};
DWORD wCount;
while(1){
//就是这里无法读取
ReadFile(hCom, c_str, 100, &wCount, NULL);
//因为以下的err错误号无法打出。
DWORD err = GetLastError();

cout<<"err" <<err <<endl;
//cout<< c_str << endl;
if (wCount > 0)
{
cout<<"Lister"<< c_str <<endl;
}

}

就是这里的ReadFile(hCom, c_str, 100, &wCount, NULL); 的方法。
请知道的朋友能教教我吗?谢谢了
...全文
197 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
不会,帮顶
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
什么呀
leonguo_1986 2008-04-24
  • 打赏
  • 举报
回复
是吗?但就是不行,下不下去了。
会不会和我CreateFile的参数又关呢?
FILE_ATTRIBUTE_NORMAL, // WriteFile正常
但换成
FILE_FLAG_OVERLAPPED // WriteFile错误号87

会不会这也又关系呢?
Treazy 2008-04-24
  • 打赏
  • 举报
回复
err看不到?
  • 打赏
  • 举报
回复
看代码是没问题的
systemthink 2008-04-24
  • 打赏
  • 举报
回复
If part of the file is locked by another process and the read operation overlaps the locked portion, this function fails.

Accessing the input buffer while a read operation is using the buffer may lead to
corruption of the data read into that buffer. Applications must not read from,
write to, reallocate, or free the input buffer that a read operation is using until
the read operation completes.
When reading from a communications device, the behavior of ReadFile is governed by the
current communication time-outs as set and retrieved using the SetCommTimeouts and
GetCommTimeouts functions. Unpredictable results can occur if you fail to set the
time-out values. For more information about communication time-outs, see COMMTIMEOUTS.
The ReadFile function may fail and return ERROR_INVALID_USER_BUFFER or ERROR_NOT_ENOUGH_MEMORY whenever there are too many outstanding asynchronous I/O requests.

webipstin 2008-04-24
  • 打赏
  • 举报
回复
hCom值对吗
leonguo_1986 2008-04-24
  • 打赏
  • 举报
回复
么有人知道吗?

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧