1
以下是我用於在Windows XP上與我的arduino進行通信的代碼。我遇到的問題是當我有兩個命令試圖同時訪問端口,這是UnauthroizedAccessException catch語句,它輸出錯誤消息,並且不執行其中一個命令,我該如何去編碼catch語句,這樣,而不是捕捉錯誤甚至能夠進入錯誤的程序來完成的第一個命令,然後用另外一個,像一個隊列執行....C++/CLI嘗試抓取處理
#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->ReadTimeout = 1000;
arduino->WriteLine(command);
String^ result = arduino->ReadLine();
Console::Write(result);
}
}
// close port to arduino
arduino->Close();
}
catch (IO::IOException^ e){
Console::Write("errormessagedisconnected");
}
catch (TimeoutException^ e){
Console::Write("errormessage");
}
catch (UnauthorizedAccessException^ e){
Console::Write("errormessage");
}
catch (ArgumentException^ e){
Console::WriteLine(e->GetType()->Name+": incorrect port name syntax, must start with COM/com");
}
// end program
return 0;
}
你能澄清嗎?我們在談論[this](http://www.arduino.cc/)arduino嗎?你知道哪些函數拋出異常嗎?你有權訪問這些函數的源代碼嗎? – beduin 2011-04-23 06:22:18