Article View: borland.public.cppbuilder.nativeapi
Article #17099Reading From Serial Port
From: Yilmaz
Date: Wed, 09 Apr 2003 08:23
Date: Wed, 09 Apr 2003 08:23
147 lines
5195 bytes
5195 bytes
Hi,
I am trying to write some code to use the serial port. I am olso attaching
a copy of the code below...
I am able to write out data to the serial port which has a twisted cable
attached. However, I am unable to read any data in...
Any help is appreciated..
Thanks
//--------------------------------------------------------------------------
-
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#include "Serial2.h"
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
class Serial {
public:
HANDLE hComm;
DCB PortDCB;
COMMTIMEOUTS CommTimeOuts;
}; Serial SerialPort;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//DCB dcbCport;
}
//--------------------------------------------------------------------------
-
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
if (SerialPort.hComm == INVALID_HANDLE_VALUE) Application->MessageBox("NO
can DO","eRRor",MB_OK);
else
Memo->Lines->Add("Serial Port is Open");
//Initialisation of Serial Port & Setting
PurgeComm(SerialPort.hComm, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT
|PURGE_RXABORT);
SerialPort.PortDCB.DCBlength = sizeof(DCB);
if(!GetCommState(SerialPort.hComm, &SerialPort.PortDCB)){
Application->MessageBox("Unable to get COM state","eRRor",MB_OK);
return;
}
if (!BuildCommDCB("9600,N,8,1",&SerialPort.PortDCB)){
Application->MessageBox("Unable to obtain DCB","eRRor",MB_OK);
return;
}
if(!SetCommState(SerialPort.hComm, &SerialPort.PortDCB)){
Application->MessageBox("Unable to set DCB","eRRor",MB_OK);
return;
}
if(!GetCommTimeouts(SerialPort.hComm, &SerialPort.CommTimeOuts)){
Application->MessageBox("Unable to obtain TimeOut
parameters","eRRor",MB_OK);
return;
}
SerialPort.CommTimeOuts.ReadIntervalTimeout = MAXWORD;
SerialPort.CommTimeOuts.ReadTotalTimeoutMultiplier = 1;
SerialPort.CommTimeOuts.ReadTotalTimeoutConstant = 250;
SerialPort.CommTimeOuts.WriteTotalTimeoutMultiplier =1;
SerialPort.CommTimeOuts.WriteTotalTimeoutConstant = 250;
if (!SetCommTimeouts(SerialPort.hComm,&SerialPort.CommTimeOuts)){
Application->MessageBox("Unable to set TimeOut parameters","eRRor",MB_OK);
return;
}
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button2Click(TObject *Sender)
{
char *inbuffer;
char *outbuffer;
DWORD dwBytesWritten;
DWORD dwBytesRead;
DWORD dwCommModemStatus;
int write,i,inread;
i=0;
inbuffer[i]=0x11;
// Write data to the Srial Port
write=WriteFile(SerialPort.hComm,inbuffer,1,&dwBytesWritten,NULL);
if(!write){
Application->MessageBox("Unable to WriteFile","eRRor",MB_OK);
return; }
//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");
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button3Click(TObject *Sender)
{
CloseHandle(SerialPort.hComm);
}
//--------------------------------------------------------------------------
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.467 / Virus Database: 266 - Release Date: 01/04/2003
Technical Details
Message-ID:
<3e93d88d@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,c1814f9ecc8efe9a,start X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-04-09 01:34:37 PST Reply-To: "Yilmaz" <y.gul@herts.ac.uk> From: "Yilmaz" <y.gul@herts.ac.uk> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Reading From Serial Port Date: Wed, 9 Apr 2003 09:23:40 +0100 Lines: 149 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 217.37.52.217 Message-ID: <3e93d88d@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1049876621 217.37.52.217 (9 Apr 2003 01:23:41 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail