Serial port C++/CLI第二种形式的相同串行端口

Serial port C++/CLI第二种形式的相同串行端口,serial-port,c++-cli,command-line-interface,windows-forms-designer,Serial Port,C++ Cli,Command Line Interface,Windows Forms Designer,我正在编写一个程序,向串口发送命令。 到目前为止,通信工作正常 现在,我添加了一个新表单,我想找到端口名并在新表单中打开端口。之后,我想在我的第一个表单中使用连接的串行端口 我总是会出错: 错误1错误C2039:“SerialPort”:不是“test3::Form2”的成员 表格1: #pragma once #include <windows.h> #using <System.dll> #include <iostream> //for usin

我正在编写一个程序,向串口发送命令。 到目前为止,通信工作正常

现在,我添加了一个新表单,我想找到端口名并在新表单中打开端口。之后,我想在我的第一个表单中使用连接的串行端口

我总是会出错:

错误1错误C2039:“SerialPort”:不是“test3::Form2”的成员

表格1:

#pragma once
#include <windows.h>
#using <System.dll>
#include <iostream>     //for using cout
#include <stdlib.h>     //for using the function sleep
#include <string>
#include <sstream>
#include <msclr/marshal_cppstd.h>
#include <time.h>
#include <stdio.h>
#include <ctime>
#include <cstdio>
#include "Form2.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;
    using namespace std;
    using namespace System::Diagnostics;
    using namespace System::IO;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form

    {
    public:

        Form1(void)
        {
            InitializeComponent();
            findPorts();



                 // disable this text field

                 this->textBox2->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::Void button10_Click(System::Object^  sender, System::EventArgs^  e) {

             Form^ f2= gcnew Form2(this->_serialPort);
             f2->Show();
         }
};
表格2:

#pragma once
#include <windows.h>
#using <System.dll>
#include <iostream>     //for using cout
#include <stdlib.h>     //for using the function sleep
#include <string>
#include "Form1.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;
    using namespace std;
    using namespace System::Diagnostics;
    using namespace System::IO;

    /// <summary>
    /// Summary for Form2
    /// </summary>
    public ref class Form2 : public System::Windows::Forms::Form
    {

    public:
        Form2(SerialPort^ _serialPort)
        {
            InitializeComponent();




                this->SerialPort = _serialPort;


            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form2()
        {
            if (components)
            {
                delete components;
            }
        }

“test3::Form2”中没有“SerialPort”成员字段或属性。编译器错误很明显。只需添加一个新的字段/属性来存储您在Form2的构造函数中接收到的引用。我该怎么做?我对这个C++/CLIWell不太了解,正如您对Form1所做的:“private:System::IO::Ports::SerialPort^_SerialPort;”好的,谢谢,现在可以了,但是我现在如何使用form2中form1的serialport,例如打开端口或其他什么?此->\u serialport不起作用,form1->\u serialport也不起作用。对不起,我对这种类型的编程非常陌生