HELP!

B

bex

what is wrong with this, i'm stumped and gettin nowhere :-(

CPP FILE

// Serial.cpp

#include <stdio>
#include <stdexcept>
#include "Serial.h"


CSerial::CSerial()
{

memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
m_hIDComDev = NULL;
m_bOpened = false;

}

CSerial::~CSerial()
{

Close(void);

}

BOOL CSerial::Open( int nPort, int nBaud )
{

if( m_bOpened ) return( true );

char szPort[15];
char szComParams[50];
DCB dcb;

wsprintf( szPort, "COM%d", nPort );
m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
if( m_hIDComDev == NULL ) return( false );

memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 5000;
SetCommTimeouts( m_hIDComDev, &CommTimeOuts );

wsprintf( szComParams, "COM%d:%d,n,8,1", nPort, nBaud );

m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

dcb.DCBlength = sizeof( DCB );
GetCommState( m_hIDComDev, &dcb );
dcb.BaudRate = nBaud;
dcb.ByteSize = 8;
unsigned char ucSet;
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
if( !SetCommState( m_hIDComDev, &dcb ) ||
!SetupComm( m_hIDComDev, 10000, 10000 ) ||
m_OverlappedRead.hEvent == NULL ||
m_OverlappedWrite.hEvent == NULL ){
DWORD dwError = GetLastError();
if( m_OverlappedRead.hEvent != NULL ) CloseHandle(
m_OverlappedRead.hEvent );
if( m_OverlappedWrite.hEvent != NULL ) CloseHandle(
m_OverlappedWrite.hEvent );
CloseHandle( m_hIDComDev );
return( false );
}

m_bOpened = TRUE;

return( m_bOpened );

}

BOOL CSerial::Close( void )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );

if( m_OverlappedRead.hEvent != NULL ) CloseHandle(
m_OverlappedRead.hEvent );
if( m_OverlappedWrite.hEvent != NULL ) CloseHandle(
m_OverlappedWrite.hEvent );
CloseHandle( m_hIDComDev );
m_bOpened = FALSE;
m_hIDComDev = NULL;

return( true );

}

BOOL CSerial::WriteCommByte( unsigned char ucByte )
{
BOOL bWriteStat;
DWORD dwBytesWritten;

bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten,
&m_OverlappedWrite );
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) ) dwBytesWritten
= 0;
else{
GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten,
FALSE );
m_OverlappedWrite.Offset += dwBytesWritten;
}
}

return( true );

}

int CSerial::SendData( const char *buffer, int size )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwBytesWritten = 0;
int i;
for( i=0; i<size; i++ ){
WriteCommByte( buffer );
dwBytesWritten++;
}

return( (int) dwBytesWritten );

}

int CSerial::ReadDataWaiting( void )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );

return( (int) ComStat.cbInQue );

}

int CSerial::ReadData( void *buffer, int limit )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) return( 0 );

dwBytesRead = (DWORD) ComStat.cbInQue;
if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead,
&m_OverlappedRead );
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
}

return( (int) dwBytesRead );

}

HEADER FILE

// Serial.h

#ifndef __SERIAL_H__
#define __SERIAL_H__

#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04
#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13

class CSerial
{

public:
CSerial();
~CSerial();

BOOL (Open( int nPort, int nBaud));
BOOL (Close( void ));

int ReadData( void *, int );
int SendData( const char *, int );
int ReadDataWaiting( void );

BOOL (IsOpened( void ){return( m_bOpened )});

protected:
BOOL (WriteCommByte( unsigned char ));

HANDLE (m_hIDComDev);
OVERLAPPED ((m_OverlappedRead)(m_OverlappedWrite));
BOOL (m_bOpened);

};

#endif
 
D

David

bex,

Could you give us a hint as to what you think is wrong? What problem
are you encountering?

David

what is wrong with this, i'm stumped and gettin nowhere :-(

CPP FILE

// Serial.cpp

#include <stdio>
#include <stdexcept>
#include "Serial.h"


CSerial::CSerial()
{

memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );
m_hIDComDev = NULL;
m_bOpened = false;

}

CSerial::~CSerial()
{

Close(void);

}

BOOL CSerial::Open( int nPort, int nBaud )
{

if( m_bOpened ) return( true );

char szPort[15];
char szComParams[50];
DCB dcb;

wsprintf( szPort, "COM%d", nPort );
m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL );
if( m_hIDComDev == NULL ) return( false );

memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 5000;
SetCommTimeouts( m_hIDComDev, &CommTimeOuts );

wsprintf( szComParams, "COM%d:%d,n,8,1", nPort, nBaud );

m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

