-
Readfile() - Newbi help
Code:
#include <stdio.h>
#include <windows.h>
#include <pt_ioctl.c>
int __cdecl main(void)
{
char quit;
DWORD dwRead;
OVERLAPPED osReader = {0};
unsigned char value;
unsigned char inBuffer;
quit = '\0';
while (quit != 'q')
{
OpenPortTalk();
HANDLE hComm;
hComm = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
value = ReadFile(hComm, inBuffer, 12, &dwRead, &osReader);
ClosePortTalk();
}
return 0;
}
Well, any help on readfile. I want a quick fix, and my brain is hurting to the point i need to sleep, but i need to get this done.
Thanks.
-
>>any help on readfile. I want a quick fix
Ask a well thought out question, you'll get a well thought out answer. ;)
-
Haha funny.
All i need is to be able to use readfile to get the input from my device.
Easy eh??
So, any helpfull ideas which will help me...
-
Considering that it shouldn't even compile, let alone run, what were you expecting
> value = ReadFile(hComm, inBuffer, 12, &dwRead, &osReader);
1. check that hComm is valid, by actually checking the status return of the CreateFile()
2. the 2nd parameter is supposed to be a pointer to where you want the answer stored. This is not a pointer, you should be getting error messages
3. the 3rd parameter is the buffer length, and 12 most assuredly is not it's length.
char inBuffer[12];
value = ReadFile(hComm, inBuffer, sizeof(inBuffer), &dwRead, &osReader);
-
Also, since you are providing an overlapped structure, you should really wait for completion by allocating an event handle, storing that in the overlapped structure, and then WaitForSingleObject or similar BEFORE closing the port. Closing the port immediately after the read will simply cause the read operation to be cancelled as the version of ReadFile you are using is Asynchronous (ie. runs in the background and may return before anything has been done).