Arduino is not reset when closing the serial port in c ++

I installed grbl v0.9, and when I access the serial port from Tools/Serial Monitor, in arduino app , or the app "Universal Gcode Sender", the arduino returns me:

Grbl 0.9j [ '$' for help]
[ '$ H' | '$ X' to unlock]

Perfect. This is how it has to work.

I made a program in c++ to handle the serial port. If I close the port and I open them again, the arduino is not reset and not the blocking message appears.

#include "MyForm.h"
using namespace Project1;


System::Void MyForm::findPorts()
{
	// get port names
	array<Object^>^ objectArray = SerialPort::GetPortNames();
	this->ComSelect->Items->AddRange(objectArray);
}

System::Void MyForm::OpenButton_Click(System::Object^  sender, System::EventArgs^  e)
{
	if (this->ComSelect->Text == String::Empty || this->BaudSelect->Text == String::Empty)
		this->LogBox->AppendText("Please Select Port Settings\r\n");
	else {
		try {
			// make sure port isn't open	
			if (!this->serialPort1->IsOpen) {
				this->serialPort1->PortName = this->ComSelect->Text;
				this->serialPort1->BaudRate = Int32::Parse(this->BaudSelect->Text);
				this->serialPort1->Parity = IO::Ports::Parity::None;
				this->serialPort1->StopBits = IO::Ports::StopBits::One;
				this->serialPort1->DataBits = 8;
				this->serialPort1->Open();
				this->CommandBar->Text = "Enter Message Here";
				this->LogBox->AppendText("\r\n**** Connected to " + ComSelect->Text + " at " + BaudSelect->Text + " Baud ****\r\n");
				//open serial port 
				this->progressBar1->Value = 100;
				// Disable the init button 
				this->OpenButton->Enabled = false;
				this->ComSelect->Enabled = false;
				this->BaudSelect->Enabled = false;
				this->CloseButton->Enabled = true;
				this->SendButton->Enabled = true;
				AfterEvent(); // Send ctrl-x 
			}
			else
				this->LogBox->AppendText("\r\nPort isn't openned\r\n");
		}
		catch (UnauthorizedAccessException^) {
			this->LogBox->AppendText("\r\nUnauthorizedAccess\r\n");
		}
	}
}

System::Void MyForm::AfterEvent()
{
	auto data = gcnew array<System::Byte> {0x18}; //send ctrl-x
	this->serialPort1->Write(data, 0, data->Length);
}

