Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages
Started by Mario
Mon, 05 May 2003 21:26
Problem with CreateFile() function API
Author: Mario
Date: Mon, 05 May 2003 21:26
Date: Mon, 05 May 2003 21:26
165 lines
4980 bytes
4980 bytes
When I send one series of bytes on the serial port, I receive the same
buffer wich I transmit, while I would have myself to be waited for the
answer of OK from the modem. How can I resolve this problem ?
I have used the following example:
//--------------------------------------------------------------------------
-
#include <vcl.h>
#include <windows.h>
#include <stdio.h>
#include <tapi.h>
#include <assert.h>
#pragma hdrstop
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
Tcalling *calling;
//--------------------------------------------------------------------------
-
__fastcall Tcalling::Tcalling(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
-
void __fastcall Tcalling::Button1Click(TObject *Sender)
{
state->Clear();
state->Lines->Add("###START");
if ( setComm("COM1", 38400, 8) )
{
if ( writeComm("AT\r\n", 4) )
{
readComm();
}
CloseHandle(hCom);
}
state->Lines->Add("###END");
}
//--------------------------------------------------------------------------
-
bool __fastcall Tcalling::setComm(char *pcCommPort, int baudRate, int
byteSize)
{
DCB dcb;
bool fSuccess;
// Create handle file with the serial port
hCom =CreateFile(pcCommPort,
GENERIC_READ |GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hCom ==INVALID_HANDLE_VALUE)
{
state->Lines->Add("Port not ready !");
fSuccess =false;
}
else
{
// Read state of the serial port
fSuccess =GetCommState(hCom, &dcb);
if (!fSuccess)
{
state->Lines->Add("Failed open port !");
}
else
{
dcb.BaudRate =baudRate;
dcb.ByteSize =byteSize;
dcb.Parity =NOPARITY;
dcb.StopBits =ONESTOPBIT;
// Initiate the serial port
fSuccess =SetCommState(hCom, &dcb);
if (!fSuccess)
{
state->Lines->Add("Failed initiate port !");
}
else
{
bool fSuccess =SetCommMask(hCom, EV_TXEMPTY);
if (!fSuccess)
fSuccess =false;
else
{
o.hEvent =CreateEvent(NULL, FALSE, FALSE, NULL);
assert(o.hEvent);
COMMTIMEOUTS ctmoCommPort;
ctmoCommPort.ReadIntervalTimeout = MAXDWORD;
ctmoCommPort.ReadTotalTimeoutMultiplier = MAXDWORD;
ctmoCommPort.ReadTotalTimeoutConstant = MAXDWORD;
ctmoCommPort.WriteTotalTimeoutMultiplier = 0;
ctmoCommPort.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hCom, &ctmoCommPort);
}
}
}
}
return fSuccess;
}
//--------------------------------------------------------------------------
-
int __fastcall Tcalling::readComm()
{
char *inBuffer;
int nBytesRead =0;
DWORD nBytesToRead =4;
if ( !ReadFile(hCom, &inBuffer, nBytesToRead, (DWORD *)&nBytesRead,
NULL) )
nBytesRead =-1;
if (nBytesRead >0)
state->Lines->Add("read: " +(String)inBuffer);
return nBytesRead;
}
//--------------------------------------------------------------------------
-
int __fastcall Tcalling::writeComm(char *outBuffer, int nBytesToWrite)
{
int nBytesWritten;
if (nBytesToWrite>0)
state->Lines->Add("write:" +(String)outBuffer);
if ( WriteFile(hCom, &outBuffer, nBytesToWrite, (DWORD *)&nBytesWritten,
NULL) )
{
if ( FlushFileBuffers(hCom) )
{
DWORD dwEvtMask;
if (WaitCommEvent(hCom, &dwEvtMask, NULL))
{
if (dwEvtMask & EV_TXEMPTY)
state->Lines->Add("write complete");
else
nBytesWritten =-1;
}
}
}
else
nBytesWritten =-1;
return nBytesWritten;
}
//--------------------------------------------------------------------------
-
Thanks Mario.
Re: Problem with CreateFile() function API
Author: Remy Lebeau (TeamB)
Date: Mon, 05 May 2003 22:02
Date: Mon, 05 May 2003 22:02
14 lines
1457 bytes
1457 bytes
"Mario" <m.sonnessa.spes@urmetsistemi.it> wrote in message news:3eb6d718@newsgroups.borland.com... > When I send one series of bytes on the serial port, I receive > the same buffer wich I transmit, while I would have myself > to be waited for the answer of OK from the modem. How > can I resolve this problem ? Did you verify that your modem is not simply configured with ECHO enabled? If it is, then it will return a copy of the data that was sent to it, prior to then sending its own response data as well. Gambit
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads