C++Builder how to test if COM port is free?

Status
Not open for further replies.

keen

Full Member level 2
Joined
Jul 18, 2001
Messages
123
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
881
insert button in a form modal c++ builder

Hi to all,

Just a simple question,
I am C++ Builder beginer, I have start trouble, please help me & advice..
I wanna test if COM port is free (I know do that) & if it is not open the
modal form & after that if confirm button was presed terminate application.
I have that :
if(!comport) {
NoComForm->ShowModal();
Application->terminate();
}
else // { the rest of appli. }

Not works, this case is driven by event !comport & its not member of
other Builders events I think .. who now? Please how to fix this problem ..


best regards KEEN
 

comstat builder c++

what is comport?
is it a boolean you defined???

opening a comport is like opening a file in C++
When you don't get the handle, it ain't there or it is already opened
 

c builder return from modal form

Hi,
Are you using an external component or are you trying to open comport like a file? If you want to test a comport availability, try to open it and it open function returns null you can not access it.If you want to use 3rd party comport component, try to use cport available at torry.net

Analyzer.
 

builder c++ waitforsingleobject

Hi to all,
comport is PC UART.
The target what I want to do is: open modal form if comport
is not present. On modal form is than the button, & if I press it, than
this terminate tha application, thats all waht I want to do in first turn.
I wrote the code but not work in run time. How I have to handle this job ?


regards Keen
 

c++builder

Hi to all,
comport is PC UART.
The target what I want to do is: open modal form if comport
is not present. On modal form is than the button, & if I press it, than
this terminate tha application, thats all waht I want to do in first turn.
I wrote the code but not work in run time. How I have to handle this job ?


regards Keen
 

There are useful routines below.Check it :

#include "stdafx.h"
#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();

}

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 );

}
 

C++ Builder

Analizer,
Thank you for yours help,
Well its not the problem to handl with the com port,
the major problem is as I told, my program was 'hang'
after detect that com2 port is not free .. I am doing something wrong
lets see ..

regards Keen
 

solve

hi )

I founded, I checked internal state(Com2Port) & If is not free start
the timer, on timeout event I can ShowModal form ..

tnx br Keen
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…