熱門文章

2013年2月27日

請確認您的 Main 函式上已經標記有 STAThreadAttribute

其他資訊: 目前的執行緒必須先設為單一執行緒 Apartment (STA) 模式,才能進行 OLE 呼叫。請確認您的 Main 函式上已經標記有 STAThreadAttribute。 這個例外狀況只有在偵錯工具附加至處理序時會引發。


出現這問題時請在main()上方加入[STAThreadAttribute]


[STAThreadAttribute]
int main(array<System::String ^> ^args)


在編譯一次就會正常了



2013年2月25日

型別或是結構出現模稜兩可的符號



在使用uning namespace 時會遇到當兩個命名空間重複使用同一個宣告型別或是結構時編譯器將會無法辨識而出現模稜兩可的符號。
解決方法可使用

  • typedef 指令重新定義名稱
typedef cv::Point Point;
Point(1,1);

  • 在型別前面加上命名空間
cv::Point(1,1);

端看使用者如何使用

2013年2月15日

簡易的網路PING程式


簡易的網路PING程式


ping是:一個電腦網路工具,用來測試特定主機能否通過IP到達。ping的運作原理是:向目標主機傳出一個ICMPecho要求封包,等待接收echo回應封包。程式會按時間和反應成功的次數,估計失去封包率(丟包率)和封包來回時間(網路時延)(Round-trip delay time)。

1983年12月,Mike Muuss寫了這個程式,在IP網路出問題時方便找出其根源。因為這個程式的運作和潛水艇的聲納相似,他便用聲納的聲音來為程式取名。David L. Mills曾提出另一個取名:Packet Internet Grouper/Gopher(後者指地鼠)。

網路管理員之間通常把ping用作動詞,如「ping一下電腦X,看他是否開著。」




// ConsolePing.cpp: 主要專案檔。

#include "stdafx.h"

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Net;
using namespace System::Net::NetworkInformation;
using namespace System::Threading;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Console Ping App");
Console::WriteLine(L"Command PING,Ping,ping,exit");
Console::WriteLine(L"ex:Ping 192.168.1.1");
Console::WriteLine(L"ex:Ping 192.168.1.5-50");
Console::WriteLine(L"ex:Ping 192.168.1.1-all");
while (true)
{
Console::WriteLine(L"請輸入命令");
String ^strCommand = Console::ReadLine();
if (strCommand->ToUpper()->Contains("EXIT") == true)
{
Console::WriteLine(L"Exit");
break;
}
else if (strCommand->ToUpper()->Contains("PING") == true)
{
//MessageBox::Show("Command is True");
array<Char> ^delimiterChars = gcnew array<Char>{' ','.', '-'};
array<String^> ^strSplitCmd = strCommand->Split(delimiterChars);
if (strSplitCmd->Length == 5 || strSplitCmd->Length == 6)
{
if (strSplitCmd->Length == 5)
{
String ^IP = gcnew String(strSplitCmd[1]+"."+strSplitCmd[2]+"."+strSplitCmd[3]+"."+strSplitCmd[4]);
Ping ^ping = gcnew Ping();
for (int i = 0; i < 4; i++)
{
PingReply ^pingreply = ping->Send(IP);//IP Address
if (pingreply->Status.ToString() == "Success")
Console::WriteLine("IP : "+IP+" Status : "+pingreply->Status.ToString()+" Time : "+pingreply->RoundtripTime.ToString());
else
Console::WriteLine("IP : "+IP+" Status : 主機無回應");
}
}
else
{
if (strSplitCmd[5] != "all")
{
Console::WriteLine(L"區段 ip");
for (int i = Convert::ToInt32(strSplitCmd[4]); i <= Convert::ToInt32(strSplitCmd[5]); i++)
{
String ^IP = gcnew String(strSplitCmd[1]+"."+strSplitCmd[2]+"."+strSplitCmd[3]+"."+i);
Ping ^ping = gcnew Ping();
PingReply ^pingreply = ping->Send(IP);//IP Address
if (pingreply->Status.ToString() == "Success")
Console::WriteLine("IP : "+IP+" Status : "+pingreply->Status.ToString()+" Time : "+pingreply->RoundtripTime.ToString());
else
Console::WriteLine("IP : "+IP+" Status : 主機無回應");
}
}
else
{
Console::WriteLine(L"所有 ip");
for (int i = Convert::ToInt32(strSplitCmd[4]); i <= 254; i++)
{
String ^IP = gcnew String(strSplitCmd[1]+"."+strSplitCmd[2]+"."+strSplitCmd[3]+"."+i);
Ping ^ping = gcnew Ping();
PingReply ^pingreply = ping->Send(IP);//IP Address
if (pingreply->Status.ToString() == "Success")
Console::WriteLine("IP : "+IP+" Status : "+pingreply->Status.ToString()+" Time : "+pingreply->RoundtripTime.ToString());
else
Console::WriteLine("IP : "+IP+" Status : 主機無回應");
}
}
}
}
else
{
MessageBox::Show("Command is False");
Console::WriteLine(L"Console Ping App");
Console::WriteLine(L"Command PING,Ping,ping,exit");
Console::WriteLine(L"ex:Ping 192.168.1.1");
Console::WriteLine(L"ex:Ping 192.168.1.5-50");
Console::WriteLine(L"ex:Ping 192.168.1.1-all");
}
}
else
{
MessageBox::Show("Command is False");
Console::WriteLine(L"Console Ping App");
Console::WriteLine(L"Command PING,Ping,ping,exit");
Console::WriteLine(L"ex:Ping 192.168.1.1");
Console::WriteLine(L"ex:Ping 192.168.1.5-50");
Console::WriteLine(L"ex:Ping 192.168.1.1-all");
}
}
    return 0;
}

