Serial Port Control with Visual Studio 2008 and C++

Reading and writing a serial port using Visual Studio Express

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 70375 Views - Last Post: 04 January 2010 - 04:21 AM Rate Topic: -----

#1 dk-info  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-March 09

Serial Port Control with Visual Studio 2008 and C++

Posted 02 July 2009 - 03:00 PM

There are literally hundreds of postings here and elsewhere concerning the serial port and various tools and compilers, but none seem to describe how to access serial port resources under Windows (XP & Vista) using the free Microsoft Visual Studio Express 2008 tools and Visual C++.

I have "grepped" (The great grandfather of "Googled") the internet for just this information only to find old snippets of Borland code, Visual Basic 2005 and Visual C# examples. I have also seen vague references to "Calling serial interface methods via the MFC". None of which pertain to the free tools I can get today.

The problem: I have an embedded device (A model rocket altimeter of my own design based on a PIC microcontroller) I would like to "talk to" using my XP or Vista laptop. The only interface I have to the embedded controller is a Plain Jane serial port on the embedded controller, and either a hard wired port or a USB->Serial perverter on the laptop PC. I can interface to the embedded controller now using a terminal emulator application like TeraTerm. I would like to create a slick windows application to set flight parameters and download flight data.

The constraints:
  • I would like the ultimate interface to be using a window (vs. a console) interface under the aforementioned operating systems.
  • I am proficient in C and Java, and would like to use C++ as the development language of choice; I am not afraid of using Classes.
  • I would like to use the cool, glossy Visual Studio 2008 (Express Edition)



The Questions:
  • Is there a complete example of a simple application that will get a handle of a serial port, and use the associated class methods to read and write a character? A string? Validate the existance of the logical/physical device?
  • Is there a SerialPort Class (like in VB 2005) that will work out of the box for Visual Studio 2008 Express?


Any direction/hints/commiseration from informed individuals would be appreciated, even if it is an admonition to "RTFM" as long as the requisite "FM" is included in the rebuke.

Thanks,
David W.
Spacecoast, Florida

Is This A Good Question/Topic? 0
  • +

Replies To: Serial Port Control with Visual Studio 2008 and C++

#2 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 02 July 2009 - 03:21 PM

See the items under the "Programming" section at:

http://www.lookrs232...rt_programming/
Was This Post Helpful? 0
  • +
  • -

#3 dk-info  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-March 09

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 July 2009 - 04:46 AM

View Postdk-info, on 2 Jul, 2009 - 02:00 PM, said:

There are literally hundreds of postings here and elsewhere concerning the serial port and various tools and compilers, but none seem to describe how to access serial port resources under Windows (XP & Vista) using the free Microsoft Visual Studio Express 2008 tools and Visual C++.

I have "grepped" (The great grandfather of "Googled") the internet for just this information only to find old snippets of Borland code, Visual Basic 2005 and Visual C# examples. I have also seen vague references to "Calling serial interface methods via the MFC". None of which pertain to the free tools I can get today.

The problem: I have an embedded device (A model rocket altimeter of my own design based on a PIC microcontroller) I would like to "talk to" using my XP or Vista laptop. The only interface I have to the embedded controller is a Plain Jane serial port on the embedded controller, and either a hard wired port or a USB->Serial perverter on the laptop PC. I can interface to the embedded controller now using a terminal emulator application like TeraTerm. I would like to create a slick windows application to set flight parameters and download flight data.

The constraints:
  • I would like the ultimate interface to be using a window (vs. a console) interface under the aforementioned operating systems.
  • I am proficient in C and Java, and would like to use C++ as the development language of choice; I am not afraid of using Classes.
  • I would like to use the cool, glossy Visual Studio 2008 (Express Edition)


I failed to list in the constraints that I did not wish to buy a solution; to date the only comment I got was a commercial package. Am I to believe that NOBODY uses the serial port anymore? Isn't anybody using Visual Studio for anything more useful than "Hello World"?

Thanks,
David W.
Spacecoast, Florida
Was This Post Helpful? 0
  • +
  • -

#4 jeff666  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 193
  • Joined: 30-December 08

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 July 2009 - 05:50 AM

Just use I/O Win32 api (see Win32 ng for C samples (same ones for 16 years))
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 July 2009 - 08:13 AM

Indeed the windows API is really all you need for serial communication: MSDN Serial Communication.

There really is nothing particularly hard about using the API's. There are also some 3rd party libraries (open source I believe) that can map a USB device to COM ports allowing one to use the serial API to communicate with USB devices (not really sure how this works but it is how my cellphone's drivers work -- I have been thinking about trying to use this to get data off of my old digital camera).

You can get LOTS of example of using the serial port by just going to sourceforge.net and searching for "Serial Port Communications" -- plenty of open source examples.

