Тема: Незрозуміла поведінка bool
Доброго всім здоров"я
Пишу на builder 10.4 community, треба реалізувати доступ до ком порта. Скопіпастив функції змережі, трохи відредагував
срр
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( wchar_t *szPort, int nBaud,int nDataBids,float nStopBits )
{
if( m_bOpened ) return( TRUE );
char szComParams[50];
DCB dcb;
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 );
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 = nDataBids;
dcb.StopBits = nStopBits;
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 );
}
.h
#include <windows.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( wchar_t *szPort, int nBaud = 9600,int nDataBids=8,float nStopBits=2 );
bool Close( void );
int ReadData( void *, int );
int SendData( const char *, int );
int ReadDataWaiting( void );
bool IsOpened( void ){ return( m_bOpened ); }
void GetComList(TStringList list);
struct ComPort
{
ComPort(LPSTR name, LPSTR key);
~ComPort();
LPSTR name;
LPSTR key;
};
protected:
bool WriteCommByte( unsigned char );
HANDLE m_hIDComDev;
OVERLAPPED m_OverlappedRead, m_OverlappedWrite;
bool m_bOpened;
};
#endif
та робота з ком портом , це подія. ComPort- обєкт CSerial , StopBitsCBox -комбобокс, LStatus- лейбл
void __fastcall TForm1::Button1Click(TObject *Sender)
{ ShowMessage(BoolToStr(ComPort.IsOpened));
if(!ComPort.IsOpened)
{
if(ComPort.Open(ComCBox->Selected->Text.c_str(),BaudCBox->Selected->Text.ToInt(),BitsCBox->Selected->Text.ToInt(),StopBitsCBox->Selected->Text.ToDouble()) )
{ ShowMessage(BoolToStr(ComPort.IsOpened));
LStatus->Text="CONNECTED";
LStatus->TextSettings->FontColor=TAlphaColor(claBlue);
} else{ShowMessage("Error Com don`t open");}
} else
{
ComPort.Close();
LStatus->Text="DISCONNECTED";
LStatus->TextSettings->FontColor=TAlphaColor(claChocolate);
}
}
Справа в тому, що в конструкторі класу IsOpened одразу ж ініціалізується FALSE але код не виконується, якщо прибрати ! , то все навпаки код виконується, але все одно у функції ComPort.Open не міняється значення чому?