Article View: borland.public.cppbuilder.nativeapi
Article #6989Serial Port general reading/writing questions
From: Vladimir Stefanovic
Date: Mon, 21 Mar 2005 14:22
Date: Mon, 21 Mar 2005 14:22
130 lines
5121 bytes
5121 bytes
Hi,
I have never before worked with serial ports and so I
have some questions about their propper usage. My
questions are for now pretty generalized, and I need
some additional answers to start coding.
My target is to make an 'SMS Server' (Service app +
TServerSocket + TThisSerialPortProblem class).
SMS Server will receive strings (and collect them in a
TStringList) from clients which are in fact AT commands
supposed to be written to COM port with Mobile Phone or
GSM Modem connected to it.
Questions:
1) What should be configurable at my side rather than
hardcoded? For example, I know that I must have at least:
strings 'COM1'..'COM4', because of CreateFile(). Further,
I think I could also have:
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 20;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.WriteTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 100;
or, this:
dcb.fOutxCtsFlow = false; // Disable CTS monitoring
dcb.fOutxDsrFlow = false; // Disable DSR monitoring
dcb.fDtrControl = DTR_CONTROL_DISABLE; // Disable DTR monitoring
dcb.fOutX = false; // Disable XON/XOFF for transmission
dcb.fInX = false; // Disable XON/XOFF for receiving
dcb.fRtsControl = RTS_CONTROL_DISABLE; // Disable RTS (Ready To Send)
BaudRate = 9600;
ByteSize = 8;
Parity = NOPARITY;
StopBits = ONESTOPBIT;
fAbortOnError = true;
What do you think I should keed in config and what hardcoded?
2) About sending data. If I have TStringList which contains
AT commands, how should I technically send them /pseudo/:
a) // Port is opened all the time
CreateFile(); // Opens port at server start/resume
for ( int i=0; i<ATCommands->Count; i++ )
WriteSomeBufferToSpecifiedComPort( ATCommands[i] );
CloseHandle(); // Close port at server stop/pause
b) // Port is opened for sending a whole list, and then closes
CreateFile(); // Open port at start of sending a serie
for ( int i=0; i<ATCommands->Count; i++ )
WriteSomeBufferToSpecifiedComPort( ATCommands[i] );
CloseHandle(); // Close port when a serie is sent
c) // Port is opened for sending a single item only
for ( int i=0; i<ATCommands->Count; i++ )
{
CreateFile(); // Opens port
WriteSomeBufferToSpecifiedComPort( ATCommands[i] );
CloseHandle(); // Closes port
}
I suppose that (a) is the most appropriate, because (If I want
to extend the system for bi-directional transfer) how can I even
read the closed port if some SMS message is sent from outsde to
my 'system mobile phone'.
3) When writing data (WriteFile) to COM port, how the device
knows that is has received the whole package of data (one item
'AT command' from stringlist), and what is the second, etc?
Maybe the '\0' is the terminator?
4) I intend to use this code skeleton for sending a single item
(AT command) to COM port:
bool WriteABuffer( char *lpBuf, DWORD dwToWrite )
{
OVERLAPPED osWrite = {0};
DWORD dwWritten;
bool bResult;
if ( !WriteFile( hComm, lpBuf, dwToWrite, &dwWritten, NULL ) )
{
if ( GetLastError() != ERROR_IO_PENDING )
{
// WriteFile failed!
bResult = false;
}
else
{
// Write is pending...
if ( !GetOverlappedResult( hComm, &osWrite, &dwWritten, true ) )
{
bResult = false;
}
else
{
// Write operation completed successfully.
bResult = true;
}
}
}
else
{
// WriteFile completed immediately.
bResult = true;
}
return bResult;
}
Does this code can be used for a loop?
for ( int i=0; i<ATCommands->Count; i++ )
WriteABuffer( ATCommands[i].c_str(), ATCommands[i].Length() );
5) For my case are 'overlapped' or 'non-overlapped' solutions better?
--
Best regards,
Vladimir Stefanovic
Technical Details
Message-ID:
<423ed88d@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.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,b842bafb98039a75,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Serial Port general reading/writing questions Date: Mon, 21 Mar 2005 15:22:14 +0100 Lines: 132 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: 82.117.212.65 Message-ID: <423ed88d@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1111414925 82.117.212.65 (21 Mar 2005 06:22:05 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail