Solved

Serial Port Communication in Windows XP with C++

Posted on 2007-12-03
10
5,993 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

735 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