dcb.DCBlength = sizeof( DCB );
GetCommState( m_hIDComDev, &dcb );
dcb.BaudRate = nBaud;
dcb.ByteSize = 8;
unsigned char ucSet;
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 );
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 );
ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 );
if( !SetCommState( m_hIDComDev, &dcb ) ||
!SetupComm( m_hIDComDev, 10000, 10000 ) ||
m_OverlappedRead.hEvent == NULL ||
m_OverlappedWrite.hEvent == NULL ){
DWORD dwError = GetLastError();
if( m_OverlappedRead.hEvent != NULL ) CloseHandle(
m_OverlappedRead.hEvent );
if( m_OverlappedWrite.hEvent != NULL ) CloseHandle(
m_OverlappedWrite.hEvent );
CloseHandle( m_hIDComDev );
return( false );
}

m_bOpened = TRUE;

return( m_bOpened );

}

BOOL CSerial::Close( void )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE );

if( m_OverlappedRead.hEvent != NULL ) CloseHandle(
m_OverlappedRead.hEvent );
if( m_OverlappedWrite.hEvent != NULL ) CloseHandle(
m_OverlappedWrite.hEvent );
CloseHandle( m_hIDComDev );
m_bOpened = FALSE;
m_hIDComDev = NULL;

return( true );

}

BOOL CSerial::WriteCommByte( unsigned char ucByte )
{
BOOL bWriteStat;
DWORD dwBytesWritten;

bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten,
&m_OverlappedWrite );
if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){
if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) ) dwBytesWritten
= 0;
else{
GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten,
FALSE );
m_OverlappedWrite.Offset += dwBytesWritten;
}
}

return( true );

}

int CSerial::SendData( const char *buffer, int size )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwBytesWritten = 0;
int i;
for( i=0; i<size; i++ ){
WriteCommByte( buffer );
dwBytesWritten++;
}

return( (int) dwBytesWritten );

}

int CSerial::ReadDataWaiting( void )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

DWORD dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );

return( (int) ComStat.cbInQue );

}

int CSerial::ReadData( void *buffer, int limit )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) return( 0 );

dwBytesRead = (DWORD) ComStat.cbInQue;
if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead,
&m_OverlappedRead );
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
}

return( (int) dwBytesRead );

}

HEADER FILE

// Serial.h

#ifndef __SERIAL_H__
#define __SERIAL_H__

#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04
#define ASCII_BEL 0x07
#define ASCII_BS 0x08
#define ASCII_LF 0x0A
#define ASCII_CR 0x0D
#define ASCII_XON 0x11
#define ASCII_XOFF 0x13

class CSerial
{

public:
CSerial();
~CSerial();

BOOL (Open( int nPort, int nBaud));
BOOL (Close( void ));

int ReadData( void *, int );
int SendData( const char *, int );
int ReadDataWaiting( void );

BOOL (IsOpened( void ){return( m_bOpened )});

protected:
BOOL (WriteCommByte( unsigned char ));

HANDLE (m_hIDComDev);
OVERLAPPED ((m_OverlappedRead)(m_OverlappedWrite));
BOOL (m_bOpened);

};

#endif
 
J

John Harrison

David said:
bex,

Could you give us a hint as to what you think is wrong? What problem
are you encountering?

David

C'mon David, its obvious what's wrong, it doesn't work.

john
 
B

Buster

bex said:
what is wrong with this, i'm stumped and gettin nowhere :-(

You can do better than that. Read the welcome message and the posting
guidelines in the FAQ. Search the web for "C++ welcome message" and
"C++ FAQ". There's also an article about asking good questions on usenet
which you can find in the same way (it is by Eric Steven Raymond).
 
J

JKop

As implied in your post, I pressume that when you hit "compile",
custard is oozing out of your printer. This is most likely a problem
with Microsoft Windows, not specifically with your code.

If you have any other problems with your code, please feel free to
elaborate on them. Two kinds of problem I predict:


A) Compile/Link error. There's an error in your code, eg. syntax
error, type mismatch. You're not even getting an executable file.

B) You are getting an executable file, but your calls to the Win32API
aren't working as you would like.


In the case of A: People here will be glad to help you if you tell us
what errors you're getting.


In the case of B: Connect to the news server, "msnews.microsoft.com",
and go to the newsgroup "microsoft.public.win32.programmer.kernel",
where experts will be more than happy to help.



-JKop
 
T

Thomas Matthews

myName said:
?? What's wrong where ? u didn't mention what problem u r facing ..
Y du u not shrtn al the words?
"Mention" s bigr den "you" or "are".

I am really curious why these people can't take the time to
write in the full written language. Why do they abbreviate
the small words but not the longer ones? I thought that
abbreviations were to make the longer words short.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top