Article View: borland.public.cppbuilder.nativeapi
Article #8825Again Comm Port - Windows Version
From: McKool
Date: Tue, 21 Sep 2004 08:34
Date: Tue, 21 Sep 2004 08:34
140 lines
4572 bytes
4572 bytes
Hello.... i made a Synchronous Comm Port and it works under XP OS where i
programmed it), but in W2K and 98 don't work. I will write here the Read,
Write and create functions....
CREATE FUNCTION
//-----------------------------------------------------------------------
bool __stdcall clsCommPort::CreatePort(
LPTSTR strComm
)
{
// Specific Port is Opened
//
m_hPort = CreateFile (strComm, GENERIC_READ | GENERIC_WRITE,0,
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
// Catch error
//
if (m_hPort == INVALID_HANDLE_VALUE){
wLastError = GetLastError();
return false;
}
COMMTIMEOUTS CommTimeouts;
// Time Outs of the are Configurated
//
GetCommTimeouts(m_hPort, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.ReadTotalTimeoutConstant = 1;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 5;
if (!SetCommTimeouts(m_hPort, &CommTimeouts)){
// Catch error
//
wLastError = GetLastError();
return false;
}
// Port is Configurated with the Espicified BaudRate
// and the Common and/or standart values
//
PortDCB.DCBlength = sizeof (DCB);
GetCommState(m_hPort, &PortDCB);
PortDCB.BaudRate = wBaudRate;
PortDCB.fBinary = true;
PortDCB.fParity = false;
PortDCB.fOutxCtsFlow = false;
PortDCB.fOutxDsrFlow = false;
PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
PortDCB.fDsrSensitivity = false;
PortDCB.fTXContinueOnXoff = true;
PortDCB.fOutX = false;
PortDCB.fInX = false;
PortDCB.fErrorChar = false;
PortDCB.fNull = false;
PortDCB.fRtsControl = RTS_CONTROL_DISABLE; //RTS_CONTROL_ENABLE
PortDCB.XoffLim = 100;
PortDCB.XonLim = 100;
PortDCB.ByteSize = 8;
PortDCB.fAbortOnError = true;
PortDCB.wReserved = 0;
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONESTOPBIT;
// Set Dcb Configuration to the Port
//
if (!SetCommState(m_hPort, &PortDCB)){
// Catch Error
//
wLastError = GetLastError();
return false;
}
// Everything Ok
//
return true;
--
//-----------------------------------------------------------------------
READ FUNCTION:
//-----------------------------------------------------------------------
DWORD __stdcall PortReadThread(
LPVOID lpvoid
)
{
clsCommPort* pPort = (clsCommPort*) lpvoid;
DWORD dwBytesTransferred;
BYTE buffer[2048];
while(pPort->GetHandlerPort() != INVALID_HANDLE_VALUE){
ReadFile(pPort->GetHandlerPort(), buffer, 1,
&dwBytesTransferred,NULL);
if(dwBytesTransferred == 0)
Sleep(2);
else{
for (i=0; i < (int)dwBytesTransferred; i++){
pPort->AddCharToQueue(buffer[i]);
}
dwBytesTransferred = 0;
pPort->Read();
}
}
// Finish Thread
//
return 0;
//-----------------------------------------------------------------------
WRITE FUNCTION
//-----------------------------------------------------------------------
bool __stdcall clsCommPort::Write(
BYTE Byte
)
DWORD dwBWrote,iErr;
bool bTest;
bTest = WriteFile(m_hPort,&Byte,1,&dwBWrote,NULL);
if(!bTest){
if(dwBWrote == 0)
return false;
iErr = GetLastError ();
if(iErr != ERROR_IO_PENDING)
return false;
}
return true;
//------------------------------------------------------------------------
This work perfecly in Windows XP -SP 2.
Keneth Wagner.
Technical Details
Message-ID:
<414fe69c$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6ff2f2a7e9dff124,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "McKool" <kenethw@peak-system.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Again Comm Port - Windows Version Date: Tue, 21 Sep 2004 10:34:28 +0200 Lines: 142 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 213.157.13.214 Message-ID: <414fe69c$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1095755420 213.157.13.214 (21 Sep 2004 01:30:20 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail