Thursday, 8 January 2015

Serial Communication between Arduino and Visual C++


Interfacing is one big problem which hobbyists or students face while approaching towards a project. Most of the time is spent in eliminating the errors and the essential work of making and testing gets lesser time.

After browsing many web pages(which claim to serve the purpose) I finally decided to write a tutorial regarding serial communication between Arduino and Visual Studio 2010.

PS : This tutorial explains how to communicate with arduino hardware via Visual Studio. If you are searching for writing arduino code in Visual C++ refer to my another tutorial(coming soon).
Scroll below for step wise screen shots.


Installation of essential softwares :

  • Here I am using Microsoft Visual Studio 2010 Professional version. You can get student version for free on Microsoft DreamSpark 
  • For Arduino drivers install arduino IDE from its official website.

Additional files :

  • The code for serial communication class is available on arduino's official website or you can download the files here.
  • Move the files to the project folder.

Setting up IDE :

  • Create a new project in Visual Studio.(File > New > Project > Windows 32 console application)
  • Copy the two files Serial.cpp and SerialClass.h to project directory.
  • Now go to Project menu and select Properties.(Alt+F7).
  • In Configuration Properties > General set Character set to Multi-Byte Character
  • Then select General in C/C++ in Configuration Properties.
  • On the right side select Additional Include Directories > edit. Add the path to the files above.
  • Now in Solution explorer(left side of the ide) add serial.cpp file in Source Files.
  • Now in sample.cpp file in Solution Explorer, copy and run the following test.

Desired Output :

  • In the output console it will show "Welcome to serial test app."If arduino is not connected then it will show handle not attached.
  • You need to change the port number (COM) in the code to the port where your arduino is connected. You can check the COM port number through arduino ide.
  • If everything works fine it will show "We're connected"





Arduino connected

Arduino not connected or COM port is not correct


// sample1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include "SerialClass.h" // Library described above
#include <string>

// application reads from the specified serial port and reports the collected data
int _tmain(int argc, _TCHAR* argv[])
{
printf("Welcome to the serial test app!\n\n");

Serial* SP = new Serial("\\\\.\\COM18");    // adjust as needed

if (SP->IsConnected())
printf("We're connected");

char incomingData[256] = ""; // don't forget to pre-allocate memory
//printf("%s\n",incomingData);
int dataLength = 256;
int readResult = 0;

while(SP->IsConnected())
{
readResult = SP->ReadData(incomingData,dataLength);
printf("Bytes read: (-1 means no data available) %i\n",readResult);

std::string test(incomingData);

printf("%s",incomingData);
test = "";

Sleep(500);
}
return 0;
}

 

Comment below if faced with any difficulty. Thank you.

6 comments:

  1. I want to meet you Akash...

    ReplyDelete
  2. Thank You I was looking for this for a long time. :)

    ReplyDelete
  3. Find all Casino Game Shows 2021 - Dr.MD
    Find all Casino Game Shows 2021 Las Vegas 전주 출장안마 is now 김천 출장마사지 open and accepting players over 전라남도 출장샵 21 and over. We want you 여주 출장마사지 to have 포천 출장샵 fun in the casinos you are

    ReplyDelete