Article View: borland.public.cppbuilder.nativeapi
Article #17101Re: Reading From Serial Port
From: Michel Leunen
Date: Wed, 09 Apr 2003 09:13
Date: Wed, 09 Apr 2003 09:13
50 lines
2935 bytes
2935 bytes
Yilmaz wrote:
> void __fastcall TForm1::Button1Click(TObject *Sender)
> {
> // Opening the Communication resource for overlapped operation
>
>
> SerialPort.hComm = NULL;
> SerialPort.hComm = CreateFile(ComboBox1->Text.c_str(), // Pointer to the
> name of the port
> GENERIC_WRITE | GENERIC_READ, // Access
> (read-write) mode
> 0, // Share mode
> NULL, // Pointer to the security
> attribute
> OPEN_EXISTING, // How to open the serial
> port
> 0, // Port attributes
> 0); // Handle to port with
> attribute to copy
Last parameter is NULL so you're *NOT* opening your Com port for
OVERLAPPED I/O. But it's not related to your problem.
> //Read data to the Serial Port
> inread=ReadFile(SerialPort.hComm,outbuffer,1,&dwBytesRead,NULL);
> if(!inread){
> Application->MessageBox("Unable to Read Serial Port","eRRor",MB_OK);
> return; }
>
> Memo->Lines->Add("Data sent to Serial");
Where do you define outbuffer? And why read (or write) one byte at a
time. It's more efficient to read a chunk of bytes.
char outbuffer[256];
inread=ReadFile(SerialPort.hComm,outbuffer,sizeof(outbuffer),&dwBytesRead,NULL);
if(!inread)//Error
if(dwBytesRead<sizeof(outbuffer))
outbuffer[dwBytesRead]='\0';
Michel
--
----------------------------------------
Michel Leunen
mailto:michel@leunen.com
http://www.leunen.com/cbuilder/
----------------------------------------
Technical Details
Message-ID:
<3e93e428$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!priapus.visi.com!news-out.visi.com!hermes.visi.com!newshosting.com!news-xfer2.atl.newshosting.com!216.166.71.118.MISMATCH!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3e93d88d@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,c1814f9ecc8efe9a X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-04-09 02:21:37 PST Date: Wed, 09 Apr 2003 11:13:07 +0200 From: Michel Leunen <mleu@skynet.be> Organization: http://www.leunen.com/cbuilder/ User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3) Gecko/20030312 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Reading From Serial Port References: <3e93d88d@newsgroups.borland.com> In-Reply-To: <3e93d88d@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 217.136.41.226 Message-ID: <3e93e428$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1049879592 217.136.41.226 (9 Apr 2003 02:13:12 -0800) Lines: 51 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!priapus.visi.com!news-out.visi.com!hermes.visi.com!newshosting.com!news-xfer2.atl.newshosting.com!216.166.71.118.MISMATCH!small1.nntp.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail