Tag Archives: Visual Studio

SERIAL PORT AND VISUAL STUDIO

Hello everyone,

Getting a little off-track from our image processing,in my internship I had to integrate Serial Port with Visual Studio(C++).In that process I got stuck in retreiving data from the serial port as the event handler errors were coming up.After a very intensive research I tried to compile all my knowledge to implement it and “KA-BOOM”….. it worked 🙂

So I would like to share with you the entire process.

First open a windows c++ form application.

Then,in the form the very first thing you do is add a serial port from tool-box,in its properties set the BaudRate,Parity,StopBits,Number of bits to be transferred according to the hardware.

Now,select “button” from tool-box and drop it down on the form,set Text = Transfer and Name = btnTransfer.
Select another button from tool-box and drop it down on the form.set Text = Open and Name = btnOpen.
Select another button from tool-box and drop it down on the form.set Text = Close and Name = btnClose.

Select a comboBox from tool-box and drop it down on the form.Change its DropDown Style poperty from DropDown to DropDownList.

Select a label and place it in front of the comboBox and name it “Port”.

Select “TextBox” from tool-box and drop it down on the form,set Name = txtTransfer.

Double click anywhere on the form to go to the form1.h file.

Firstly in the namespace add:
using namespace System::IO::Ports;

Then after the following part of the code:
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();

Add This: findPorts();

Just before the “#pragma region” add:
String^ Data;
String^ data1;

Now inside the function named void “InitializeComponent(void)” add:
this->serialPort1->DataReceived += gcnew System::IO::Ports::SerialDataReceivedEventHandler(this, &Form1::serialPort1_DataReceived_1);

After the “#pragma endregion” add:

private: void findPorts(void)
{
// get port names
array<Object^>^ objectArray = SerialPort::GetPortNames();
// add string array to combobox
this->comboBox1->Items->AddRange( objectArray );
}

Come to the form(GUI) and select the serial port,in the properties window,select the bar which is like lightning flashing and click on the DataReceieved and write its value as “serialPort1_DataReceived_1”.

Now in the code add this:

private: System::Void SetTextCallback(System::Object^ sender, System::EventArgs^ e)
{
this->txtTransfer->AppendText(data1);
Data = this->txtTransfer->Text;
}

private: System::Void serialPort1_DataReceived_1(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
data1 = serialPort1->ReadExisting();
this->Invoke(gcnew EventHandler(this, &Form1::SetTextCallback));
}

Now go to the form and double click on “Open” button and paste the code inside the function created:

if(this->comboBox1->Text==String::Empty)
this->txtTransfer->Text=”Please Select Port Settings”;
else {
try{
// make sure port isn’t open
if(!this->serialPort1->IsOpen){
this->serialPort1->PortName=this->comboBox1->Text;

//open serial port
this->serialPort1->Open();
}
else
this->txtTransfer->Text=”Port isn’t openned”;
}
catch(UnauthorizedAccessException^){
this->txtTransfer->Text=”UnauthorizedAccess”;
}
}

Now go to the form and double click on “Transfer” button and paste the code inside the function created:

this->serialPort1->Write(“THE INVOKING STRING GOES HERE”);

Now go to the form and double click on “Close” button and paste the code inside the function created:

this->serialPort1->Close();

Compile and run it.
And as soon as the string reaches the port you will receive the data immediately in the textBox we created.

Thank you 🙂

Advertisements