討論區快速選單
知識庫快速選單
政府補助!學嵌入式+物聯網 網路投保旅行平安險 軟體開發過程中有哪些資安漏洞?
[ 回上頁 ] [ 討論區發言規則 ]
com port 用visualC++寫 讀取抓不到要如何改???
更改我的閱讀文章字型大小
作者 : jasonchang(小豪)
[ 貼文 3 | 人氣 0 | 評價 0 | 評價/貼文 0 | 送出評價 3 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/8/26 上午 11:44:28
#pragma once
#include <iostream>
using namespace std;

namespace part11 {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO::Ports;

/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();

//
//TODO: Add the constructor code here
//
findPorts();
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::ComboBox^ comboBox1;
private: System::IO::Ports::SerialPort^ serialPort1;
private: System::Windows::Forms::ComboBox^ comboBox2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::ProgressBar^ progressBar1;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private: System::ComponentModel::IContainer^ components;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>


#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// comboBox1
//
this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Location = System::Drawing::Point(355, 27);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(121, 20);
this->comboBox1->TabIndex = 0;
this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
//
// serialPort1
//
this->serialPort1->ReadTimeout = 500;
this->serialPort1->WriteTimeout = 500;
//
// comboBox2
//
this->comboBox2->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
this->comboBox2->FormattingEnabled = true;
this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^ >(2) {L"9600", L"115200"});
this->comboBox2->Location = System::Drawing::Point(355, 64);
this->comboBox2->Name = L"comboBox2";
this->comboBox2->Size = System::Drawing::Size(121, 20);
this->comboBox2->TabIndex = 1;
this->comboBox2->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox2_SelectedIndexChanged);
//
// label1
//
this->label1->autoSize = true;
this->label1->Location = System::Drawing::Point(285, 30);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(59, 12);
this->label1->TabIndex = 2;
this->label1->Text = L"COM Ports";
//
// label2
//
this->label2->autoSize = true;
this->label2->Location = System::Drawing::Point(285, 70);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(59, 12);
this->label2->TabIndex = 3;
this->label2->Text = L"Baud Rate";
//
// button1
//
this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button1->Location = System::Drawing::Point(355, 102);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(90, 51);
this->button1->TabIndex = 4;
this->button1->Text = L"Init Port";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button2->Location = System::Drawing::Point(355, 159);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(90, 51);
this->button2->TabIndex = 5;
this->button2->Text = L"Close Port";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// textBox1
//
this->textBox1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(128)));
this->textBox1->Enabled = false;
this->textBox1->Location = System::Drawing::Point(59, 91);
this->textBox1->Name = L"textBox1";
this->textBox1->ReadOnly = true;
this->textBox1->Size = System::Drawing::Size(170, 21);
this->textBox1->TabIndex = 6;
this->textBox1->Text = L"Received Here";
//
// textBox2
//
this->textBox2->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(192)),
static_cast<System::Int32>(static_cast<System::Byte>(128)));
this->textBox2->Location = System::Drawing::Point(59, 175);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(170, 21);
this->textBox2->TabIndex = 7;
this->textBox2->Text = L"Enter Here";
//
// button3
//
this->button3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button3->Location = System::Drawing::Point(59, 125);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 37);
this->button3->TabIndex = 8;
this->button3->Text = L"Send";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// button4
//
this->button4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button4->Location = System::Drawing::Point(162, 125);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(81, 37);
this->button4->TabIndex = 9;
this->button4->Text = L"Read";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(59, 42);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 26);
this->progressBar1->TabIndex = 10;
this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
//
// label3
//
this->label3->autoSize = true;
this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label3->Location = System::Drawing::Point(56, 18);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(70, 13);
this->label3->TabIndex = 11;
this->label3->Text = L"Port Status";
//
// label4
//
this->label4->autoSize = true;
this->label4->Location = System::Drawing::Point(12, 179);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(29, 12);
this->label4->TabIndex = 12;
this->label4->Text = L"send";
//
// label5
//
this->label5->autoSize = true;
this->label5->Location = System::Drawing::Point(12, 94);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(29, 12);
this->label5->TabIndex = 13;
this->label5->Text = L"read";
//
// Form1
//
this->autoScaleDimensions = System::Drawing::SizeF(6, 12);
this->autoScaleMode = System::Windows::Forms::autoScaleMode::Font;
this->BackColor = System::Drawing::Color::Silver;
this->ClientSize = System::Drawing::Size(500, 242);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->comboBox2);
this->Controls->Add(this->comboBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load_1);
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion


