Click here to Skip to main content
15,868,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++/CLI. I can use serial port in myForm.h, but I need to send serial port messages also from file.cpp. How do I do that?

myForm.h:
C++
protected:
private: System::IO::Ports::SerialPort^ serialPort1;


In myForm.h I can send message to serial port this way:
this->serialPort1->WriteLine(message);


What I have tried:

I don't have a clue how to send a message to serial port from some other file than myForm.h...
Posted
Updated 25-Jan-23 4:54am

1 solution

You do it exactly the same way, create a SerialPort object reference and call the relevant method(s).
C++
System::IO::Ports::SerialPort^ serialPort1;
// set any speed. parity values etc.
serialPort1->WriteLine(message);
 
Share this answer
 
Comments
WantToLearn1 26-Jan-23 5:00am    
Even the port is already opened in myForm.h?
Richard MacCutchan 26-Jan-23 6:03am    
Then you just need to pass the reference object to the other file. Same as any similar methods in C++/CLI.
WantToLearn1 26-Jan-23 7:09am    
Is the serialPort1 the object? I changed it to public for a start.
Richard MacCutchan 26-Jan-23 7:46am    
Yes, but you only need to make it public if the code that you want to run is using a reference to the myForm class.
WantToLearn1 26-Jan-23 8:58am    
Thanks. I need to study more how these things work and how to use myForm class from another cpp.

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