TIPS/Programming/SerialCommunication

Visual C++ 2008 繧堤畑縺縺溘す繝ェ繧「繝ォ騾壻ソ。繧オ繝ウ繝励Ν


讎りヲ

縺薙ョ繝壹シ繧ク縺ッ縲`icrosoft Visual C++ 2008 繧堤畑縺縺ヲ縲√す繝ェ繧「繝ォ騾壻ソ。繧定。後≧縺溘a縺ョ繧オ繝ウ繝励Ν繧ス繝輔ヨ縺ァ縺吶 縺薙ョ繧オ繝ウ繝励Ν繝励Ο繧ー繝ゥ繝縺ッ縲∽ク玖ィ倥ョ讖溯ス繧呈戟縺」縺ヲ縺縺セ縺

  • SerialPort 繧ウ繝ウ繝医Ο繝シ繝ォ繧剃スソ縺
  • 荳陦後★縺、縺ョ縲√ユ繧ュ繧ケ繝医ョ繝シ繧ソ縺ョ騾∽ソ。縺ィ蜿嶺ソ。
  • 譛牙柑縺ェ繝昴シ繝亥錐縺ョ蛻玲嫌
  • 繝昴シ繝亥錐繝サ繝懊シ繝ャ繝シ繝医↑縺ゥ縺ョ繝ェ繧ケ繝医懊ャ繧ッ繧ケ縺九i縺ョ驕ク謚

蝓コ譛ャ蜍穂ス

縺薙ョ繧オ繝ウ繝励Ν繝励Ο繧ー繝ゥ繝縺ッ縲∽ク玖ィ倥ョ繧医≧縺ォ蜍穂ス懊@縺セ縺吶

  • 縲梧磁邯壹阪懊ち繝ウ繧呈款縺吶→縲√昴シ繝亥錐繝サ繝懊シ繝ャ繝シ繝医↑縺ゥ縺ョ險ュ螳壹ム繧、繧「繝ュ繧ー繧定。ィ遉コ縺吶k縲
  • 險ュ螳壹ム繧、繧「繝ュ繧ー縺ォ縺ヲ縲∽ク願ィ俶ュ蝣ア繧定ィュ螳壹☆繧九
  • SerialPort 繧ウ繝ウ繝医Ο繝シ繝ォ縺ォ縲∝ソ隕√↑諠蝣ア繧定ィュ螳壹☆繧九
  • 繝繧ュ繧ケ繝医懊ャ繧ッ繧ケ荳翫↓譁蟄怜励r蜈・蜉帙@縲碁∽ソ。縲阪懊ち繝ウ繧呈款縺吶→縲√ョ繝シ繧ソ縺碁∽ソ。縺輔l繧九
  • 繧キ繝ェ繧「繝ォ繝繝シ繧ソ繧貞女菫。縺吶k縺ィ縲∝牡繧願セシ縺ソ縺檎匱逕溘@蜿嶺ソ。繝舌ャ繝輔ぃ縺ォ蜿嶺ソ。繝繝シ繧ソ繧呈シ邏阪
  • 蜿嶺ソ。縺励◆譁蟄励ッ縲√す繝ェ繧「繝ォ繝昴シ繝井ク翫↓繧ィ繧ウ繝シ繝舌ャ繧ッ縺輔l繧九
  • 謾ケ陦梧枚蟄励r蜿嶺ソ。縺吶k縺ィ縲√ユ繧ュ繧ケ繝医懊ャ繧ッ繧ケ荳翫↓蜿嶺ソ。縺励◆譁蟄怜励r陦ィ遉コ縺吶k縲

笳上せ繧ッ繝ェ繝シ繝ウ繧キ繝ァ繝繝 繝。繧、繝ウ逕サ髱「縺ィ繝上う繝代シ繧ソ繝シ繝溘リ繝ォ

programmingtips_serial_mainwindow.png

險ュ螳壹ム繧、繧「繝ュ繧ー

programmingtips_serial_configwindow.png

螳溯。檎腸蠅(遘√′繝繧」繝舌ャ繧ー縺励※縺縺溽腸蠅縺ァ縺)

  • OS: WindowsXP SP3
  • 髢狗匱迺ー蠅シ Microsoft Visual C++ 2008 Express Edition

蛻ゥ逕ィ譚。莉カ縺ェ縺ゥ

縺薙ョ繧オ繝ウ繝励Ν繧剃スソ縺荳翫〒縲∫音縺ォ蛻カ髯舌ッ縺斐*縺縺セ縺帙s縲ゅ%縺ョ繧ス繝輔ヨ繧剃スソ縺」縺滉コ九↓繧医k謳榊ョウ縺ェ縺ゥ縺ッ縲∽ス懆縺ッ荳蛻髢「遏・縺励∪縺帙s縺ョ縺ァ縲√#莠謇ソ縺上□縺輔>縲

縺薙ョ繧オ繝ウ繝励Ν繧剃スソ縺」縺ヲ菴輔i縺九ョ繧ス繝輔ヨ繧剃ス懊▲縺ヲ蜈ャ髢九@縺溘j縲∵・ュ蜍吶〒菴ソ逕ィ縺吶k縺薙→縺ォ繧ゅ→縺上↓蛻カ髯舌ッ縺斐*縺縺セ縺帙s縲ゅ◆縺縺励√◎縺ョ蝣エ蜷医ッ逶ク莠偵Μ繝ウ繧ッ縺ェ縺ゥ縺ァ邏ケ莉九@縺ヲ縺縺溘□縺代k縺ィ螫峨@縺縺ァ縺吶

繝繧ヲ繝ウ繝ュ繝シ繝

荳玖ィ倥ョ繝輔ぃ繧、繝ォ繧偵ム繧ヲ繝ウ繝ュ繝シ繝峨@縺ヲ縺上□縺輔>縲 Visual C++ 2008 縺ョ繧オ繝ウ繝励Ν繝励Ο繧ク繧ァ繧ッ繝医r荳ク縺斐→ZIP縺励※縺縺セ縺吶ゅム繧ヲ繝ウ繝ュ繝シ繝牙セ後∬ァ」蜃阪@縺ヲ繧ス繝ェ繝・繝シ繧キ繝ァ繝ウ繝輔ぃ繧、繝ォ繧帝幕縺縺ヲ縺上□縺輔>縲

SerialCommSample010.zip(3.1MB)

繧ス繝シ繧ケ繧ウ繝シ繝我ク驛ィ隱ャ譏

縺薙%縺ァ縺ッ縲√た繝シ繧ケ繧ウ繝シ繝峨ョ荳驛ィ繧偵°縺縺、縺セ繧薙〒隱ャ譏弱@縺セ縺吶りゥウ邏ー縺ッ縲∽ク願ィ倥ヵ繧。繧、繝ォ繧偵ム繧ヲ繝ウ繝ュ繝シ繝峨@縺ヲ縺上□縺輔>縲

SerialPort 繧ウ繝ウ繝医Ο繝シ繝ォ繧堤畑縺縺滄∝女菫。

private: String^ COMRevieveBuffer; // 蜿嶺ソ。繝舌ャ繝輔ぃ
//繝繝ェ繧イ繝シ繝医☆繧九
delegate void SerialDataRecievedDelegate(String^ recvData);
private: void SerialDataRecieved(String^ recvData){
			//蛻・繧ケ繝ャ繝繝峨〒襍キ蜍輔&繧後k髢「謨ー縲
			textRecieved->Text =  recvData;
		 }
//繧キ繝ェ繧「繝ォ蜿嶺ソ。蜑イ繧願セシ縺ソ縲ゑシ代ヰ繧、繝亥女菫。縺吶k縺斐→縺ォ縲√%縺ョ髢「謨ー縺後h縺ウ縺縺輔l繧九
private: System::Void serialPort_DataReceived(System::Object^  sender,  System::IO::Ports::SerialDataReceivedEventArgs^  e) {
			SerialDataRecievedDelegate^ dlgte = gcnew    SerialDataRecievedDelegate( this, &SerialCommSample::Form1::SerialDataRecieved );
			String^ RecievedData = serialPort->ReadExisting();
			//1繝舌う繝医★縺、蜿嶺ソ。縺吶k縺薙→繧貞燕謠
			if(serialPort->IsOpen == true) {	//繧キ繝ェ繧「繝ォ繝昴シ繝医′髢九>縺ヲ縺繧九%縺ィ繧堤「コ隱阪@縺ヲ縺九i縲
			COMRevieveBuffer = COMRevieveBuffer + RecievedData; //蜿嶺ソ。繝舌ャ繝輔ぃ縺ォ霑ス蜉
			//謾ケ陦後さ繝シ繝峨r讀懷コ縺吶k
			if(RecievedData == "\r" ) {			
				RecievedData = RecievedData + "\n";
				COMRevieveBuffer = COMRevieveBuffer + RecievedData;
				//蜿嶺ソ。縺励◆荳陦後r繝繧ュ繧ケ繝医懊ャ繧ッ繧ケ縺ォ霆「騾√☆繧九ゅユ繧ュ繧ケ繝医懊ャ繧ッ繧ケ縺ョ謫堺ス懊ッ蛻・繧ケ繝ャ繝繝峨ョ髢「謨ー縺ォ縺吶k縲
				this->Invoke(dlgte, COMRevieveBuffer);
				COMRevieveBuffer = "";
			}
			if(RecievedData == "\n" ) {
				RecievedData = RecievedData + "\n";
				COMRevieveBuffer = COMRevieveBuffer + RecievedData;
				//蜿嶺ソ。縺励◆荳陦後r繝繧ュ繧ケ繝医懊ャ繧ッ繧ケ縺ォ霆「騾√☆繧九ゅユ繧ュ繧ケ繝医懊ャ繧ッ繧ケ縺ョ謫堺ス懊ッ蛻・繧ケ繝ャ繝繝峨ョ髢「謨ー縺ォ縺吶k縲
				this->Invoke(dlgte, COMRevieveBuffer);
				COMRevieveBuffer = "";
			}
			//繧ィ繧ウ繝シ繧定ソ斐☆
			serialPort->Write(RecievedData);
		 }
	}
	
	 
private: System::Void buttonTransmit_Click(System::Object^  sender, System::EventArgs^  e) {
		 if(serialPort->IsOpen == true) {
			 //繧キ繝ェ繧「繝ォ繝昴シ繝医′髢九>縺ヲ縺繧九%縺ィ繧堤「コ隱阪@縺ヲ縺九i縲
			 serialPort->Write( textTransmit->Text + "\r\n" );
		 }
		 }

繝医ャ繝   蟾ョ蛻 繝舌ャ繧ッ繧「繝繝 繝ェ繝ュ繝シ繝   荳隕ァ 蜊倩ェ樊、懃エ「 譛邨よ峩譁ー   繝倥Ν繝   譛邨よ峩譁ー縺ョRSS
Last-modified: 2009-05-23 (蝨) 12:56:18 (3012d)