// find available ports
private: void findPorts(void)
{
// get port names
array<Object^>^ objectArray = SerialPort::GetPortNames();
// add string array to combobox
this->comboBox1->Items->AddRange( objectArray );

}
// init button
private: System::void button1_Click(System::Object^ sender, System::EventArgs^ e) {

this->textBox1->Text=String::Empty;
if(this->comboBox1->Text==String::Empty || this->comboBox2->Text==String::Empty)
this->textBox1->Text="Please Select Port Settings";
else {
try{
// make sure port isn't open
if(!this->serialPort1->IsOpen){
this->serialPort1->PortName=this->comboBox1->Text;
//this->textBox1->Text=this->comboBox1->Text;
this->serialPort1->BaudRate=Int32::Parse(this->comboBox2->Text);
//this->textBox1->Text=this->comboBox2->Text;
this->textBox2->Text="Enter Message ";

/*String^ name = this->serialPort1->PortName;
String^ message = this->textBox2->Text ;
this->serialPort1->WriteLine(message);*/

//open serial port
this->serialPort1->Open();
this->progressBar1->Value=100;
}
else
this->textBox2->Text="Port isn't openned";
}
catch(UnauthorizedAccessException^){
this->textBox2->Text="UnauthorizedAccess";
}
}

}

// close button
private: System::void button2_Click(System::Object^ sender, System::EventArgs^ e) {

//close serialPort
this->serialPort1->Close();
// update progress bar
this->progressBar1->Value=0;
// Enable read button
this->button4->Enabled = true;
// Enable the init button
this->button1->Enabled = true;


}

// send button
private: System::void button3_Click(System::Object^ sender, System::EventArgs^ e) {

// add sender name
String^ name = this->serialPort1->PortName;
// grab text and store in send buffer
String^ message = this->textBox2->Text;

// write to serial
if(this->serialPort1->IsOpen)
//this->_serialPort->WriteLine(String::Format("<{0}>: {1}",name,message));
this->serialPort1->WriteLine(message);
else
this->textBox2->Text="Port Not Opened";


}

//read button
private: System::void button4_Click(System::Object^ sender, System::EventArgs^ e) {

// check if port is ready for reading
if(this->serialPort1->IsOpen){
// Reset the text in the result label.
this->textBox1->Text = String::Empty;

// this will read manually
try{
this->textBox1->Text=this->serialPort1->ReadLine();
}
catch(TimeoutException^){
this->textBox1->Text="Timeout Exception";
}
// Disable the init button
// the asynchronous operation is done.
this->button1->Enabled = false;
}
else
// give error warning
this->textBox1->Text="Port Not Opened";


}
private: System::void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::void progressBar1_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::void comboBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
/* if((sender as SerialPort).BytesToRead>0){try{Byte[]buffer = new Byte[1024];
Int32length = (sender as SerialPort).Bead(buffer,0;buffer.Length);
Array.Resize(ref buffer, length);
Display d = new Display(DisplayText);
this->Invoke(d, new object[]{buffer});}catah(timeoutException timeoutEx)}
*/ }
private: System::void comboBox2_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
}

