我在虚拟串行端口中使用 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 有问题,代码没有像我预期的那样读取,我尝试使用 super 终端并且工作完美。
我已经在dcb结构中定义了:
dcb.fNull = false;
但我仍然遇到同样的问题,有什么帮助吗?
最佳答案
问题似乎不在 ReadFile() 中,而是在您对 tmp[] 的使用中,因为终止 '\0' 恰好也是 0x00。
“读起来不像我预期的那样”是什么意思?你能更详细地描述一下症状吗?
关于c++ - C++ 中的 ReadFile、COM 和 NULL 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2325616/