我的系统如下: 在一个单独的线程中,我需要读取来自串口的连续数据。 现在,我可以读取字符串相当不错,但只有当我将鼠标移动到表单上时(为什么?) 停止读取按钮也不起作用,所以我确定我的线程调用中有一些问题或类似的东西.. 任何的想法?我找不到我的错误.. 非常感谢..
#pragma once
namespace part1 {
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;
using namespace System::Threading;
/// <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::Button^ button5;
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->button5 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// comboBox1
//
this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Location = System::Drawing::Point(678, 46);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(121, 21);
this->comboBox1->TabIndex = 0;
//
// 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(678, 86);
this->comboBox2->Name = L"comboBox2";
this->comboBox2->Size = System::Drawing::Size(121, 21);
this->comboBox2->TabIndex = 1;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(608, 49);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(58, 13);
this->label1->TabIndex = 2;
this->label1->Text = L"COM Ports";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(608, 93);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(58, 13);
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(678, 128);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(90, 55);
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(678, 189);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(90, 55);
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, 80);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->ReadOnly = true;
this->textBox1->Size = System::Drawing::Size(170, 39);
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, 190);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(170, 20);
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, 135);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 40);
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, 135);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(81, 40);
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, 46);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 28);
this->progressBar1->TabIndex = 10;
//
// 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, 19);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(70, 13);
this->label3->TabIndex = 11;
this->label3->Text = L"Port Status";
//
// button5
//
this->button5->Location = System::Drawing::Point(252, 99);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(88, 20);
this->button5->TabIndex = 12;
this->button5->Text = L"Stop Reading";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Silver;
this->ClientSize = System::Drawing::Size(932, 485);
this->Controls->Add(this->button5);
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->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 Here";
//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("s");
else
this->textBox2->Text="Port Not Opened";
this->textBox2->Text="Abilitate to read";
}
void UpdateButton() {
while(this->textBox2->Text=="Abilitate to read"){
// check if port is ready for reading
if(this->serialPort1->IsOpen){
// Reset the text in the result label.
// this->textBox1->Text = String::Empty;
//this->textBox2->Text="Abilitate to read";
// 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";
}
}
void ThreadMethod() {
this->Invoke(gcnew MethodInvoker(this, &Form1::UpdateButton));
}
//read button
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
Thread^ oThread = gcnew Thread( gcnew ThreadStart( this, &Form1::ThreadMethod ) );
oThread->Start();
/*while(this->textBox2->Text=="Abilitate to read"){
// check if port is ready for reading
if(this->serialPort1->IsOpen){
// Reset the text in the result label.
// this->textBox1->Text = String::Empty;
//this->textBox2->Text="Abilitate to read";
// 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 button5_Click(System::Object^ sender, System::EventArgs^ e) {
this->textBox2->Text="Stop Reading";
this->button1->Enabled = true;
}
};
}
我认为问题应该是在线程中使用这样的方法:
void ThreadMethod() {
while(this->textBox2->Text=="Abilitate to read"){
// check if port is ready for reading
if(this->serialPort1->IsOpen){
// Reset the text in the result label.
// this->textBox1->Text = String::Empty;
//this->textBox2->Text="Abilitate to read";
// 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";
}
}
//read button
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
Thread^ oThread = gcnew Thread( gcnew ThreadStart( this, &Form1::ThreadMethod ) );
oThread->Start();
}
调试器向我发送异常:我无法从创建的其他线程访问文本框
也许代替“this”我可以在刚创建的线程中引用Form1 ..
答案 0 :(得分:0)
这是最终解决方案:) 我使用了backgroundWorker utylities:
#pragma once
#include <windows.h>
namespace test3 {
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();
findPorts();
// disable this text field
this->txtBoxGloveString->Enabled = false;
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::IO::Ports::SerialPort^ _serialPort;
private: System::Windows::Forms::Button^ btnEnableRead;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::ComboBox^ comboBox1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::ComboBox^ comboBox2;
private: System::Windows::Forms::Button^ btnRead;
private: System::Windows::Forms::TextBox^ txtBoxGloveString;
private: System::Windows::Forms::ProgressBar^ pBPortStatus;
private: System::Windows::Forms::Button^ btnClosePort;
private: System::Windows::Forms::Label^ label1;
private: Microsoft::VisualBasic::PowerPacks::ShapeContainer^ shapeContainer1;
private: Microsoft::VisualBasic::PowerPacks::OvalShape^ ovalShape1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::ComponentModel::BackgroundWorker^ backgroundWorker1;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::ProgressBar^ progressBar2;
private: System::ComponentModel::IContainer^ components;
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->_serialPort = (gcnew System::IO::Ports::SerialPort(this->components));
this->btnEnableRead = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->button2 = (gcnew System::Windows::Forms::Button());
this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
this->btnRead = (gcnew System::Windows::Forms::Button());
this->txtBoxGloveString = (gcnew System::Windows::Forms::TextBox());
this->pBPortStatus = (gcnew System::Windows::Forms::ProgressBar());
this->btnClosePort = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->shapeContainer1 = (gcnew Microsoft::VisualBasic::PowerPacks::ShapeContainer());
this->ovalShape1 = (gcnew Microsoft::VisualBasic::PowerPacks::OvalShape());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
this->button5 = (gcnew System::Windows::Forms::Button());
this->progressBar2 = (gcnew System::Windows::Forms::ProgressBar());
this->SuspendLayout();
//
// _serialPort
//
this->_serialPort->PortName = L"COM5";
this->_serialPort->ReadTimeout = 500;
this->_serialPort->WriteTimeout = 500;
//
// btnEnableRead
//
this->btnEnableRead->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->btnEnableRead->Location = System::Drawing::Point(36, 48);
this->btnEnableRead->Name = L"btnEnableRead";
this->btnEnableRead->Size = System::Drawing::Size(101, 62);
this->btnEnableRead->TabIndex = 0;
this->btnEnableRead->Text = L"Send";
this->btnEnableRead->UseVisualStyleBackColor = true;
this->btnEnableRead->Click += gcnew System::EventHandler(this, &Form1::btnEnableRead_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->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->textBox1->ForeColor = System::Drawing::SystemColors::Desktop;
this->textBox1->Location = System::Drawing::Point(36, 10);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(261, 26);
this->textBox1->TabIndex = 1;
this->textBox1->Text = L"Enter Here";
//
// comboBox1
//
this->comboBox1->AllowDrop = true;
this->comboBox1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
this->comboBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Location = System::Drawing::Point(626, 12);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(102, 28);
this->comboBox1->TabIndex = 2;
//
// button2
//
this->button2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button2->Location = System::Drawing::Point(626, 98);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(101, 62);
this->button2->TabIndex = 3;
this->button2->Text = L"Init Port";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// comboBox2
//
this->comboBox2->AllowDrop = true;
this->comboBox2->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
this->comboBox2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->comboBox2->FormattingEnabled = true;
this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^ >(3) {L"9600", L"38400", L"115200"});
this->comboBox2->Location = System::Drawing::Point(626, 64);
this->comboBox2->Name = L"comboBox2";
this->comboBox2->Size = System::Drawing::Size(102, 28);
this->comboBox2->TabIndex = 4;
//
// btnRead
//
this->btnRead->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->btnRead->Location = System::Drawing::Point(36, 116);
this->btnRead->Name = L"btnRead";
this->btnRead->Size = System::Drawing::Size(101, 62);
this->btnRead->TabIndex = 5;
this->btnRead->Text = L"Read";
this->btnRead->UseVisualStyleBackColor = true;
this->btnRead->Click += gcnew System::EventHandler(this, &Form1::btnRead_Click);
//
// txtBoxGloveString
//
this->txtBoxGloveString->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->txtBoxGloveString->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtBoxGloveString->Location = System::Drawing::Point(139, 121);
this->txtBoxGloveString->Multiline = true;
this->txtBoxGloveString->Name = L"txtBoxGloveString";
this->txtBoxGloveString->Size = System::Drawing::Size(260, 26);
this->txtBoxGloveString->TabIndex = 7;
this->txtBoxGloveString->Text = L"Received Here";
this->txtBoxGloveString->TextChanged += gcnew System::EventHandler(this, &Form1::txtBoxGloveString_TextChanged);
//
// pBPortStatus
//
this->pBPortStatus->Location = System::Drawing::Point(463, 121);
this->pBPortStatus->Name = L"pBPortStatus";
this->pBPortStatus->Size = System::Drawing::Size(100, 32);
this->pBPortStatus->TabIndex = 9;
this->pBPortStatus->Click += gcnew System::EventHandler(this, &Form1::pBPortStatus_Click);
//
// btnClosePort
//
this->btnClosePort->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->btnClosePort->Location = System::Drawing::Point(626, 167);
this->btnClosePort->Name = L"btnClosePort";
this->btnClosePort->Size = System::Drawing::Size(101, 62);
this->btnClosePort->TabIndex = 10;
this->btnClosePort->Text = L"Close Port";
this->btnClosePort->UseVisualStyleBackColor = true;
this->btnClosePort->Click += gcnew System::EventHandler(this, &Form1::btnClosePort_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->Location = System::Drawing::Point(458, 98);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(100, 20);
this->label1->TabIndex = 11;
this->label1->Text = L"Port Status";
//
// shapeContainer1
//
this->shapeContainer1->Location = System::Drawing::Point(0, 0);
this->shapeContainer1->Margin = System::Windows::Forms::Padding(0);
this->shapeContainer1->Name = L"shapeContainer1";
this->shapeContainer1->Shapes->AddRange(gcnew cli::array< Microsoft::VisualBasic::PowerPacks::Shape^ >(1) {this->ovalShape1});
this->shapeContainer1->Size = System::Drawing::Size(761, 444);
this->shapeContainer1->TabIndex = 12;
this->shapeContainer1->TabStop = false;
//
// ovalShape1
//
this->ovalShape1->FillStyle = Microsoft::VisualBasic::PowerPacks::FillStyle::Sphere;
this->ovalShape1->Location = System::Drawing::Point(576, 125);
this->ovalShape1->Name = L"ovalShape1";
this->ovalShape1->Size = System::Drawing::Size(22, 23);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label2->Location = System::Drawing::Point(549, 20);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(72, 16);
this->label2->TabIndex = 13;
this->label2->Text = L"COM Ports";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 9.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label3->Location = System::Drawing::Point(549, 72);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(72, 16);
this->label3->TabIndex = 14;
this->label3->Text = L"Baud Rate";
//
// backgroundWorker1
//
this->backgroundWorker1->WorkerReportsProgress = true;
this->backgroundWorker1->WorkerSupportsCancellation = true;
this->backgroundWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::backgroundWorker1_DoWork);
this->backgroundWorker1->ProgressChanged += gcnew System::ComponentModel::ProgressChangedEventHandler(this, &Form1::backgroundWorker1_ProgressChanged);
this->backgroundWorker1->RunWorkerCompleted += gcnew System::ComponentModel::RunWorkerCompletedEventHandler(this, &Form1::backgroundWorker1_RunWorkerCompleted);
//
// button5
//
this->button5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->button5->Location = System::Drawing::Point(36, 184);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(101, 62);
this->button5->TabIndex = 5;
this->button5->Text = L"Cancel Reading";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
//
// progressBar2
//
this->progressBar2->Location = System::Drawing::Point(139, 346);
this->progressBar2->Name = L"progressBar2";
this->progressBar2->Size = System::Drawing::Size(532, 47);
this->progressBar2->TabIndex = 15;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Silver;
this->ClientSize = System::Drawing::Size(761, 444);
this->Controls->Add(this->progressBar2);
this->Controls->Add(this->button5);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->btnClosePort);
this->Controls->Add(this->pBPortStatus);
this->Controls->Add(this->txtBoxGloveString);
this->Controls->Add(this->btnRead);
this->Controls->Add(this->comboBox2);
this->Controls->Add(this->button2);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->btnEnableRead);
this->Controls->Add(this->shapeContainer1);
this->Name = L"Form1";
this->Text = L"Uart Gui v1.1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
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 );
}
// Send button
private: System::Void btnEnableRead_Click(System::Object^ sender, System::EventArgs^ e) {
// add sender name
String^ name = this->_serialPort->PortName;
// grab text and store in send buffer
String^ message = "s";
// write to serial
if(this->_serialPort->IsOpen)
//this->_serialPort->WriteLine(String::Format("<{0}>: {1}",name,message));
this->_serialPort->WriteLine(message);
else
this->textBox1->Text="Port Not Opened";
}
// Init button------------------------------------
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
this->txtBoxGloveString->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->_serialPort->IsOpen){
this->_serialPort->PortName=this->comboBox1->Text;
//this->textBox1->Text=this->comboBox1->Text;
this->_serialPort->BaudRate=Int32::Parse(this->comboBox2->Text);
//this->textBox1->Text=this->comboBox2->Text;
this->textBox1->Text="Enter Message Here";
//open serial port
this->_serialPort->Open();
this->pBPortStatus->Value=100;
this->ovalShape1->FillColor= Color::Green;
}
else
this->textBox1->Text="Port isn't openned";
}
catch(UnauthorizedAccessException^){
this->textBox1->Text="UnauthorizedAccess";
}
}
}
// Read button --------------------------------------
//this will start the asyn for backgroundwork
private: System::Void btnRead_Click(System::Object^ sender, System::EventArgs^ e) {
backgroundWorker1->RunWorkerAsync(10);
/* // check if port is ready for reading
if(this->_serialPort->IsOpen){
// Reset the text in the result label.
this->textBox2->Text = String::Empty;
// this will read manually
try{
this->textBox2->Text=this->_serialPort->ReadLine();
}
catch(TimeoutException^){
this->textBox2->Text="Timeout Exception";
}
// Disable the init button
// the asynchronous operation is done.
this->button2->Enabled = false;
this->ovalShape1->FillColor= Color::Green;
}
else
// give error warning
this->textBox2->Text="Port Not Opened";*/
}
// progress bar -------------------------------------------
private: System::Void pBPortStatus_Click(System::Object^ sender, System::EventArgs^ e) {}
// close port button
private: System::Void btnClosePort_Click(System::Object^ sender, System::EventArgs^ e) {
// add sender name
String^ name = this->_serialPort->PortName;
// grab text and store in send buffer
String^ message = "e";
// write to serial
if(this->_serialPort->IsOpen)
//this->_serialPort->WriteLine(String::Format("<{0}>: {1}",name,message));
this->_serialPort->WriteLine(message);
else
this->textBox1->Text="Port Not Opened";
//close serialPort
this->_serialPort->Close();
// update progress bar
this->pBPortStatus->Value=0;
// Enable read button
this->button2->Enabled = true;
// Enable the init button
this->btnRead->Enabled = true;
// ovalShape
this->ovalShape1->FillColor= Color::Red;
Application::Exit();
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {
int count = (int)e->Argument;
int sum = 0;
String^ valuestr;
while ( true ){
if (backgroundWorker1->CancellationPending){
e->Cancel=true;
break;
}
if(_serialPort->IsOpen){
try{
valuestr= _serialPort->ReadLine();
}
catch(TimeoutException^){
valuestr= "Error";
}
}
backgroundWorker1->ReportProgress(10,valuestr);
System::Threading::Thread::Sleep(1);
}
e->Result=sum;
}
private: System::Void backgroundWorker1_ProgressChanged(System::Object^ sender, System::ComponentModel::ProgressChangedEventArgs^ e) {
txtBoxGloveString->Text=System::Convert::ToString(e->UserState);
}
private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e) {
progressBar2->Value=0;
if(e->Cancelled){
MessageBox::Show("Stop reading glove by the user");
}
else{
MessageBox::Show("Result: " + e->Result);
}
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
backgroundWorker1->CancelAsync();
progressBar2->Value=0;
}
private: System::Void txtBoxGloveString_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
}
谢谢我!