Solved

Serial Port Communication in Windows XP with C++

Posted on 2007-12-03
10
5,985 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now