Click here to Skip to main content
15,885,985 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
SuggestionRe: GetModuleFileName and case sensitivity Pin
Richard MacCutchan24-Jul-13 4:56
mveRichard MacCutchan24-Jul-13 4:56 
GeneralRe: GetModuleFileName and case sensitivity Pin
vikramlinux24-Jul-13 16:56
vikramlinux24-Jul-13 16:56 
GeneralRe: GetModuleFileName and case sensitivity Pin
Richard MacCutchan24-Jul-13 21:13
mveRichard MacCutchan24-Jul-13 21:13 
Questioncalculated as 0 and 1 in a matrix Pin
yahya7723-Jul-13 8:06
yahya7723-Jul-13 8:06 
QuestionRe: calculated as 0 and 1 in a matrix Pin
David Crow23-Jul-13 9:08
David Crow23-Jul-13 9:08 
AnswerRe: calculated as 0 and 1 in a matrix Pin
Alan Balkany25-Jul-13 4:23
Alan Balkany25-Jul-13 4:23 
GeneralRe: calculated as 0 and 1 in a matrix Pin
yahya7725-Jul-13 5:50
yahya7725-Jul-13 5:50 
QuestionSerial Port OK in class constructor but not outside... Pin
Mike Grove23-Jul-13 5:49
Mike Grove23-Jul-13 5:49 
I am opening a COM port in a class constructor - no problem.

I can send some data at this point too - no problem.

