Solved

Serial Comm. in VC++4.0 ie ReadFile etc..

Posted on 1997-12-31
1
509 Views
Last Modified: 2008-02-01
When writing to and reading from COM2
using GetComm...SetComm...WriteFile...
ReadFile and so on, my ReadFile func.
is returning error #87. Although it's
not a guarantee, I am assuming this is
an invalid parameter error(Error Codes
in on-line help). The following is my
handle initialization:
hCom=CreateFile("COM2",GENERIC_READ |
GENERIC_WRITE,0,NULL,OPEN_EXISTING,
0,NULL); I can write to the port with
no problem, I know my CommState is
correct. My error is a run-time error
when the ReadFile is attempted. My read
statement is: BOOL rSuccess = ReadFile(hCom,
&data,nBytesToRead,&nBytesRead,NULL).
&data is LPVOID, nB..T..R is DWORD,
&nB..R is LPDWORD, and I am not using
OVERLAPPED operations.
What am I doing wrong??????? As a test
I continuously send one byte 0xff of data
until I receive that data back from another
program which is monitoring COM2. I have
another PC that is watching this
communication and displaying the data that
is sent and received. This PC can see the
0xff sent and 0xff received, but the ReadFile
function is not reading the 0xff. I am not
using any timeout functions. The write func
ends after 1024 attempts for a successful
read.
Is there any other Communications functions
that may be better for me to use? Eventually
I will be using the WaitCommEvent function
to monitor for interrupt messages. Right now
I really need to know why the ReadFile is
not working properly.
0
Comment
Question by:jrmcg
1 Comment
 
LVL 23

Accepted Solution

by:
chensu earned 150 total points
ID: 1177301
Try this:

BYTE data[8];
DWORD dwBytesRead;
BOOL rSuccess = ReadFile(hCom, data, 1, &dwBytesRead, NULL);


/*lpNumberOfBytesRead

Points to the number of bytes read. ReadFile sets this value to zero before doing any work or error checking.*/

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

13 Experts available now in Live!

Get 1:1 Help Now