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

8 thoughts on “SERIAL PORT AND VISUAL STUDIO”

  1. I can open the serial port. Boundrate,DataBits and other properteries are correct. When I recieve data stream, it comes 16 bytes blocks. 1th one identifier byte. 2,3,4. bytes are values of time. 5,6. bytes values of speed. 7,8,9,10.th bytes values of distance and 11,12,13,14,15,16th bytes are zero. For example, I can read bytes with using this “identifier_byte= _serialPort->ReadByte();”
    For collecting datas (I had to collect 3 bytes here), I use this,

    time_trigger2 = _serialPort->ReadByte();
    time_trigger3 = _serialPort->ReadByte();
    time_trigger4 = _serialPort->ReadByte();
    total_time=(time_trigger4<<16) | (time_trigger3<<8) | time_trigger2;
    "
    But I can't handle data stream and I receieve complicated datas. It should be regular for each 16 bytes data stream. Help me.Thanks 🙂

  2. Interesting, but I am getting 31 errors after following these directions, starting with ‘data1’: undeclared identifier. Any way of getting a copy of your Form1.h after all these edits? I am just learning Visual C++ after several years of embedded C programming…

  3. Ok, that line helps. Also, when I just copied and pasted the strings, the webpage uses different quote symbols, so I had to change them to standard quote symbols. Got rid of all but one of my errors!

    1. Hi There,

      The thing is I am putting the data string received in a text-box so that there is a user satisfaction that the process is going on smoothly.You can definitely use that data by declaring a “GLOBAL” system string and passing the textbox stuff into it. Even the tutorial includes that,I mean the String name Data stores it. If you need any further help, do ping me in on this mail ID- abhishek4273@gmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s