private: System::void Form1_Load_1(System::Object^ sender, System::EventArgs^ e) {
}
};
}
作者 : ice_emissary(燃燒的大地) 貼文超過200則
[ 貼文 408 | 人氣 0 | 評價 1890 | 評價/貼文 4.63 | 送出評價 18 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/8/26 上午 11:52:51
你根本就寫錯了吧!
這是 C++ 程式碼嗎?
作者 : jasonchang(小豪)
[ 貼文 3 | 人氣 0 | 評價 0 | 評價/貼文 0 | 送出評價 3 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/8/26 下午 12:56:13
你把code 拿去跑跑看 阿
這是C++ 內的 FORM
作者 : aries_chen(Aries) Java卓越專家貼文超過500則
[ 貼文 782 | 人氣 1941 | 評價 3690 | 評價/貼文 4.72 | 送出評價 2 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
主題發起人jasonchang註記此篇回應為很有道理 2015/8/26 下午 04:10:21
這是使用了.net的c++
應該只有Visual Studio能跑

說明一下visual studio跟.net的版本
會的人會比較容易幫忙
作者 : sflam(Raymond)討論區板主 Visual C++ .NET卓越專家VC++一代宗師新手入門優秀好手資訊類作業求救頂尖高手C++一代宗師貼文超過4000則
[ 貼文 4945 | 人氣 9172 | 評價 32290 | 評價/貼文 6.53 | 送出評價 142 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
主題發起人jasonchang註記此篇回應為很有道理 2015/8/26 下午 08:44:41
>你把code 拿去跑跑看 阿
>這是C++ 內的 FORM

這不是 C++! C++ 沒有 form, 沒有 ref 這個關鍵字, 不用 ^ 做 managed pointer, 更沒有 managed pointer 這個概念. 你用的程式語言的正式名稱叫做 C++/CLI, 是微軟以 C++ 為基, 針對 .NET 平台特性創造出來的語言, 可以視為跟 C++ 是兩種不同的語言.
作者 : kagaya(kagaya) VC++優秀好手C++優秀好手貼文超過1000則人氣指數超過30000點
[ 貼文 1602 | 人氣 38709 | 評價 4610 | 評價/貼文 2.88 | 送出評價 115 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/8/27 上午 10:04:07
縮小問題範圍 對你自己釐清問題也有幫助
你連UI的code都貼出來 花花雜雜一片
有空幫你從大海裡撈針的能有幾人?
作者 : sflam(Raymond)討論區板主 Visual C++ .NET卓越專家VC++一代宗師新手入門優秀好手資訊類作業求救頂尖高手C++一代宗師貼文超過4000則
[ 貼文 4945 | 人氣 9172 | 評價 32290 | 評價/貼文 6.53 | 送出評價 142 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/8/27 上午 11:17:12
建議樓主先寫個簡單的 console 程式來測試你想要的功能.
作者 : ice_emissary(燃燒的大地) 貼文超過200則
[ 貼文 408 | 人氣 0 | 評價 1890 | 評價/貼文 4.63 | 送出評價 18 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/8/27 下午 12:16:27
>建議樓主先寫個簡單的 console 程式來測試你想要的功能.

我大膽猜測樓主不會寫 Console 程式。
作者 : gmailjoey(建中) 貼文超過200則
[ 貼文 206 | 人氣 0 | 評價 190 | 評價/貼文 0.92 | 送出評價 13 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/8/27 下午 05:26:50
> this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^ >(2) {L"9600", L"115200"});

這是新版Visual C++的寫法嗎?
我目前沒有在做開發所以沒看過這種寫法,
也不知道這一行會不會跑哦。
你需要的是C++/CLI規格的進階書籍跟開發文件,
目前這些文件在市面上很搶手,供不應求。我都還沒有買到過。
建議你到微軟的網站搜尋"C++/CLI"就可以找到一些相關的網站和開發文件,
如果有資金的話,也可以直接請教國外微軟公司的人員問問看。
作者 : kuolung(kuolung)
[ 貼文 151 | 人氣 1414 | 評價 130 | 評價/貼文 0.86 | 送出評價 39 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
主題發起人jasonchang註記此篇回應為最佳解答 2015/8/31 上午 07:22:01
您可能弄錯了,我重新再確認過了,
C++ 沒有 Form 這種專案,應該是 C# 的專案才是

其次,您可以先用 Term 這個程式,先確認過您的 rs-232 可以收到資料,再來測這個程式,因為我以前常常是 rs-232 有問題,而不是程式有問題,現在的主機版,很多都沒有 rs-232 , 如果您是用 usb 轉 rs-232 那還要再測一下,驅動才行,
還有一個地方,您有先按 init 那個 button嗎,還有 rs-232 的其他設定都對嗎
作者 : qazujm(joe) Visual Basic卓越專家C#優秀好手貼文超過1000則
[ 貼文 1521 | 人氣 2524 | 評價 9070 | 評價/貼文 5.96 | 送出評價 90 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/8/31 上午 09:41:35
SerialPort::DataReceived Event
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1
用DataReceived事件Read試試...
作者 : jasonchang(小豪)
[ 貼文 3 | 人氣 0 | 評價 0 | 評價/貼文 0 | 送出評價 3 次 ] 
[ 給個讚 ]  [ 給個讚 ]  [ 回應本文 ]  [ 發表新文 ]  [ 回上頁 ] [ 回討論區列表 ] [ 回知識入口 ]
2015/11/25 上午 09:58:10
謝謝各位~ 小弟大概懂了
我後來改用別的做出來了
 板主 : 青衫 , Raymond
 > Visual C++ - 討論區
 - 最近熱門問答精華集
 - 全部歷史問答精華集
 - Visual C++ - 知識庫
  ■ 全站最新Post列表
  ■ 我的文章收藏
  ■ 我最愛的作者
  ■ 全站文章收藏排行榜
  ■ 全站最愛作者排行榜
  ■  月熱門主題
  ■  季熱門主題
  ■  熱門主題Top 20
  ■  本區Post排行榜
  ■  本區評價排行榜
  ■  全站專家名人榜
  ■  全站Post排行榜
  ■  全站評價排行榜
  ■  全站人氣排行榜
 請輸入關鍵字 
  開始搜尋
 
Top 10
評價排行
Visual C++
1 青衫 11070 
2 Raymond 10090 
3 Clier 7630 
4 小約翰 2500 
5 Cog 2030 
6 coco 1870 
7 aming 1410 
8 牧童哥 1400 
9 r2109 1380 
10 Akira 1350 
Visual C++
  專家等級 評價  
  一代宗師 10000  
  曠世奇才 5000  
  頂尖高手 3000  
  卓越專家 1500  
  優秀好手 750  
Microsoft Internet Explorer 6.0. Screen 1024x768 pixel. High Color (16 bit).
2000-2019 程式設計俱樂部 http://www.programmer-club.com.tw/
0.1875