使用c++从Arduino读取串行数据
Reading serial data from an Arduino using C++
这就是我要做的。我已经有了一些功能,例如这个将数据写入串行端口的功能,它工作得很好:
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
所以我的函数基本上是一样的。我只是将它们复制到我的代码中,而不是将它们用作类对象,但除此之外,它们是相同的。
所以这是我的问题,我可以写数据到串行,但我不能读,我能做什么?
对于感兴趣的人来说,我已经解决了这个问题,这是一个愚蠢的错误。我对Arduino进行了编程,以便它在发送任何内容之前等待串行输入。计算机程序编写和发送一行又一行的代码,我猜i7比Atmel快…显然,收集数据需要一些时间。
添加睡眠(10);在从计算机读取端口之前就足够最终读取数据了。
感谢@Matts的帮助。
相关文章:
- C++将文本文件中的数据读取到结构数组中
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 如何将来自文本文件的数据读取到结构的int阵列中
- 如何将不同长度的数据读取到 2D 数组中
- 从TXT文件到C 的结构向量中的数据读取
- 将sqlite原始数据读取到qbyTearray
- 如何将来自输入TXT文件的数据读取到2 C 数组中
- 使用MQQueue将数据读取到缓冲区中时麻烦
- 在 VC++ 中将类数据读取和写入二进制文件
- 将数据从数据读取器终端获取到GUI(QtCreator)
- C 将数据读取到一个结构,但它跳过了我的一个获取线,使该元素空白
- C++将二进制数据读取到结构
- c++我需要将文件中的数据读取到多维数组中,然后用一种数据类型对数组进行排序.怎样
- 对齐数据:速度取决于恒定数据读取的相对采样大小和采样频率
- 如何将文件中的数据读取到数组中
- 如何使用Visual C++应用程序的完整应用程序数据读取Minidump的应用程序数据
- 如何将csv文件数据读取到数组中
- 用C++将格式化的数值数据读取到数组中
- 正在从文件中将数据读取到结构数组中
- 将分离的数据读取到数组结构c++中