如何通过串行端口从微控制器接收消息(逐行)接收事件,如超级终端? [英] how can I receive messages (line by line) through serial port from microcontroller by received event like hyperterminal?
问题描述
亲爱的朋友们,
我在这个论坛和其他网站上阅读了很多关于串口通信的讨论,并使用windows form应用程序和datareceived在visual studio 2012(c ++)中编写以下代码事件但它不起作用:
#pragma once
命名空间 WindowsFormsApplication1 {
使用 命名空间系统;
使用 命名空间 System :: ComponentModel;
使用 命名空间 System :: Collections;
使用 命名空间 System :: Windows :: Forms;
使用 命名空间 System :: Data;
使用 命名空间 System :: Drawing;
使用 命名空间 System :: IO :: Ports;
/// < 摘要 >
/// Form1的摘要
/// < / summary >
public ref class Form1: public System :: Windows :: Forms :: Form
{
// 委托void SetTextCallback(String ^ text);
String ^ data1;
// 委托void SetTextCallback(System :: Object ^ sender,System :: EventArgs ^ e);
// 创建委托
// 委托void invokeDelegate(); //没有参数传递
public :
Form1( void )
{
InitializeComponent();
findPorts();
//
// TODO:在此处添加构造函数代码
// < span class =code-comment>
}
protected :
/// < 摘要 >
/// 清理正在使用的所有资源。
/// < / summary >
~Form1()
{
if (components)
{
删除组件;
}
}
private :System :: IO :: Ports :: SerialPort ^ serialPort1;
protected :
private :System :: Windows :: Forms :: ComboBox ^ comboBox1;
private :System :: Windows :: Forms :: Label ^ label1;
private :System :: Windows :: Forms :: Label ^ label2;
private :System :: Windows :: Forms :: ComboBox ^ comboBox2;
private :System :: Windows :: Forms :: Button ^ button1;
private :System :: Windows :: Forms :: Button ^ button2;
private :System :: Windows :: Forms :: TextBox ^ textBox1;
private :System :: Windows :: Forms :: ProgressBar ^ progressBar1;
private :System :: Windows :: Forms :: Label ^ label3;
private :System :: ComponentModel :: IContainer ^ components;
private :
/// < 摘要 >
/// 必需的设计变量。
/// < / summary >
< span class =code-preprocessor> #pragma region Windows窗体设计器生成的代码
/// < 摘要 >
/// Designer支持所需的方法 - 不要修改
/// 使用代码编辑器的方法的内容。
/// < / summary >
void InitializeComponent( void )
{
this-> components =(gcnew System :: ComponentModel :: Container() );
this-> serialPort1 =(gcnew System :: IO :: Ports :: SerialPort(this-> components));
this-> comboBox1 =(gcnew System :: Windows :: Forms :: ComboBox());
this-> label1 =(gcnew System :: Windows :: Forms :: Label());
this-> label2 =(gcnew System :: Windows :: Forms :: Label());
this-> comboBox2 =(gcnew System :: Windows :: Forms :: ComboBox());
this-> button1 =(gcnew System :: Windows :: Forms :: Button());
this-> button2 =(gcnew System :: Windows :: Forms :: Button());
this-> textBox1 =(gcnew System :: Windows :: Forms :: TextBox());
this-> progressBar1 =(gcnew System :: Windows :: Forms :: ProgressBar());
this-> label3 =(gcnew System :: Windows :: Forms :: Label());
this-> SuspendLayout();
//
// serialPort1
//
this-> serialPort1-> DataReceived + = gcnew System :: IO :: Ports :: SerialDataReceivedEventHandler( this ,& Form1: :serialPort1_DataReceived);
//
// comboBox1
//
this-> comboBox1-> FormattingEnabled = true ;
this-> comboBox1-> Location = System :: Drawing :: Point( 212 , 16 < /跨度>);
this-> comboBox1-> Name = L comboBox1;
this-> comboBox1-> Size = System :: Drawing :: Size( 121 , 21 < /跨度>);
this-> comboBox1-> TabIndex = 0 ;
//
// label1
//
this-> label1-> AutoSize = true ;
this-> label1-> Location = System :: Drawing :: Point( 148 , 20 < /跨度>);
this-> label1-> Name = L label1;
this-> label1-> Size = System :: Drawing :: Size( 58 , 13 < /跨度>);
this-> label1-> TabIndex = 1 ;
this-> label1-> Text = L COM Ports;
//
// label2
//
this-> label2-> AutoSize = true ;
this-> label2-> Location = System :: Drawing :: Point( 148 , 47 < /跨度>);
this-> label2-> Name = L label2;
this-> label2-> Size = System :: Drawing :: Size( 58 , 13 < /跨度>);
this-> label2-> TabIndex = 2 ;
this-> label2-> Text = L 波特率;
//
// comboBox2
//
this-> comboBox2-> FormattingEnabled = true ;
this-> comboBox2-> Items-> AddRange(gcnew cli :: array< System :: Object ^ > ( 1 ){L 9600});
this-> comboBox2-> Location = System :: Drawing :: Point( 212 , 44 < /跨度>);
this-> comboBox2-> Name = L comboBox2;
this-> comboBox2-> Size = System :: Drawing :: Size( 121 , 21 < /跨度>);
this-> comboBox2-> TabIndex = 3 ;
//
// button1
//
this-> button1-> Location = System :: Drawing :: Point( 26 , 10 跨度>);
this-> button1-> Name = L button1;
this-> button1-> Size = System :: Drawing :: Size( 75 , 23 < /跨度>);
this-> button1-> TabIndex = 4 ;
this-> button1-> Text = L 初始化端口;
this-> button1-> UseVisualStyleBackColor = true ;
this-> button1->点击+ = gcnew System :: EventHandler( this ,& Form1 :: button1_Click);
//
// button2
//
this-> button2-> Location = System :: Drawing :: Point( 26 , 42 跨度>);
this-> button2-> Name = L button2;
this-> button2-> Size = System :: Drawing :: Size( 75 , 23 < /跨度>);
this-> button2-> TabIndex = 5 ;
this-> button2-> Text = L 关闭端口;
this-> button2-> UseVisualStyleBackColor = true ;
this-> button2->点击+ = gcnew System :: EventHandler( this ,& Form1 :: button2_Click);
//
// textBox1
//
this-> textBox1-> Enabled = false ;
this-> textBox1-> Location = System :: Drawing :: Point( 12 , 87 < /跨度>);
this-> textBox1-> Multiline = true ;
this-> textBox1-> Name = L textBox1;
this-> textBox1-> ReadOnly = true ;
this-> textBox1-> ScrollBars = System :: Windows :: Forms :: ScrollBars :: Vertical;
this-> textBox1-> Size = System :: Drawing :: Size( 447 , 163 < /跨度>);
this-> textBox1-> TabIndex = 6 ;
//
// progressBar1
//
this-> progressBar1-> Location = System :: Drawing :: Point( 359 , 39 跨度>);
this-> progressBar1-> Name = L progressBar1;
this-> progressBar1-> Size = System :: Drawing :: Size( 100 , 23 < /跨度>);
this-> progressBar1-> TabIndex = 8 ;
//
// label3
//
this-> label3-> AutoSize = true ;
this-> label3-> Location = System :: Drawing :: Point( 359 , 19 < /跨度>);
this-> label3-> Name = L label3;
this-> label3-> Size = System :: Drawing :: Size( 63 , 13 < /跨度>);
this-> label3-> TabIndex = 9 ;
this-> label3-> Text = L Port STatus;
//
// Form1
//
this-> AutoScaleDimensions = System :: Drawing :: SizeF( 6 , 13 跨度>);
this-> AutoScaleMode = System :: Windows :: Forms :: AutoScaleMode :: Font;
this-> ClientSize = System :: Drawing :: Size( 471 , 262 ) ;
this-> Controls-> Add(this-> comboBox2);
this-> Controls-> Add(this-> label2);
this-> Controls-> Add(this-> comboBox1);
this-> Controls-> Add(this-> label1);
this-> Controls-> Add(this-> label3);
this-> Controls-> Add(this-> progressBar1);
this-> Controls-> Add(this-> textBox1);
this-> Controls-> Add(this-> button2);
this-> Controls-> Add(this-> button1);
this-> Name = L Form1;
this-> Text = L Form1;
this-> ResumeLayout( false );
this-> PerformLayout();
}
#pragma endregion
// 查找序列可用端口
private : void findPorts( void )
{
// 获取端口名称
array< Object ^> ^ objectArray = SerialPort :: GetPortNames();
// 将字符串数组添加到组合框
this-> comboBox1-> ; Items-> AddRange(objectArray);
}
// 初始化按钮
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 = 请选择端口设置;
其他 {
尝试 {
// 确保端口未打开
如果 (!this-> serialPort1-> IsOpen){
this-> serialPort1-> PortName = this-> comboBox1-> Text;
this-> serialPort1-> BaudRate = Int32 :: Parse(this-> comboBox2-> Text);
// this-> textBox1-> Text =在此处输入消息;
// 打开串口
this-> serialPort1->打开();
this-> progressBar1-> Value = 100 ;
this-> button1->已启用= false ;
}
else
this-> textBox1-> Text = 端口未打开;
}
catch (UnauthorizedAccessException ^){
this-> textBox1-> Text = UnauthorizedAccess;
}
}
}
// 关闭按钮
private :System :: Void button2_Click(System :: Object ^ sender,System :: EventArgs ^ e){
// close serialPort
this-> serialPort1-> Close();
// 更新进度条
this-> progressBar1-> Value = 0 ;
// 启用阅读按钮
// this-> button2-> Enabled = true;
// 启用初始化按钮
this-> button1-> Enabled = true ;
// ovalShape
/ / this-> ovalShape1-> FillColor = Color :: Red;
}
private :System :: Void serialPort1_DataReceived(System :: Object ^ sender,System :: IO :: Ports :: SerialDataReceivedEventArgs ^ e){
data1 = serialPort1-> ReadExisting();
textBox1-> Text = textBox1-> Text + data1 + \\\\ n 跨度>;
this-> Invoke(gcnew EventHandler( this ,& Form1 :: SetTextCallback));
// this-> BeginInvoke(gcnew invokeDelegate(this,& Form1 :: SetTextCallback));
}
private :System :: Void SetTextCallback(System :: Object ^ sender,System :: EventArgs ^ e)
{
this-> textBox1-> AppendText(data1);
// this-> textBox1-> Text = data1;
// this-> textBox1-> Text =test;
}
};
}
可以查看并指导我解决这个问题?
这个:
// Form1
//
this-> AutoScaleDimensions = System :: Drawing :: SizeF( 6 , 13 跨度>);
this-> AutoScaleMode = System :: Windows :: Forms :: AutoScaleMode :: Font;
this-> ClientSize = System :: Drawing :: Size( 471 , 262 ) ;
this-> Controls-> Add(this-> comboBox2);
this-> Controls-> Add(this-> label2);
this-> Controls-> Add(this-> comboBox1);
this-> Controls-> Add(this-> label1);
this-> Controls-> Add(this-> label3);
this-> Controls-> Add(this-> progressBar1);
this-> Controls-> Add(this-> textBox1);
this-> Controls-> Add(this-> button2);
this-> Controls-> Add(this-> button1);
this-> Name = L Form1;
this-> Text = L Form1;
this-> ResumeLayout( false );
this-> PerformLayout();
不是同一个:< br $>
this-> components =(gcnew System :: ComponentModel :: Container());
this-> serialPort1 =(gcnew System :: IO :: Ports :: SerialPort(this-> components));
this-> comboBox1 =(gcnew System :: Windows :: Forms :: ComboBox());
this-> label1 =(gcnew System :: Windows :: Forms :: Label());
this-> label2 =(gcnew System :: Windows :: Forms :: Label());
this-> comboBox2 =(gcnew System :: Windows :: Forms :: ComboBox());
this-> button1 =(gcnew System :: Windows :: Forms :: Button());
this-> button2 =(gcnew System :: Windows :: Forms :: Button());
this-> textBox1 =(gcnew System :: Windows :: Forms :: TextBox());
this-> progressBar1 =(gcnew System :: Windows :: Forms :: ProgressBar());
this-> label3 =(gcnew System :: Windows :: Forms :: Label());
this-> SuspendLayout();
似乎你忘记添加:
this-> Controls-> Add(this-> serialPort1);
所以恕我直言,你粘贴了足够的代码来确定错误。
不,我做得很对。串口通讯工作正常。例如,我可以通过按下按钮并更改文本框或标签的Text属性来接收消息,但我无需按任何按钮即可从串口事件接收消息。
如果我添加this-> Controls-> Add(this-> serialPort1);到代码,VS显示此错误:
错误C2664:'System :: Windows :: Forms :: Control :: ControlCollection :: Add':无法转换参数1从'System :: IO :: Ports :: SerialPort ^'到'System :: Windows :: Forms :: Control ^'
Dear friends,
I read a lot of past discussions about serial port communication in this forum nd other websites and write the following code in visual studio 2012 (c++) by using windows form application and datareceived event but it's not working:
#pragma once
namespace WindowsFormsApplication1 {
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
{
//delegate void SetTextCallback(String^ text);
String^ data1;
//delegate void SetTextCallback(System::Object^ sender, System::EventArgs^ e);
// create a delegte
//delegate void invokeDelegate(); // no parameter passing
public:
Form1(void)
{
InitializeComponent();
findPorts();
//
//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^ serialPort1;
protected:
private: System::Windows::Forms::ComboBox^ comboBox1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::ComboBox^ comboBox2;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::ProgressBar^ progressBar1;
private: System::Windows::Forms::Label^ label3;
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->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->comboBox2 = (gcnew System::Windows::Forms::ComboBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->label3 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// serialPort1
//
this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived);
//
// comboBox1
//
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Location = System::Drawing::Point(212, 16);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(121, 21);
this->comboBox1->TabIndex = 0;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(148, 20);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(58, 13);
this->label1->TabIndex = 1;
this->label1->Text = L"COM Ports";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(148, 47);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(58, 13);
this->label2->TabIndex = 2;
this->label2->Text = L"Baud Rate";
//
// comboBox2
//
this->comboBox2->FormattingEnabled = true;
this->comboBox2->Items->AddRange(gcnew cli::array< System::Object^ >(1) {L"9600"});
this->comboBox2->Location = System::Drawing::Point(212, 44);
this->comboBox2->Name = L"comboBox2";
this->comboBox2->Size = System::Drawing::Size(121, 21);
this->comboBox2->TabIndex = 3;
//
// button1
//
this->button1->Location = System::Drawing::Point(26, 10);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 4;
this->button1->Text = L"Initialize Port";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(26, 42);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
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->Enabled = false;
this->textBox1->Location = System::Drawing::Point(12, 87);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->ReadOnly = true;
this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
this->textBox1->Size = System::Drawing::Size(447, 163);
this->textBox1->TabIndex = 6;
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(359, 39);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 23);
this->progressBar1->TabIndex = 8;
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(359, 19);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(63, 13);
this->label3->TabIndex = 9;
this->label3->Text = L"Port STatus";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(471, 262);
this->Controls->Add(this->comboBox2);
this->Controls->Add(this->label2);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->label1);
this->Controls->Add(this->label3);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
//find serial 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->serialPort1->BaudRate=Int32::Parse(this->comboBox2->Text);
//this->textBox1->Text="Enter Message Here";
//open serial port
this->serialPort1->Open();
this->progressBar1->Value=100;
this->button1->Enabled = false;
}
else
this->textBox1->Text="Port isn't openned";
}
catch(UnauthorizedAccessException^){
this->textBox1->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->button2->Enabled = true;
// Enable the init button
this->button1->Enabled = true;
// ovalShape
//this->ovalShape1->FillColor= Color::Red;
}
private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
data1 = serialPort1->ReadExisting();
textBox1->Text = textBox1->Text + data1 + "\r\n";
this->Invoke(gcnew EventHandler(this, &Form1::SetTextCallback));
//this->BeginInvoke( gcnew invokeDelegate( this, &Form1::SetTextCallback));
}
private: System::Void SetTextCallback(System::Object^ sender, System::EventArgs^ e)
{
this->textBox1->AppendText(data1);
//this->textBox1->Text=data1;
//this->textBox1->Text="test";
}
};
}
Could check it and guide me to solve the exsting fault?
THIS:
// Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(471, 262); this->Controls->Add(this->comboBox2); this->Controls->Add(this->label2); this->Controls->Add(this->comboBox1); this->Controls->Add(this->label1); this->Controls->Add(this->label3); this->Controls->Add(this->progressBar1); this->Controls->Add(this->textBox1); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); this->PerformLayout();
IS NOT THE SAME AS:
this->components = (gcnew System::ComponentModel::Container()); this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components)); this->comboBox1 = (gcnew System::Windows::Forms::ComboBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->label2 = (gcnew System::Windows::Forms::Label()); this->comboBox2 = (gcnew System::Windows::Forms::ComboBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar()); this->label3 = (gcnew System::Windows::Forms::Label()); this->SuspendLayout();
It seems that you forgot to add:
this->Controls->Add(this->serialPort1);
So IMHO, you pasted just enough code to determine the error.
No, I have done it correctly. the serial communication is working properly. for example I can receive the message by pushing a button and changing the Text property of textbox or label but I need t receive the messages from serial port event based without pressing any button.
If I add "this->Controls->Add(this->serialPort1);" to the code, VS shows this error:
error C2664: 'System::Windows::Forms::Control::ControlCollection::Add' : cannot convert parameter 1 from 'System::IO::Ports::SerialPort ^' to 'System::Windows::Forms::Control ^'
这篇关于如何通过串行端口从微控制器接收消息(逐行)接收事件,如超级终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!