Another way to research example is to use Google Code Search or Koders -- Use MSDN to get the names of functions used and then use Google CS/Koders to search for uses of that function.

On sourceforge you can also find lots of libraries that wrap the API and make it a little easier to use.
Was This Post Helpful? 0
  • +
  • -

#6 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 July 2009 - 11:21 AM

View Postdk-info, on 3 Jul, 2009 - 06:46 AM, said:

I failed to list in the constraints that I did not wish to buy a solution; to date the only comment I got was a commercial package. Am I to believe that NOBODY uses the serial port anymore? Isn't anybody using Visual Studio for anything more useful than "Hello World"?

Thanks,
David W.
Spacecoast, Florida


While the website that I linked to happens to sell a piece of serial port monitoring software, the programming section gives information on RS232 programming using the standard Windows API, in which the headers/libraries are included with Visual C++ 2008 Express.
Was This Post Helpful? 0
  • +
  • -

#7 avinashgadde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-July 09

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 17 August 2009 - 01:28 AM

Hi David

I also need to read the data of serial port and input it to microcontroller(ATMEGA 16) by using a C++ program.Do you have a fix for this ?I will be really thankful if you help me .
Was This Post Helpful? 0
  • +
  • -

#8 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 17 August 2009 - 01:46 AM

View Postavinashgadde, on 17 Aug, 2009 - 07:28 AM, said:

Hi David

I also need to read the data of serial port and input it to microcontroller(ATMEGA 16) by using a C++ program.Do you have a fix for this ?I will be really thankful if you help me .

have you seen
http://www.captain.a...serial-port.php
Was This Post Helpful? 0
  • +
  • -

#9 Aniss1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-October 09

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 October 2009 - 01:13 AM

Hi,

Just wondering..did you ever get this to work? I've been spending the last many hours trying to find out how to read a com port using Visual C++ 2008 Express Edition. It's a mess. I keep ending up with stuff that doesn't lead anywhere.

Does ANYONE know where I can find a simple example that actually COMPILES and WORKS using the mentioned compiler?

Or maybe just tell me which include file to use in order to use the .NET SerialPort Class described here?

msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx

It would be a HUGE help!!


Aniss1001
Was This Post Helpful? 0
  • +
  • -

#10 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 October 2009 - 01:30 AM

View PostAniss1001, on 3 Oct, 2009 - 07:13 AM, said:

Hi,

Just wondering..did you ever get this to work? I've been spending the last many hours trying to find out how to read a com port using Visual C++ 2008 Express Edition. It's a mess. I keep ending up with stuff that doesn't lead anywhere.

Does ANYONE know where I can find a simple example that actually COMPILES and WORKS using the mentioned compiler?

Aniss1001

have a look at
http://www.codeguru....ticle.php/c2503
Was This Post Helpful? 0
  • +
  • -

#11 Aniss1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-October 09

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 October 2009 - 02:09 AM

View Posthorace, on 3 Oct, 2009 - 12:30 AM, said:

View PostAniss1001, on 3 Oct, 2009 - 07:13 AM, said:

Hi,

Just wondering..did you ever get this to work? I've been spending the last many hours trying to find out how to read a com port using Visual C++ 2008 Express Edition. It's a mess. I keep ending up with stuff that doesn't lead anywhere.

Does ANYONE know where I can find a simple example that actually COMPILES and WORKS using the mentioned compiler?

Aniss1001

have a look at
http://www.codeguru....ticle.php/c2503



Thanks for the answer :) BUT...the example you gave me (like many others I've seen) uses the #include "stdafx.h" which isn't in part of VC++ Express. SO...I found out that I'd have to download and install something called Platform SDK. Then I downloadad 17 cab-files which don't seem to unpack as they should? When I try a DOS prompt pops up for a sec and nothing else...

I then found another download site but that doesn't work either. When I run the installer file it says: "the installation package could not be opened....".

I also tried compiling this example with no luck.

...Just to mention a few things I've tried. Usually I can figure out stuff myself but this time I'm truly STUCK :(

This post has been edited by Aniss1001: 03 October 2009 - 02:11 AM

Was This Post Helpful? 0
  • +
  • -

#12 slomobile  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 28-September 09

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 October 2009 - 09:17 AM

I hope this link is of some use. It is helping me understand a similar project using 8 bit AVRs and Visual C++ 2008 but Im not using the express edition.
MSDN serial ports
Not sure if its there in Express but I just started a VC++ CLR windows form application and dragged the serialPort1 object from the tool box. All the methods you need to set up the connection are in the properties window for the serialPort1 object, the above link explains methods to read from and write to the serial port. I don't have any working code written yet. I am a noob so it probably would be rough anyway. I would love to see any code you come up with.
Was This Post Helpful? 0
  • +
  • -

#13 Aniss1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-October 09

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 October 2009 - 05:55 PM

Thanks for your answer :)

But in the meantime I found out that in order to use the .NET SerialPort class it has to be a CLR app as you mention. I should have clarified that I'm making a DirectX app that communicates with a microcontroller. I've made something similar using the Processing language in a matter of seconds, but the graphics performance of Processing is horrible so I turned to DirectX/C++.

The microcontroller is intermittently sending bytes of 0's and 1's and the Processing code below prints out: 0 255 0 255 0.... as it should...

import processing.serial.*;

Serial port;
int val;

void setup() {
  port = new Serial(this, Serial.list()[1], 115200);
}

void draw() {
  val = port.read();
  println(val);
}


I've been trying to follow these instructions but it's ridiculously complicated and so far I've only managed to OPEN the com port and detect that SOMETHING is being sent. Occasionally I've been able to receive one or two bytes of CORRECT data but usually it's messed up (perhaps a sync issue?)

I tried varios ways of doing it (based on the instructions above)..here is the simple version:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <commdlg.h>
#include <windef.h>

#define READ_TIMEOUT 500

void main()
{
	TCHAR* currentuser = L"COM8";
	LPCWSTR *lstr = (LPCWSTR *)(&currentuser);
	HANDLE hComm;

	DWORD dwRead;
	OVERLAPPED osReader = {0};
   
	DWORD dwRes;

	hComm = CreateFile( 
		*lstr,  
		GENERIC_READ | GENERIC_WRITE,
		0,
		0,
		OPEN_EXISTING,
		FILE_FLAG_OVERLAPPED,
		0);

	if (hComm == INVALID_HANDLE_VALUE) printf("*Error opening port\n");
	else printf("*Port opened succesfully\n");

	osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	if (osReader.hEvent == NULL) printf("*Error creating overlapped event; abort\n");

	BYTE inByte = 1;

	for(int i=0; i<100; i++) {
		if(!ReadFile(hComm, &inByte, 1, &dwRead, &osReader))
			dwRes = WaitForSingleObject(osReader.hEvent, READ_TIMEOUT);

		printf("%02x\n", inByte);
	}

	CloseHandle(osReader.hEvent);

	//wait for user
	int a;
	scanf("%d", &a);
}


So any help is still more than welcome???

This post has been edited by Aniss1001: 03 October 2009 - 06:00 PM

Was This Post Helpful? 1

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 October 2009 - 06:00 PM

To make the serial port examples on MSDN work with an Express edition you may need to download the platform sdk... there is a work arround for getting the platform sdk for work with the express edition (I think there are just some batch files you need to run but I don't remember off the top of my head).

All I did (since I just use the compiler and not Visual Studio) was set my include and lib directories to that of the platform sdk...

Once you have done that even the express edition should compile the examples on MSDN (if it did not already). I find this all a bit baffeling though because I can compile most of those example on borland... with just the windows.h.
Was This Post Helpful? 0
  • +
  • -

#15 Aniss1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 03-October 09

Re: Serial Port Control with Visual Studio 2008 and C++

Posted 03 October 2009 - 07:48 PM

Well...I started off doing some research on the options and I found:

1) The .NET SerialPort class, which is a no go for me cause I'm making a DirectX app and NOT a CLR app.

2) The method I posted above which SHOULD work without the Platform SDK, BUT it's tricky..

3) The method you mention which seems fairly easy (from the code I've seen), BUT requires the Platform SDK. I started out trying to install this SDK but after HOURS wasted it seems to me that MS doesn't WANT you to use this with the free Express Edition. It simply doesn't work..

I started by downloading the files PSDK-FULL.1.cab to PSDK-FULL.17.cab and PSDK-FULL.exe from this page.

When I ran the PSDK-FULL.exe the program "IExpress Self-Extracting Cabinet" prompted "Please type the location where you want to place the extracted files". I did and the files Extract.exe and PSDK-FULL.bat were created in the specified folder.

When I run either Extract.exe or PSDK-FULL.bat a DOS prompt pops up for a split second and the closes, nothing else happens.

I've also tried downloading/running the file setup.exe (web install) from the same page. I get a missing file error message. I tried following the rather incoherent MS instructions. I tried searching forums. I tried unpacking the cab-files manually and THEN running the various setup files. I tried all sorts of stuff and after hours wasted I gave up and turned to the 2nd solution...Which works SOME of the way...but then not really...

I have been programming many years and I've NEVER tried anything this frustrating. I've wasted around 15 hours on NOTHING at this point :(

This post has been edited by Aniss1001: 03 October 2009 - 07:50 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2