我现在正在研究用C语言与win32进行串行通信。从串口读取数据如下。
DWORD dwEventMask;
DWORD dwSize;
if(!SetCommMask(hSerial, EV_RXCHAR)){
//Error handling
printf("Error Setting Comm Mask \n");
}
if(WaitCommEvent(hSerial, &dwEventMask, NULL))
{
unsigned char szBuf[1024];
DWORD dwIncommingReadSize;
do
{
if(ReadFile(hSerial, &szBuf, 1, &dwIncommingReadSize, NULL) != 0) {
//Handle Error Condition
}
if(dwIncommingReadSize > 0)
{
dwSize += dwIncommingReadSize;
sb.sputn(&szBuf, dwIncommingReadSize);
printf("Reading from port \n");
}
else{
//Handle Error Condition
}
printf("Reading data from port \n");
} while(dwIncommingReadSize > 0);
}
else
{
//Handle Error Condition
}
他们使用 DWORD dwIncommingReadSize 作为 while 条件 (while(dwIncommingReadSize > 0);。
请解释一下如何满足这个条件。对此看不到任何修改。
请再次解释以下部分。
if(dwIncommingReadSize > 0)
{
dwSize += dwIncommingReadSize;
sb.sputn(&szBuf, dwIncommingReadSize);
printf("Reading from port \n");
}
最佳答案
这一行:
if(ReadFile(hSerial, &szBuf, 1, &dwIncommingReadSize, NULL)
将 dwIncommingReadSize
的地址(无论拼写可能多么糟糕)传递给函数,以便它可以将其更改为所需的任何内容。
它类似于:
void fn (int *x) { *x = 42; }
:
int xyzzy = 1;
fn (&xyzzy);
// Here, xyzzy is now 42.
<小时/>
就你的第二个问题而言,如果没有看到更多代码,就很难判断,但看起来它只是为读入的每个数据 block 增加了一个“总大小”变量(加上任何 sb .sputn
应该做)。
这是典型的情况,一次读取可能无法获得您想要的所有数据 - 您只需存储您获得的数据,然后返回获取更多数据。
关于c - 解释DWORD在串行通信中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10546784/