Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serial Communications 1

Status
Not open for further replies.

Denyson

Programmer
Mar 20, 2001
19
BR
Hi,

I´m using Visual C++ 6.0 and windows 98.
I´m trying to write and read data by Serial port (COM 1).
The problem is: when I reset or turn on my computer the following code doesn´t works. It comes back to work only If I start a connection by HiperTerminal. It´s not necessary to keep the connection opened, I just have to start and closed right after for my program begins to run correctly.
The code is:

hCom = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING,FILE_FLAG_OVERLAPPED,
NULL);
ret = WriteFile(hCom, Buffer, BufferSize,
&NumBytes, Struct);

CloseHandle(hCom);

Anybody could help me?
Thanks
 
I have had similar experience with HyperTerminal affecting the com ports, but usually rather negatively; like I can't talk to com port again after HyperTerminal been in there and I have to reboot. When I saw you code sample here the first thing I notice was that you didn't mention where you were calling "SetCommState" to setup the DCB (DTE, parity, stop bits, flow control, etc). If you're not calling "SetCommState" then you should.

The next thing I noticed was the "WriteFile" line passes "Struct" (which is an OVERLAPPED structure) as required because you've opened the COM port using FILE_FLAG_OVERLAPPED. Make sure your passed the address of the "Struct" (i.e. "&Struct") and are not just pushing the whole "Struct" onto the stack.

Looking at some of my own code I noticed when I open the COM port the dwFlagsAndAttributes parameter is set to "FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED", not just "FILE_FLAG_OVERLAPPED". Don't ask me why I do this, all I can say is its either a mistake or something I discovered sometime ago and have forgotten about. If it is a mistake it won't make any difference as "FILE_ATTRIBUTE_NORMAL" will be ignored. If I'm not mistaken then its probably a tip I picked up from CodeGuru or somewhere that sort out a problem I was trying to solve at the time. Anyway I can't remember but its worth trying to see if helps.

Based on what you've said this is all I can come up with. One thing is for sure, the "SetCommState" call needs to be in there.

Let me know if this helps or what the affect these suggested changes have. If you can provide more details of the problems I may be able to come up with something more solid.

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top