Solved

Serial Port Communication in Windows XP with C++

Posted on 2007-12-03
10
5,992 Views
Last Modified: 2012-06-21
I need to perform serial port communications in a program developed in C++.

The solutions should provide functions or methods to configure, open, read from, write to and close the serial port.

The computers I'm using run Windows XP. I'm using Visual C++ 6.0 for development, but, if possible, I'd like to have a generic solution that can be used with other C++ compilers.
0
Comment
Question by:gandhirs
  • 6
  • 4
10 Comments
 
LVL 7

Accepted Solution

by:
lucky_james earned 250 total points
ID: 20395313
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20395321
0
 

Author Comment

by:gandhirs
ID: 20395643
I tried the http://www.codeproject.com/system/SerialPortComm.asp

I downloaded the source code, but I don't know how to use it! The information on that page seems to explain how the class was implemented, but it doesn't explain how to use it.

I wrote this code but I receive an execution error: Debug error: DAMAGE: after Normal block (#46) at 0x004419F0.

I don't know what to put in the argument of the constructor of the class CSyncSerialComm

#include "SyncSerialComm.h"

void main()
{
      CSyncSerialComm serial("COM3");
      //serial.Open();
      //serial.Close();
}
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 7

Expert Comment

by:lucky_james
ID: 20397202
i am not able to access that site.....the sooner i will be able to access its code, i will be in a better position to tell you. I am facing some sort of connectivity problem. :(


>>I don't know what to put in the argument of the constructor of the class CSyncSerialComm

Meanwhile, you can look into the constructor and see where this argument is being used.


>>I wrote this code but I receive an execution error: Debug error: DAMAGE: after Normal block (#46) at 0x004419F0.

plz tell which statement is causing the error.
0
 

Author Comment

by:gandhirs
ID: 20397433
The code of the constructor seems to accept a string as argument:

CSyncSerialComm::CSyncSerialComm(const char *pszPortName)
      : m_hSerialComm(INVALID_HANDLE_VALUE)
{
      assert(pszPortName);

      m_pszPortName = new char[strlen(pszPortName)];
      strcpy(m_pszPortName, pszPortName);
}

The statement that causes the error is

CSyncSerialComm serial("COM3");

since the other statements are commented
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20398450
>>I wrote this code but I receive an execution error: Debug error: DAMAGE: after Normal block (#46) at 0x004419F0.

http://elliottback.com/wp/archives/2004/10/23/damage-after-normal-block/

It says this can be a buffer overflow.

Can you check under which code of the constructor is this failing....

regards,
James
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20398629
>>m_pszPortName = new char[strlen(pszPortName)];
would be better if :
m_pszPortName = new char[strlen(pszPortName) * sizeof(char)];

Hey, till now i was not able to  download the code from
http://www.codeproject.com/system/SerialPortComm.asp

but i saw the code at :
http://ontrak.net/mfc.htm

and it has the sample application code as well.

try to download the code from above link.(it is given on the link  "VISUAL C++ EXAMPLE" on the bottom of the page.)


Hope it helps.

James
0
 

Author Comment

by:gandhirs
ID: 20398813
m_pszPortName = new char[strlen(pszPortName) * sizeof(char)];

haven't eliminated the error. It makes sense since sizeof(char) is 1.

But you gave me an idea. I tried

m_pszPortName = new char[strlen(pszPortName) +1];

and the error disappeared
0
 

Author Comment

by:gandhirs
ID: 20398984
The code at :
http://ontrak.net/mfc.htm

is not adequate to my purposes because It uses MFC.
0
 
LVL 7

Expert Comment

by:lucky_james
ID: 20399043
gud, :)
do let me know if you face any problem......


regards,
James
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question