Ok, getting better I think and I am trying to put more error checking to help find my issue.
Now I appear to be opening the port ok, creating an even handler for the open port reference, but I am getting an error that I can not read data from the port.
again I downloaded a sample program from the internet that does work and read the device I am trying to use for testing, so I know it has to be something my code is doing wrong.
Are there some other errors I can check for??? I posted my revised code with the additional code provide to me. I am still trying to understand some of the pointer references and how to initialize the variables to use when calling the ReadFile() function, I have posted my code and the variable declarations, in hopes that someone will see my goof, slap me around a bit and hopefully be able to show me the error of my ways. Thanks again for all the help-
OS: WindowsXP
Compiler: MS Visual Studio 6.0 C++
Code:
// rs232test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "rs232test.h"
#include <string>
#include <cstring>
#include <stdlib.h>
#include <afx.h>
#include <windows.h>
#include <stdio.h>
const unsigned short MAX_MESSAGE = 100;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE hComm;
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
//define functions for port settings and reading port
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
DCB dcb;
char gszPort [5] = "COM1";
int iLen;
BYTE Data;
DWORD dwRead;
DWORD dwScanTime = 46;
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osReader = {0};
//notify user of opening port
cout<<"opening port:COM1, Baud: 9600, Parity: N, Bytes: 8, StopBit: 1"<<endl;
hComm = CreateFile( gszPort,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (hComm == INVALID_HANDLE_VALUE)
FillMemory(&dcb, sizeof(dcb), 0);
dcb.DCBlength = sizeof(dcb);
if (!BuildCommDCB("9600,n,8,1", &dcb))
{
// Couldn't build the DCB. Usually a problem
// with the communications specification string.
cout<<"could not open port"<<endl;
return FALSE;
}
else
{
cout<<"Port opened and configured successfully"<<endl;
// DCB is ready for use.
// Create the overlapped event. Must be closed before exiting
// to avoid a handle leak.
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osReader.hEvent == NULL)
{
// Error creating overlapped event; abort.
cout <<"ERROR: creating overlapped event failed, aborting"<<endl;
return 0;
}
if (!fWaitingOnRead)
{
// Issue read operation.
cout<<"inside read operation"<<endl;
memset(&osReader,0,sizeof(osReader));
osReader.hEvent = CreateEvent(0,true,false,0);
if(osReader.hEvent == 0)
{
cout<<"Failed to create event object"<<endl;
return 1;
}
else
{
cout<<"Event Handle appears to be created successfully"<<endl;
// Read the data
if (!ReadFile(hComm,&Data,iLen,&dwRead,&osReader))
{
// Set the internal error code
long lLastError = GetLastError();
// Overlapped operation in progress is not an actual error
if (lLastError != ERROR_IO_PENDING)
{
cout<<"Unable to read data from com-port"<<endl;
cout<<"GestLastError Code = "<<lLastError<<endl;
return 1;
}
}
else
{
// Wait for the overlapped operation to complete
switch (WaitForSingleObject(osReader.hEvent,INFINITE))
{
case WAIT_OBJECT_0:
// The overlapped operation has completed
if (!GetOverlappedResult(hComm,&osReader,&dwRead,FALSE))
{
cout<<"Overlapped completed without result"<<endl;
}
break;
case WAIT_TIMEOUT:
// Cancel the I/O operation
CancelIo(hComm);
cout<<"Overlapped operation timed out"<<endl;
break;
default:
// Set the internal error code
cout<<"Unable to wait for read operation to complete";
}
}
}
}
}
CloseHandle(hComm);
// error opening port; abort
return nRetCode;
}
below is a list of the output from my program that I am using to try and found my issue:
Code:
opening port:COM1, Baud: 9600, Parity: N, Bytes: 8, StopBit: 1
Port opened and configured successfully
inside read operation
Event Handle appears to be created successfully
Unable to read data from com-port
GestLastError Code = 998
Press any key to continue