Join 300,488 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,811 people online right now. Registration is fast and FREE... Join Now!
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.
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"?
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.
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.
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 .
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 .
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?
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?
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?
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 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.
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...
CODE
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:
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.
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: 3 Oct, 2009 - 06:50 PM
OK! I now managed to INSTALL the Platform SDK. I also added the the bin-, lib-, include-, include/mfc-, include/atl- and include/crt- folders of the platform SDK to Visual C++ via: Tools->Options->Projects and solutions->VC++ Directories.
However I STILL got the "Cannot open include file: 'stdafx.h': No such file or directory"-error when I try to compile this CSerial Class. I then did a search for the file "stdafx.h" and the Platform SDK folder is full of them, BUT they're all different? I thought this was a standard file?
Any tips regarding the next step? How do I know WHICH version of "stdafx.h" is suitable for this class?
This post has been edited by Aniss1001: 3 Oct, 2009 - 09:19 PM
You may be on to something when you said synch issue. Especially since you are occasionally able to get correct data. What baudrate are you using? Main clock frequency on the AVR? What type of crystal are you using? The internal RC oscillator is rarely accurate enough for reliable uart operation without external calibration.