c++ 中的 ReadFile、COM 和 NULL 字符

发布于 2024-08-22 23:09:21 字数 562 浏览 6 评论 0原文

我在虚拟串行端口中使用 ReadFile 函数时遇到问题:

char tmp[128];
int multiplo=0;
DWORD err;
COMSTAT stt;

ClearCommError(hcom, &err, &stt);
do{
    if(ReadFile(hcom, tmp, stt.cbInQue, &err, NULL)){
        tmp[err] = '\0';
        memcpy(bfIn+multiplo, tmp, err);
        multiplo = multiplo + err;
    }else
        return 0;
}while(err > 0);

当 ReadFile 获得有效字符(如 0x01、0x02、0x03...)时,此代码有效,但 0x00 存在问题,代码读起来不像我预期的那样,我尝试与超级终端,效果完美。

我已经在 dcb 结构中定义了:

dcb.fNull = false;

但我仍然遇到同样的问题,有帮助吗?

I have a problem with ReadFile function in a virtual serial port:

char tmp[128];
int multiplo=0;
DWORD err;
COMSTAT stt;

ClearCommError(hcom, &err, &stt);
do{
    if(ReadFile(hcom, tmp, stt.cbInQue, &err, NULL)){
        tmp[err] = '\0';
        memcpy(bfIn+multiplo, tmp, err);
        multiplo = multiplo + err;
    }else
        return 0;
}while(err > 0);

this code works when ReadFile get valid character like 0x01, 0x02, 0x03... but there is a problem with 0x00, the code doesn't read like I expected, I try with hyperterminal and that works perfect.

I've defined in dcb structure:

dcb.fNull = false;

but still I have the same problem, any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如此安好 2024-08-29 23:09:21

问题似乎不在 ReadFile() 中,而是在您对 tmp[] 的使用中,因为终止 '\0' 也恰好是 0x00。

“读起来​​不像我预期的那样”是什么意思?您能更详细地描述一下症状吗?

The problem seems to be not in ReadFile() but rather in your use of tmp[] as the terminating '\0' happens to be 0x00, too.

What do you mean by "doesn't read like I expected"? Can you describe the symptoms in more detail?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文