Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a GUI in Windows with Visual Studio, and that GUI has a working serial communication. Now I added an another cpp file to project and I would like that cpp to communicate with already opened com port. What would be the easiest way for absolute beginner to try to make that work? Or is it even possible?

What I have tried:

MyForm.h (gui) (shortened):
C++
using namespace System;
    using namespace System::IO::Ports;

public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            findPorts();
        }

protected:
private: System::IO::Ports::SerialPort^ serialPort1;

#pragma region Windows Form Designer generated code  
        void InitializeComponent(void)
        {
          this->components = (gcnew System::ComponentModel::Container());
          this->serialPort1 = (gcnew System::IO::Ports::SerialPort(this->components));
        }
#pragma endregion
    private: void findPorts(void)
    {
        array<Object^>^ objectArray = SerialPort::GetPortNames();
    }
private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e) {
    //add sender name
    String^ name = this->serialPort1->PortName;
    //grab text and store in send buffer
    String^ message = "hello";
    //write to serial
    if (this->serialPort1->IsOpen)
        this->serialPort1->WriteLine(message);
    else
        this->textBox2->Text = "Port Not Opened";
}
Posted
Comments
Richard MacCutchan 31-Dec-21 11:31am    
The other file should contain a function that the above program calls with the open port reference as a parameter to the function.
WantToLearn1 1-Jan-22 3:50am    
Thanks! I will try.

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