How do I make local copy of a handle for a serail port class
I’m trying to pass a handle to a serial port class to a form that will configure the serial port.
I can pass a handle to the constructor and it works fine/as I expect inside the constructor but I need a local copy of the handle that points to the port I can use after the constructor is finished.
I can declare a local serial port class var.
I can copy of the original serial port’s class data to that var.
But I want a handle to the original not a local copy of its data.
public ref class Config_Serial_Port_Form : public System::Windows::Forms::Form
{
public:
Config_Serial_Port_Form(System::IO::Ports::SerialPort^% initport)
{
initport->BaudRate = 600; //this accesses the correct port
theport = initport; //this only copies all the original to the local object
//I need theport to be a pointer
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
void domystuff(void) //all this stuff works but only on my local copy "theport" I want to do this to the original object instead
{
port_stuff^ port_stuff_ptr;
port_stuff_ptr = gcnew port_stuff();
this->Baud_Rate_comboBox->Text = port_stuff_ptr->BaudRate_to_string(theport);
this->Parity_comboBox->Text = port_stuff_ptr->Parity_to_string(theport);
this->handshakingcomboBox->Text = port_stuff_ptr->Handshake_to_string(theport);
this->databitscomboBox->Text = port_stuff_ptr->DataBits_to_string(theport);
if (theport->IsOpen)
this->portselectcomboBox->Text = theport->PortName;
this->portselectcomboBox->Items->AddRange(theport->GetPortNames());
this->portselectcomboBox->Items->Add("COM4");
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Config_Serial_Port_Form()
{
if (components)
{
delete components;
}
}
private: System::IO::Ports::SerialPort^ theport; /// this gives me a second object not a handle to the original.
Re: How do I make local copy of a handle for a serail port class
I can spell serial I just can't figure out how to edit the name of a thread
Re: How do I make local copy of a handle for a serail port class
I'd bet you have added the SerialPort member to the form class using the Forms Designer. That way the member function InitializeComponent() instanciates a new SerialPort object and assigns it to theport. Since the constructor calls this member after you have assigned the passed SerialPort object handle to theport, that handle will be overwritten in that step.
To fix that, simply remove the SerialPort object in Forms Designer and re-add a member variable of the same name and type manually. (Don't try to be smart and simply remove the instantiation of the port object from InitializeComponent(). The IDE really wouldn't like that.)
There's also no reason to declare the SerialPort object handle passed to the constructor as a tracking handle reference (^%). You don't want to modify the original variable holding the handle you get passed, so you can simply declare the parameter as tracking handle (^). Traking handles behave like pointers, and so you'll modify the original SerialPort object by accessing it via that handle, which is what you want.
The code you posted doesn't show your implementation of the port_stuff class, but I certainly would have implemented it differently (if at all). It looks like all the members of that class you use here could as well be static (because you always pass the port object handle to act on as a parameter) and that way you wouldn't need to instantiate an object of that class at all. Either that, or pass the port handle upon construction, store it in the object and drop that parameter.
Quote:
Originally Posted by
gpsdude
I can spell serial I just can't figure out how to edit the name of a thread
:lol: I probably wouldn't have noticed that typo at all without that post.
No, you can't do that. You just can edit the titles of individual posts.
Please use code tags.