Article View: borland.public.cppbuilder.nativeapi
Article #3475Comm Port kicking my butt
From: JD
Date: Sat, 17 Jun 2006 18:40
Date: Sat, 17 Jun 2006 18:40
89 lines
3894 bytes
3894 bytes
I have absolutely no clue why I can't get this simple test to
work. All it does is open a port, and write to it and read from
it.
The write seems to work because no errors are reported and the
number of bytes written are correct. The read, OTOH reports
nada - no errors and no data.
//--------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
hCom = ::CreateFile( TEXT("COM1"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
if( hCom == INVALID_HANDLE_VALUE ) throw ComException( "Open Failed", ::GetLastError() );
FDCB.DCBlength = sizeof( DCB );
if( ! ::GetCommState(hCom, &FDCB) )
{
::CloseHandle( hCom );
hCom = INVALID_HANDLE_VALUE;
throw ComException( "Get State Failed", ::GetLastError() );
}
FDCB.BaudRate = 9600;
FDCB.ByteSize = 8;
FDCB.Parity = NOPARITY;
FDCB.StopBits = ONESTOPBIT;
if( ! ::SetCommState(hCom, &FDCB) )
{
::CloseHandle( hCom );
hCom = INVALID_HANDLE_VALUE;
throw ComException( "Set State Failed", ::GetLastError() );
}
if( ! ::SetupComm(hCom, 1024*32, 1024*9) )
{
::CloseHandle( hCom );
hCom = INVALID_HANDLE_VALUE;
throw ComException( "Setup Failed", ::GetLastError() );
}
FTimeOuts.ReadIntervalTimeout = 15;
FTimeOuts.ReadTotalTimeoutMultiplier = 1;
FTimeOuts.ReadTotalTimeoutConstant = 250;
FTimeOuts.WriteTotalTimeoutMultiplier = 1;
FTimeOuts.WriteTotalTimeoutConstant = 250;
if( ! ::SetCommTimeouts(hCom, &FTimeOuts) )
{
::CloseHandle( hCom );
hCom = INVALID_HANDLE_VALUE;
throw ComException( "Set Timeouts Failed", ::GetLastError() );
}
}
//--------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
if( hCom != INVALID_HANDLE_VALUE ) ::CloseHandle( hCom );
}
//--------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if( hCom != INVALID_HANDLE_VALUE )
{
DWORD nBytes;
AnsiString c = "Test String";
if( ! ::WriteFile( hCom, c.c_str(), c.Length(), &nBytes, NULL) )
{
throw ComException( "Write Failed " + SysErrorMessage(::GetLastError()), ::GetLastError() );
}
ShowMessage("Write " + IntToStr(nBytes) );
}
}
//--------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if( hCom != INVALID_HANDLE_VALUE )
{
DWORD nBytes = 0;
char Buffer[ 101 ] = { 0 };
if( ! ::ReadFile(hCom, &Buffer, 100, &nBytes, NULL) )
{
throw ComException( "Read Failed " + SysErrorMessage(::GetLastError()), ::GetLastError() );
}
else ShowMessage( IntToStr(nBytes) + " " + Buffer );
}
}
//--------------------------------------------------------------
~ JD
Technical Details
Message-ID:
<44944ca9$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,5117e51131b9ac21,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "JD" <nospam@nospam.com> Sender: "JD" <nospam@nospam.com> Reply-To: "JD" <nospam@nospam.com> Subject: Comm Port kicking my butt Newsgroups: borland.public.cppbuilder.nativeapi X-User-Info: 205.188.116.136 X-Forwarded-For: 172.128.228.215 NNTP-Posting-Host: 207.105.83.66 Message-ID: <44944ca9$1@newsgroups.borland.com> Date: 17 Jun 2006 11:40:41 -0700 X-Trace: newsgroups.borland.com 1150569641 207.105.83.66 (17 Jun 2006 11:40:41 -0700) Lines: 90 Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail