Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
i am trying to read and Write the data from microcontroller to c++/cli application using protocol . Write data were successfully done but i am unable to received the data.

What I have tried:

private: System::Void main_form_Load(System::Object^ sender, System::EventArgs^ e)
   {
	serialPort1->PortName = "COM5";
	serialPort1->BaudRate = 9600;
	serialPort1->DtrEnable = true;
		
   }
private: System::Void ReadDatabtn_Click(System::Object^ sender, System::EventArgs^ e)
{
	
		      serialPort1->DataReceived += gcnew SerialDataReceivedEventHandler( SP_DataReceived);
		    serialPort1->Open();
			commentbox->Text = "Read OK";
			commentbox->BackColor = Color::Green;
            serialPort1->Close();
	       
	}
 private:static void SP_DataReceived(Object^ sender, SerialDataReceivedEventArgs^ e)
		  {
			 SerialPort^ sp = (SerialPort^)sender;
             String^ start = "\x52\x4c\x53\x73\x64\r\n\r\n";//Read start command
			String^ end = "\x52\x4c\x45\x73\x64\r\n\r\n";//Read End command
			sp->ReadTimeout = 2000;

			  try
			  {
                  sp->Write(start );
                  Thread::Sleep(100);
				  String^ str = sp->ReadExisting();
				  Console::Write("dataReceived ",str +"\n");
                  Thread::Sleep(100);
			      sp->Write(end);
				  
			  }
			  catch (Exception^ ex)
			  {
				  Console::Write("data_received Error");
				 // MessageBox::Show("s");
			  }
		  }
private: System::Void Sendbtn_Click(System::Object^ sender, System::EventArgs^ e)
{
		{
			   serialPort1->Open();
			{
				String^ data = "\x57\x54\x53\x73\x64\r\n\r\n";//Write start command
				String^ data1 = "\x57\x54\x45\x73\x64\r\n\r\n";/write end command
				Clockw->Text = "20220809145722";
				serialPort1->Write(data);
				Thread::Sleep(10);
				 serialPort1->Write(Clockw->Text);
				 Console::Write(Clockw->Text);
				Thread::Sleep(10);
				serialPort1->Write(data1);
				commentbox->Text = "Date Write OK";
				commentbox->BackColor = Color::Green;

			}
			serialPort1->Close();
		}

		
		
}
Posted
Updated 15-Aug-22 22:11pm
v3

1 solution

Take a look at Serial Port I/O[^] which explains how best to use the serial ports.
 
Share this answer
 

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