2013年2月10日

C++/CLI With OpenCV

OpenCV過去多半是在C/C++環境下運行,當使用.Net Framework的CLR語言想使用OpenCV將透過第三方編譯的Emgu CV函式庫來做匯入編譯。
C++/CLI這時就有其優勢可同時使用managed與unmanaged寫法,首先先到OpenCV官方下載最新版的OpenCV Lib 以及Intel TBB

解壓縮OpenCV-2.4.3.exe後將會產生一個opencv資料夾,裡面將會使用到
  1. opencv\build\include
  2. opencv\build\x86\vc10\lib
  3. opencv\build\x86\vc10\bin
解壓縮tbb41_20130116oss_win.zip後將會產生一個tbb41_20130116oss資料夾,裡面將會使用到
  1. C:\Users\Henry\Desktop\tbb41_20130116oss\lib\ia32\vc11\tbb_debug.lib
  2. ※vc11 for Visual Studio2012 根據自己IDE版本選擇
開啟一個新的CLR主控台專案,並把opencv資料夾內的include與lib資料夾複製到專案資料夾內(同cpp與h檔同位置)。
再把tbb41_20130116oss內的tbb_debug.lib放置專案內剛複製的lib資料夾。

在Include目錄及程式庫目錄增加上面路徑之目錄



在程式內載入兩個.H檔
#include "include\opencv\cv.h"
#include "include\opencv\highgui.h"

在連結器部分其他相依性加入

opencv_calib3d243.lib
opencv_contrib243.lib
opencv_core243.lib
opencv_features2d243.lib
opencv_imgproc243.lib
opencv_flann243.lib
opencv_highgui243.lib
opencv_objdetect243.lib
tbb_debug.lib



程式部分輸入

#include "stdafx.h"
#include "include\opencv\cv.h"
#include "include\opencv\highgui.h"
using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
IplImage *Image = cvLoadImage("img_1357809933.jpg");
cvNamedWindow("Show Image",CV_WINDOW_AUTOSIZE);
cvShowImage("Show Image",Image);
cvWaitKey(0);
cvReleaseImage(&Image);
cvDestroyWindow("Show Image");
    return 0;
}

並把opencv\build\x86\vc10\bin內所有DLL檔案複製到Debug或是Release資料夾內
也把檔名"img_1357809933.jpg"的圖片放置專案資料夾內
  1. 如果是從Visual Studio直接編譯執行圖片請放置專案名稱資料夾(同cpp與h檔同位置)
  2. 如果從編譯完的執行檔執行請放到與執行檔同資料夾
執行結果如下
(照片為網路轉載)













2013年2月9日

Visual Studio 2012 C++/CLI Create Windows Form

Visual Studio 2012 在C++/CLI (C++ .Net)  在創建專案時並不會出現Visual Studio 2010之前的Windows Form 樣板選項,所以在這裡將以Console mode main()運行Form類別。

1.打開Visual Studio 2012 後點選CLR主控台模式
1.Create new CLR ConsoleApplication project
2.在專案內新增Windows Form類別
2.Add new Class and select Windows form in the project
3.此時專案將會新增一個Form
3.Now, have a new form in the project
4.在專案屬性>連結器>進階>進入點 輸入main意即程式將以main()函式運行
4.At Project>Property>Linker>Advanced>Entry  keyin main,the app will start run in main()
5.在專案屬性>連結器>系統>子系統>Windows (/SUBSYSTEM:WINDOWS)
5.At Project>Property>System>Subsystem>Windows (/SUBSYSTEM:WINDOWS)
6.在ConsoleApplication1.cpp程式內輸入
6.Keyin program in ConsoleApplication1.cpp


#include "stdafx.h"
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
ConsoleApplication1::MyForm form1;
Application::Run(%form1);
    return 0;
}

7.編譯過後將得到一個Windows Form
7.Now, you get a form

2011年5月20日

PIC、dsPIC CCS編譯器

站長在部落格所使用的PIC、dsPIC 編譯器均為CCS公司設計的編譯器
附上連結與使用手冊連結


CCS inc Home
http://www.ccsinfo.com/

CCS Forum
http://www.ccsinfo.com/forum/

PCB, PCM, PCH, PCW & PCWH Reference Manual
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf


PCD, PCDIDE & PCWHD Reference Manual
http://www.ccsinfo.com/downloads/PCDReferenceManual.pdf