Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
#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?
Posted
Comments
[no name] 13-Aug-13 3:43am    
My guess is that you have posted too much code. If you cut it down to the basics people may want to look at it.

THIS:
C#
// 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:

C#
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.
 
Share this answer
 
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 ^'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900