下面的代码是我用来从我的Arduino发送和接收信息的代码。我的问题是当Arduino第一次插入时,从中读取数据会挂起,因为命令没有返回任何内容,因为这里没有任何内容,所以我的整个程序崩溃了。如何将读取功能添加到arduino->ReadLine();
,导致此问题?这种方式会在一秒之后继续下去吗?C++与Arduino的串行通信超时
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace System::IO::Ports;
int main(int argc, char* argv[])
{
using namespace std;
String^ portName;
int baudRate=9600;
portName="COM4";
// Arduino settings.
SerialPort^ arduino;
arduino = gcnew SerialPort(portName, baudRate);
// Open port.
try
{
arduino->Open();
{
if (strcmp(argv[1],"-send")==0) {
String^ command = gcnew String(reinterpret_cast<const char*>(argv[2]));
if (String::Compare(command,"int6")==0) {
arduino->Write("^");
}
else
arduino->Write(command);
}
if(strcmp(argv[1],"-get")==0) {
String^ command = gcnew String(reinterpret_cast<const char*>(argv[2]));
arduino->WriteLine(command);
String^ result = arduino->ReadLine();
Console::Write(result);
}
}
完美地工作!谢谢! – user541597 2011-04-19 23:46:29