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!

Reading COM port data 2

Status
Not open for further replies.

prangster

MIS
Sep 14, 1999
18
US
Can someone please get me going on reading data from a COM port using VFP v7. I am running Windows XP and have not figured out how to read data being sent to a serial port. I can send data okay.

Thanks
 
I am sorry, I have no answer at all.
But can I ask, how do you send a data?
 
Hello All, I am trying to read data from a serial port in C using Win32 API. I created a thread from the main program which is an NT service and opened the port. Everything is working fine till opening the port and now I tried to read the data from COM port using ReadFile() in overlapped mode. The ReadFile always fails, I can see that the characters are arriving as I used WaitCommEvent() and WaitforSingleObject() to wait till a character arrives. Can anyone help me with this. I am pasting my function below......

DWORD CommThreadProc( LPDWORD param )
{

OVERLAPPED comReader = { 0 };
BOOL fWaitingOnRead = FALSE;
char * lpBuf = "";
DWORD dwRead = 0; // bytes actually read
BOOL Success;
FILE* FilePtr;
DCB dcb = {0};
DWORD dwCommEvent;
COMMTIMEOUTS CommTimeOuts;

printf(" Inside CommThreadProc \n" );

hCommPort = CreateFile( "COM1",
GENERIC_READ,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0 );

if( hCommPort == INVALID_HANDLE_VALUE )
{
printf( "Error while Opening COM Port \n" );
return 0;
}

GetCommState( hCommPort, &dcb );

dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;

SetCommState( hCommPort, &dcb );


//comReader.Offset = 1;
comReader.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );


SetCommMask( hCommPort, EV_RXCHAR );

BOOL repeat = true;

/*GetCommTimeouts( hCommPort, &CommTimeOuts );

CommTimeOuts.ReadIntervalTimeout = 3435973836;

SetCommTimeouts( hCommPort, &CommTimeOuts );*/

while( 1 )
{
WaitCommEvent( hCommPort, &dwCommEvent, &comReader );

//if( dwCommEvent )
//printf( "Event Recived is", dwCommEvent );

if( WaitForSingleObject( comReader.hEvent, INFINITE ) == WAIT_OBJECT_0 )
{

do
{
Success = ReadFile( hCommPort, lpBuf, 512, &dwRead, &comReader );

if( !Success )
if (GetLastError() == ERROR_IO_PENDING) // read not delayed?
printf( "Error in reading the file \n");
else
{
FilePtr = fopen( gszFilePath, "a" );

if( FilePtr != NULL )
{
fwrite( &lpBuf, dwRead, 512, FilePtr );
}
else
printf( "Error while opening file" );

fclose( FilePtr );
}
}while( dwRead > 0 );

}
}

CloseHandle( comReader.hEvent );
CloseHandle( hCommPort );


return 1;
}
 
I am trying to read data from a serial port in C using Win32 API.
This a FoxPro forum, you might a better response in forum713.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top