所以这就是我想做的。我已经有了一些功能,例如这个可以将数据写入串口,效果很好:
bool WriteData(char *buffer, unsigned int nbChar)
{
DWORD bytesSend;
//Try to write the buffer on the Serial port
if(!WriteFile(hSerial, (void *)buffer, nbChar, &bytesSend, 0))
{
return false;
}
else
return true;
}
读取函数是这样的:
int ReadData(char *buffer, unsigned int nbChar)
{
//Number of bytes we'll have read
DWORD bytesRead;
//Number of bytes we'll really ask to read
unsigned int toRead;
ClearCommError(hSerial, NULL, &status);
//Check if there is something to read
if(status.cbInQue>0)
{
//If there is we check if there is enough data to read the required number
//of characters, if not we'll read only the available characters to prevent
//locking of the application.
if(status.cbInQue>nbChar)
{
toRead = nbChar;
}
else
{
toRead = status.cbInQue;
}
//Try to read the require number of chars, and return the number of read bytes on success
if(ReadFile(hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0)
{
return bytesRead;
}
}
//If nothing has been read, or that an error was detected return -1
return -1;
}
而且无论我用arduino做什么,这个函数总是返回-1,我什至尝试加载一个不断向串口写入字符的代码,但什么也没有。
我从这里得到了功能:http: //playground.arduino.cc/Interfacing/CPPWindows
所以我的功能基本相同。我只是将它们复制到我的代码中,而不是将它们用作类对象,但不仅如此,它是相同的。
所以这是我的问题,我可以将数据写入串行但我无法读取,我可以尝试什么?