System::Void MyForm::CloseButton_Click(System::Object^  sender, System::EventArgs^  e)
{
	//close serialPort
	if (this->serialPort1->IsOpen)
	{
		//this->serialPort1->WriteLine("\u0018"); //Send ctrl-x to reset
		this->serialPort1->Close();
		// update progress bar
		this->progressBar1->Value = 0;
		// Enable the init button
		this->OpenButton->Enabled = true;
		this->ComSelect->Enabled = true;
		this->BaudSelect->Enabled = true;
		this->CloseButton->Enabled = false;
		this->SendButton->Enabled = false;
		this->LogBox->AppendText("\r\n**** Connection Closed ****\r\n\r\n");
		//this->LogBox->Text = String::Empty;
	}
	else
		this->LogBox->AppendText("\r\nPort Not Opened\r\n");

}

	System::Void MyForm::CommandBar_click(System::Object^  sender, System::EventArgs^  e)
	{
		this->CommandBar->Text = String::Empty;
	}

	System::Void MyForm::EnterKeyBar(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
	{
		if (e->KeyCode == Keys::Enter)
		{
			// add sender name
			//String^ name = this->serialPort1->PortName;
			// grab text and store in send buffer
			String^ message = this->CommandBar->Text;
			// write to serial
			if (this->serialPort1->IsOpen)
			{
				//this->_serialPort->WriteLine(String::Format("<{0}>: {1}",name,message));
				this->serialPort1->WriteLine(message);
				this->LogBox->AppendText(">>>> " + message + "\r\n");
				this->CommandBar->Text = String::Empty;
			}
			else
				this->LogBox->AppendText("\r\nPort Not Opened\r\n");
		}
	}

	System::Void MyForm::SendButton_Click(System::Object^  sender, System::EventArgs^  e)
	{
		// grab text and store in send buffer
		String^ message = this->CommandBar->Text;
		// write to serial
		if (this->serialPort1->IsOpen)
		{
			this->serialPort1->WriteLine(message);
			this->CommandBar->Text = String::Empty;
			this->LogBox->AppendText(">>>> " + message + "\r\n");
		}
		else
			this->LogBox->AppendText("\r\nPort Not Opened\r\n");
	}

	System::Void MyForm::serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e)
	{
		// richLogBox->Text = "Received Event";
		String^ myString = serialPort1->ReadExisting();
		DelegateType ^pfnDelegate = gcnew DelegateType(this, &MyForm::DataReceived);
		this->LogBox->BeginInvoke(pfnDelegate, myString);
	}

	System::Void MyForm::DataReceived(String ^myString)
	{
		try
		{
			this->LogBox->AppendText(myString);
			//LogBox->ScrollToCaret();
		}
		catch (Win32Exception^ ex)
		{
			this->LogBox->Text = ex->Message;
		}
	}

	System::Void MyForm::ResetButton_Click(System::Object^  sender, System::EventArgs^  e)
	{
		if (this->serialPort1->IsOpen)
		{
			AfterEvent(); // send ctrl-x
			/*auto data = gcnew array<System::Byte> {0x18};
			this->serialPort1->Write(data, 0, data->Length);*/
		}
		else
			this->LogBox->Text = "Port Not Opened\r\n";
	}

All files are here.

¿Someone knows why can it be?

What Operating System are you using on your PC?

Does the C++ program cause the Arduino to reset properly the first time the C++ program is run?

If so it suggests to me that the C++ program is no properly closing the Serial Port.

...R

Hi @Robin2,

I'm using Visual Studio 2015 on Windows 10.

Access the Arduino in this order.

ArduApp open/close serial port OK
VSC++ open serial port Ok -> close serial port-> open serial port Fail.
--Close VS program and open again.
VSC++ open Fail.
ArduApp open/close OK

(Ok and Fail -> lock message)

Although the serial port does not properly close, ArduApp opens the serial port well.
I think, as @Robin2 says, that does not close properly and the serial port. But if it is not closed. Why with another app opens it well?

Which Arduino board are you using ?
Not all of them reset when the serial port is opened, the Leonardo for example.

I'm using UNO.

This seems to be a VSC++ problem rather than an Arduino problem.

I don't know anything about VSC++ or Windows (I vaguely remember some XP stuff).

What code is used by your VSC++ program to close the serial port?

You have not said whether your VSC++ program will work OK after the Arduino Serial Monitor opens and closes the port?

...R

using namespace System::IO::Ports;
this->serialPort1->Close();

System::IO::Ports is a VSC++ library.

Yes, after the Arduino Serial Monitor opens and closes the port, my program in VSC++ open the port OK, but even if you close the entire VS program, not reopen the port correctly.

I think the problem is to close the port, but my question is also that if the port is not closed, how is that arduino monitor if reopens well without showing any error messages?

You can close a port in several ways? or arduino can interpret it in different ways?

As far as I understand it, you need to activate DTR to reset the Arduino.

sterretje:
As far as I understand it, you need to activate DTR to reset the Arduino.

That is certainly worth trying, but the OP has said that the VSC++ code does reset the Arduino some of the time which leads me to suspect a different problem.

@nowi, I know it is rather crude, but what would would happen if your VSC++ code opens and immediately closes the serial port and then opens it again ?

...R

sterretje:
As far as I understand it, you need to activate DTR to reset the Arduino.

:o YES!!
Thank you very much. Solved.

Sometimes we can't see simple or obvious things. :slight_smile:

Robin2:
@nowi, I know it is rather crude, but what would would happen if your VSC++ code opens and immediately closes the serial port and then opens it again ?

In the port properties I have disabled the Dtr and Rts.