Чтение с последовательного порта в Visual C ++ Express 2010 - PullRequest
0 голосов
/ 12 марта 2012

Я пытаюсь создать программу на C ++ в Visual C ++ Express 2010, которая может взаимодействовать с последовательным портом. Определение последовательного порта и т. Д. Не является проблемой, но у меня есть проблема со ссылками на вещи, определенные в моей «Форме» в моей основной программе.

У меня есть основной файл, созданный Visual C ++ Express, и я пытаюсь сослаться на созданный мной последовательный порт в форме "Form1":

Form1::serialPort1.BytesToRead
Form1::serialPort1->BytesToRead
serialPort1.BytesToRead
serialPort1->BytesToRead

Я пробовал разные способы доступа к последовательному порту, первые два дают ошибку компилятора:

error C2228: left of '.BytesToRead' must have class/struct/union
error C2228: left of '->BytesToRead' must have class/struct/union

и два других дают это:

error C2065: 'serialPort1' : undeclared identifier

Я чувствую, что есть что-то очевидное, что мне здесь не хватает. Если требуется дополнительная информация, пожалуйста, спросите.

Спасибо.

Вот файл Form1.h:

    #pragma once

    namespace avndator {

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

/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::TextBox^  textBox1;
protected: 
private: System::Windows::Forms::Label^  label1;
private: System::Windows::Forms::Label^  label2;
private: System::Windows::Forms::TextBox^  textBox2;
private: System::Windows::Forms::Label^  label3;

private: System::Windows::Forms::Button^  button1;
private: System::Windows::Forms::Button^  button2;
private: System::Windows::Forms::Button^  button3;
private: System::Windows::Forms::Button^  button4;
private: System::Windows::Forms::Button^  button5;
private: System::Windows::Forms::Button^  button6;
private: System::Windows::Forms::Button^  button8;
private: System::Windows::Forms::PictureBox^  pictureBox1;
private: System::Windows::Forms::Label^  label4;
private: System::Windows::Forms::TextBox^  textBox3;

private: System::Windows::Forms::Label^  label5;
private: System::Windows::Forms::Button^  button7;
private: System::Windows::Forms::Button^  button9;
private: System::Windows::Forms::Label^  label6;
private: System::Windows::Forms::TextBox^  textBox4;


private: System::Windows::Forms::Label^  label7;
private: System::Windows::Forms::Label^  label8;
private: System::Windows::Forms::TextBox^  textBox5;
private: System::Windows::Forms::Label^  label9;
private: System::Windows::Forms::TextBox^  textBox6;
private: System::Windows::Forms::Label^  label10;
private: System::Windows::Forms::TextBox^  textBox7;
private: System::Windows::Forms::Label^  label11;
private: System::Windows::Forms::TextBox^  textBox8;
private: System::Windows::Forms::Label^  label12;
private: System::Windows::Forms::TextBox^  textBox9;
private: System::IO::Ports::SerialPort^  serialPort1;


protected: 
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());
        System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
        this->textBox1 = (gcnew System::Windows::Forms::TextBox());
        this->label1 = (gcnew System::Windows::Forms::Label());
        this->label2 = (gcnew System::Windows::Forms::Label());
        this->textBox2 = (gcnew System::Windows::Forms::TextBox());
        this->label3 = (gcnew System::Windows::Forms::Label());
        this->button1 = (gcnew System::Windows::Forms::Button());
        this->button2 = (gcnew System::Windows::Forms::Button());
        this->button3 = (gcnew System::Windows::Forms::Button());
        this->button4 = (gcnew System::Windows::Forms::Button());
        this->button5 = (gcnew System::Windows::Forms::Button());
        this->button6 = (gcnew System::Windows::Forms::Button());
        this->button8 = (gcnew System::Windows::Forms::Button());
        this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
        this->label4 = (gcnew System::Windows::Forms::Label());
        this->textBox3 = (gcnew System::Windows::Forms::TextBox());
        this->label5 = (gcnew System::Windows::Forms::Label());
        this->button7 = (gcnew System::Windows::Forms::Button());
        this->button9 = (gcnew System::Windows::Forms::Button());
        this->label6 = (gcnew System::Windows::Forms::Label());
        this->textBox4 = (gcnew System::Windows::Forms::TextBox());
        this->label7 = (gcnew System::Windows::Forms::Label());
        this->label8 = (gcnew System::Windows::Forms::Label());
        this->textBox5 = (gcnew System::Windows::Forms::TextBox());
        this->label9 = (gcnew System::Windows::Forms::Label());
        this->textBox6 = (gcnew System::Windows::Forms::TextBox());
        this->label10 = (gcnew System::Windows::Forms::Label());
        this->textBox7 = (gcnew System::Windows::Forms::TextBox());
        this->label11 = (gcnew System::Windows::Forms::Label());
        this->textBox8 = (gcnew System::Windows::Forms::TextBox());
        this->label12 = (gcnew System::Windows::Forms::Label());
        this->textBox9 = (gcnew System::Windows::Forms::TextBox());
        this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
        (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
        this->SuspendLayout();
        // 
        // textBox1
        // 
        this->textBox1->Location = System::Drawing::Point(12, 25);
        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(242, 126);
        this->textBox1->TabIndex = 0;
        this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
        // 
        // label1
        // 
        this->label1->AutoSize = true;
        this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
            static_cast<System::Byte>(0)));
        this->label1->Location = System::Drawing::Point(9, 5);
        this->label1->Name = L"label1";
        this->label1->Size = System::Drawing::Size(166, 17);
        this->label1->TabIndex = 1;
        this->label1->Text = L"Kommunikationsenhet";
        this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
        // 
        // label2
        // 
        this->label2->AutoSize = true;
        this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
            static_cast<System::Byte>(0)));
        this->label2->Location = System::Drawing::Point(9, 154);
        this->label2->Name = L"label2";
        this->label2->Size = System::Drawing::Size(78, 17);
        this->label2->TabIndex = 3;
        this->label2->Text = L"Styrenhet";
        this->label2->Click += gcnew System::EventHandler(this, &Form1::label2_Click);
        // 
        // textBox2
        // 
        this->textBox2->Location = System::Drawing::Point(12, 174);
        this->textBox2->Multiline = true;
        this->textBox2->Name = L"textBox2";
        this->textBox2->ReadOnly = true;
        this->textBox2->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
        this->textBox2->Size = System::Drawing::Size(242, 150);
        this->textBox2->TabIndex = 2;
        this->textBox2->TextChanged += gcnew System::EventHandler(this, &Form1::textBox2_TextChanged);
        // 
        // label3
        // 
        this->label3->AutoSize = true;
        this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
            static_cast<System::Byte>(0)));
        this->label3->Location = System::Drawing::Point(12, 323);
        this->label3->Name = L"label3";
        this->label3->Size = System::Drawing::Size(100, 17);
        this->label3->TabIndex = 5;
        this->label3->Text = L"Sensorenhet";
        // 
        // button1
        // 
        this->button1->Location = System::Drawing::Point(269, 125);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(125, 47);
        this->button1->TabIndex = 6;
        this->button1->Text = L"Manuell Styrning På/Av";
        this->button1->UseVisualStyleBackColor = true;
        this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
        // 
        // button2
        // 
        this->button2->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"button2.Image")));
        this->button2->Location = System::Drawing::Point(330, 178);
        this->button2->Name = L"button2";
        this->button2->Size = System::Drawing::Size(55, 55);
        this->button2->TabIndex = 7;
        this->button2->UseVisualStyleBackColor = true;
        this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
        // 
        // button3
        // 
        this->button3->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"button3.Image")));
        this->button3->Location = System::Drawing::Point(269, 178);
        this->button3->Name = L"button3";
        this->button3->Size = System::Drawing::Size(55, 55);
        this->button3->TabIndex = 8;
        this->button3->UseVisualStyleBackColor = true;
        // 
        // button4
        // 
        this->button4->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"button4.Image")));
        this->button4->Location = System::Drawing::Point(269, 239);
        this->button4->Name = L"button4";
        this->button4->Size = System::Drawing::Size(55, 55);
        this->button4->TabIndex = 9;
        this->button4->UseVisualStyleBackColor = true;
        // 
        // button5
        // 
        this->button5->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"button5.Image")));
        this->button5->Location = System::Drawing::Point(391, 178);
        this->button5->Name = L"button5";
        this->button5->Size = System::Drawing::Size(55, 55);
        this->button5->TabIndex = 10;
        this->button5->UseVisualStyleBackColor = true;
        // 
        // button6
        // 
        this->button6->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"button6.Image")));
        this->button6->Location = System::Drawing::Point(391, 239);
        this->button6->Name = L"button6";
        this->button6->Size = System::Drawing::Size(55, 55);
        this->button6->TabIndex = 11;
        this->button6->UseVisualStyleBackColor = true;
        // 
        // button8
        // 
        this->button8->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"button8.Image")));
        this->button8->Location = System::Drawing::Point(330, 239);
        this->button8->Name = L"button8";
        this->button8->Size = System::Drawing::Size(55, 55);
        this->button8->TabIndex = 13;
        this->button8->UseVisualStyleBackColor = true;
        // 
        // pictureBox1
        // 
        this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureBox1.Image")));
        this->pictureBox1->Location = System::Drawing::Point(480, 47);
        this->pictureBox1->Name = L"pictureBox1";
        this->pictureBox1->Size = System::Drawing::Size(302, 401);
        this->pictureBox1->TabIndex = 14;
        this->pictureBox1->TabStop = false;
        // 
        // label4
        // 
        this->label4->AutoSize = true;
        this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
            static_cast<System::Byte>(0)));
        this->label4->Location = System::Drawing::Point(475, 19);
        this->label4->Name = L"label4";
        this->label4->Size = System::Drawing::Size(59, 25);
        this->label4->TabIndex = 15;
        this->label4->Text = L"Karta";
        // 
        // textBox3
        // 
        this->textBox3->ImeMode = System::Windows::Forms::ImeMode::NoControl;
        this->textBox3->Location = System::Drawing::Point(269, 35);
        this->textBox3->Name = L"textBox3";
        this->textBox3->Size = System::Drawing::Size(100, 20);
        this->textBox3->TabIndex = 16;
        this->textBox3->TextChanged += gcnew System::EventHandler(this, &Form1::textBox4_TextChanged);
        // 
        // label5
        // 
        this->label5->AutoSize = true;
        this->label5->Location = System::Drawing::Point(266, 19);
        this->label5->Name = L"label5";
        this->label5->Size = System::Drawing::Size(53, 13);
        this->label5->TabIndex = 17;
        this->label5->Text = L"COM-Port";
        this->label5->Click += gcnew System::EventHandler(this, &Form1::label5_Click);
        // 
        // button7
        // 
        this->button7->Location = System::Drawing::Point(269, 61);
        this->button7->Name = L"button7";
        this->button7->Size = System::Drawing::Size(75, 23);
        this->button7->TabIndex = 18;
        this->button7->Text = L"Anslut";
        this->button7->UseVisualStyleBackColor = true;
        this->button7->Click += gcnew System::EventHandler(this, &Form1::button7_Click);
        // 
        // button9
        // 
        this->button9->Enabled = false;
        this->button9->Location = System::Drawing::Point(350, 61);
        this->button9->Name = L"button9";
        this->button9->Size = System::Drawing::Size(75, 23);
        this->button9->TabIndex = 19;
        this->button9->Text = L"Koppla Från";
        this->button9->UseVisualStyleBackColor = true;
        this->button9->Click += gcnew System::EventHandler(this, &Form1::button9_Click);
        // 
        // label6
        // 
        this->label6->AutoSize = true;
        this->label6->Location = System::Drawing::Point(375, 38);
        this->label6->Name = L"label6";
        this->label6->Size = System::Drawing::Size(59, 13);
        this->label6->TabIndex = 20;
        this->label6->Text = L"Ej ansluten";
        // 
        // textBox4
        // 
        this->textBox4->Location = System::Drawing::Point(16, 365);
        this->textBox4->Name = L"textBox4";
        this->textBox4->ReadOnly = true;
        this->textBox4->Size = System::Drawing::Size(45, 20);
        this->textBox4->TabIndex = 21;
        // 
        // label7
        // 
        this->label7->AutoSize = true;
        this->label7->Location = System::Drawing::Point(12, 347);
        this->label7->Name = L"label7";
        this->label7->Size = System::Drawing::Size(49, 13);
        this->label7->TabIndex = 22;
        this->label7->Text = L"Sensor 1";
        // 
        // label8
        // 
        this->label8->AutoSize = true;
        this->label8->Location = System::Drawing::Point(71, 347);
        this->label8->Name = L"label8";
        this->label8->Size = System::Drawing::Size(49, 13);
        this->label8->TabIndex = 24;
        this->label8->Text = L"Sensor 2";
        // 
        // textBox5
        // 
        this->textBox5->Location = System::Drawing::Point(75, 365);
        this->textBox5->Name = L"textBox5";
        this->textBox5->ReadOnly = true;
        this->textBox5->Size = System::Drawing::Size(45, 20);
        this->textBox5->TabIndex = 23;
        // 
        // label9
        // 
        this->label9->AutoSize = true;
        this->label9->Location = System::Drawing::Point(126, 347);
        this->label9->Name = L"label9";
        this->label9->Size = System::Drawing::Size(49, 13);
        this->label9->TabIndex = 26;
        this->label9->Text = L"Sensor 3";
        // 
        // textBox6
        // 
        this->textBox6->Location = System::Drawing::Point(130, 365);
        this->textBox6->Name = L"textBox6";
        this->textBox6->ReadOnly = true;
        this->textBox6->Size = System::Drawing::Size(45, 20);
        this->textBox6->TabIndex = 25;
        // 
        // label10
        // 
        this->label10->AutoSize = true;
        this->label10->Location = System::Drawing::Point(9, 388);
        this->label10->Name = L"label10";
        this->label10->Size = System::Drawing::Size(49, 13);
        this->label10->TabIndex = 28;
        this->label10->Text = L"Sensor 4";
        // 
        // textBox7
        // 
        this->textBox7->Location = System::Drawing::Point(13, 406);
        this->textBox7->Name = L"textBox7";
        this->textBox7->ReadOnly = true;
        this->textBox7->Size = System::Drawing::Size(45, 20);
        this->textBox7->TabIndex = 27;
        // 
        // label11
        // 
        this->label11->AutoSize = true;
        this->label11->Location = System::Drawing::Point(71, 388);
        this->label11->Name = L"label11";
        this->label11->Size = System::Drawing::Size(49, 13);
        this->label11->TabIndex = 30;
        this->label11->Text = L"Sensor 5";
        // 
        // textBox8
        // 
        this->textBox8->Location = System::Drawing::Point(75, 406);
        this->textBox8->Name = L"textBox8";
        this->textBox8->ReadOnly = true;
        this->textBox8->Size = System::Drawing::Size(45, 20);
        this->textBox8->TabIndex = 29;
        // 
        // label12
        // 
        this->label12->AutoSize = true;
        this->label12->Location = System::Drawing::Point(126, 388);
        this->label12->Name = L"label12";
        this->label12->Size = System::Drawing::Size(49, 13);
        this->label12->TabIndex = 32;
        this->label12->Text = L"Sensor 6";
        // 
        // textBox9
        // 
        this->textBox9->Location = System::Drawing::Point(130, 406);
        this->textBox9->Name = L"textBox9";
        this->textBox9->ReadOnly = true;
        this->textBox9->Size = System::Drawing::Size(45, 20);
        this->textBox9->TabIndex = 31;
        // 
        // serialPort1
        // 
        this->serialPort1->BaudRate = 115200;
        this->serialPort1->RtsEnable = true;
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(789, 485);
        this->Controls->Add(this->label12);
        this->Controls->Add(this->textBox9);
        this->Controls->Add(this->label11);
        this->Controls->Add(this->textBox8);
        this->Controls->Add(this->label10);
        this->Controls->Add(this->textBox7);
        this->Controls->Add(this->label9);
        this->Controls->Add(this->textBox6);
        this->Controls->Add(this->label8);
        this->Controls->Add(this->textBox5);
        this->Controls->Add(this->label7);
        this->Controls->Add(this->textBox4);
        this->Controls->Add(this->label6);
        this->Controls->Add(this->button9);
        this->Controls->Add(this->button7);
        this->Controls->Add(this->label5);
        this->Controls->Add(this->textBox3);
        this->Controls->Add(this->label4);
        this->Controls->Add(this->pictureBox1);
        this->Controls->Add(this->button8);
        this->Controls->Add(this->button6);
        this->Controls->Add(this->button5);
        this->Controls->Add(this->button4);
        this->Controls->Add(this->button3);
        this->Controls->Add(this->button2);
        this->Controls->Add(this->button1);
        this->Controls->Add(this->label3);
        this->Controls->Add(this->label2);
        this->Controls->Add(this->textBox2);
        this->Controls->Add(this->label1);
        this->Controls->Add(this->textBox1);
        this->Name = L"Form1";
        this->Text = L"Cthulhu";
        this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
        (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
        this->ResumeLayout(false);
        this->PerformLayout();

    }
    #pragma endregion
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void label2_Click(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void textBox2_TextChanged(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void textBox4_TextChanged(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void label5_Click(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) {
             try {
                 serialPort1->Open();
             }  
             catch (...) {
                 label6->Text = "Ett Fel Uppstod";
             }

             textBox4->ReadOnly = true;
             button7->Enabled = false;
             button9->Enabled = true;
             label6->Text = "Ansluten";

         }


private: System::Void button9_Click(System::Object^  sender, System::EventArgs^  e) {
             serialPort1->Close();

             textBox4->ReadOnly = false;
             button7->Enabled = true;
             button9->Enabled = false;
             label6->Text = "Ej Ansluten";
         }
};
    }

1 Ответ

0 голосов
/ 29 декабря 2012

Вам нужна ссылка на объект Form1, вы не можете использовать тип Form1.Если вы не объявите serialPort1 как статическое.

(как прокомментировал Ганс Пассант, с небольшой очисткой)

Я бы добавил, внутри экземпляра Form1, вы можете увидеть, как кодгенератор ссылается на последовательный порт, который

this->serialPort1
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...