BUT, if I try to send data from any other method in the class it appears to work ok (I get the expected ERROR_IO_PENDING), but subsequently my code suffers an unpredictable and undebuggable crash. Any ideas anyone (please ask for more info' if needed)...

Some code;
C++
CSerialDevice::CSerialDevice(
  CString& strDeviceId,
  CString& strDeviceName,
  CString& COMPort,
  CString& BaudRate,
  CString& Format,
  ComWrapper_c<CConnection15> Db, GobList_t& rGobList,
  CSerialIOInterface* pInterface )
	: pOurInterface( pInterface ),
	  m_hCommPort( NULL )
{
	// Create the items for the device
	AddDigitalItems( strDeviceName, strDeviceId, Db, rGobList );
	AddAnalogueItems( strDeviceName, strDeviceId, Db, rGobList );

	// Configure the COM port for the device
	if ( !ConfigureSerialPort( COMPort, BaudRate, Format ) )
	{
		pOurInterface->AddErrorToStatus( "Failed to configure port for " + strDeviceName );
	}
}


/////////////////////////////////////////////////////////////////////////////
//
//
//
/////////////////////////////////////////////////////////////////////////////
bool CSerialDevice::ConfigureSerialPort(
  CString& strCOMPort,
  CString& strBaudRate,
  CString& strFormat )
{
	// Open the port
	m_hCommPort = CreateFile(
                      strCOMPort,
                      GENERIC_READ | GENERIC_WRITE,
                      0,
                      0,
                      OPEN_EXISTING,
                      FILE_FLAG_OVERLAPPED,
                      0 );

	// Get the default configuration...
	DCB dcb = {0};
	dcb.DCBlength = sizeof(DCB);
	if ( !GetCommState( m_hCommPort, &dcb ) )
	{
		return false;
	}

	// ...and change the items we want to
	dcb.BaudRate = atoi( strBaudRate );
	dcb.ByteSize = atoi( strFormat.Left( 1 ) );
	int parity = NOPARITY;
	if ( strFormat.Mid( 1 ) == 'N' )
	{
		parity = NOPARITY;
	}
	else if ( strFormat.Mid( 1 ) == 'E' )
	{
		parity = EVENPARITY;
	}
	else if ( strFormat.Mid( 1 ) == 'O' )
	{
		parity = ODDPARITY;
	}
	dcb.Parity = parity;
	int stopBits = atoi( strFormat.Right( 1 ) );
	switch ( stopBits )
	{
	case 0  : stopBits = ONESTOPBIT; break;
	case 2  : stopBits = TWOSTOPBITS; break;
	default : stopBits = ONESTOPBIT; break;
	}
	dcb.StopBits = stopBits;
	if ( !SetCommState( m_hCommPort, &dcb ) )
	{
		return false;
	}

	COMMTIMEOUTS timeouts;
	timeouts.ReadIntervalTimeout			= MAXDWORD;
	timeouts.ReadTotalTimeoutMultiplier		= 0;
	timeouts.ReadTotalTimeoutConstant		= 0;
	timeouts.WriteTotalTimeoutMultiplier	= 0;
	timeouts.WriteTotalTimeoutConstant		= 0;

	if ( !SetCommTimeouts( m_hCommPort, &timeouts ) )
	{
		return false;
	}

	// TEST CODE - THIS WORKS /////////////////
	Sleep( 100 );
	int loops = 40;
	while ( loops-- )
	{
	  OVERLAPPED ovlWrite = {0};
	  WriteFile( m_hCommPort, mTxBuf, 7, NULL, &ovlWrite ); // This Works
	  Sleep( 100 );
	}
	// TEST CODE - THIS WORKS /////////////////

	return true;
}
/////////////////////////////////////////////////////////////////////////////
//
// THIS GETS CALLED EVERY 100ms AND WriteFile() RETURNS
// ERROR_IO_PENDING OK. BUT 'SOME' TIME AFTER THE FIRST CALL (FEW
// SECONDS) I GET A RANDOM CRASH THAT I CAN'T TRACE - LIKE
// SERIOUS MEMORY CORRUPTION OR SOMMAT???
//
/////////////////////////////////////////////////////////////////////////////
void CSerialDevice::TransmitData( int bytesToSend )
{
	OVERLAPPED ovlWrite = {0};
	WriteFile( m_hCommPort, mTxBuf, bytesToSend, NULL, &ovlWrite );
}

AnswerRe: Serial Port OK in class constructor but not outside... Pin
Jochen Arndt23-Jul-13 6:23
professionalJochen Arndt23-Jul-13 6:23 
GeneralRe: Serial Port OK in class constructor but not outside... Pin
Mike Grove23-Jul-13 22:11
Mike Grove23-Jul-13 22:11 
AnswerRe: Serial Port OK in class constructor but not outside... Pin
etkid8423-Jul-13 8:53
etkid8423-Jul-13 8:53 
GeneralRe: Serial Port OK in class constructor but not outside... Pin
Mike Grove23-Jul-13 22:13
Mike Grove23-Jul-13 22:13 
AnswerRe: Serial Port OK in class constructor but not outside... Pin
pasztorpisti23-Jul-13 9:16
pasztorpisti23-Jul-13 9:16 
GeneralRe: Serial Port OK in class constructor but not outside... Pin
Richard Andrew x6423-Jul-13 13:31
professionalRichard Andrew x6423-Jul-13 13:31 
GeneralRe: Serial Port OK in class constructor but not outside... Pin
pasztorpisti24-Jul-13 1:03
pasztorpisti24-Jul-13 1:03 
GeneralRe: Serial Port OK in class constructor but not outside... Pin
Richard Andrew x6424-Jul-13 14:03
professionalRichard Andrew x6424-Jul-13 14:03 
GeneralRe: Serial Port OK in class constructor but not outside... Pin
pasztorpisti24-Jul-13 14:17
pasztorpisti24-Jul-13 14:17 
QuestionInitilazation error in MFC C++ App Pin
ForNow23-Jul-13 5:16
ForNow23-Jul-13 5:16 
SuggestionRe: Initilazation error in MFC C++ App Pin
Richard MacCutchan23-Jul-13 5:31
mveRichard MacCutchan23-Jul-13 5:31 
GeneralRe: Initilazation error in MFC C++ App Pin
ForNow23-Jul-13 6:30
ForNow23-Jul-13 6:30 
GeneralRe: Initilazation error in MFC C++ App Pin
Richard MacCutchan23-Jul-13 6:48
mveRichard MacCutchan23-Jul-13 6:48 
GeneralRe: Initilazation error in MFC C++ App Pin
ForNow23-Jul-13 8:22
ForNow23-Jul-13 8:22 
GeneralRe: Initilazation error in MFC C++ App Pin
David Crow23-Jul-13 9:11
David Crow23-Jul-13 9:11 
GeneralRe: Initilazation error in MFC C++ App Pin
ForNow26-Jul-13 5:16
ForNow26-Jul-13 5:16 
GeneralRe: Initilazation error in MFC C++ App Pin
ForNow23-Jul-13 6:32
ForNow23-Jul-13 6:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.