Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages
Started by Yilmaz
Wed, 09 Apr 2003 08:23
Reading From Serial Port
Author: 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
Re: Reading From Serial Port
Author: 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/
----------------------------------------
R: Reading From Serial Port
Author: Steve
Date: Wed, 09 Apr 2003 11:47
Date: Wed, 09 Apr 2003 11:47
27 lines
1874 bytes
1874 bytes
> 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... Maybe I haven't understood correctly your problem, but there's something strange in your code. What is the problem exactly? How can you say you don't receive any data? Have you tried the following values instead of yours? SerialPort.CommTimeOuts.ReadIntervalTimeout = MAXWORD; SerialPort.CommTimeOuts.ReadTotalTimeoutMultiplier = 0; SerialPort.CommTimeOuts.ReadTotalTimeoutConstant = 0; SerialPort.CommTimeOuts.WriteTotalTimeoutMultiplier = 0; SerialPort.CommTimeOuts.WriteTotalTimeoutConstant = 0; > inread=ReadFile(SerialPort.hComm,outbuffer,1,&dwBytesRead,NU LL); If I'm not wrong outbuffer is not allocated. Are you sure this line is correct? Have you tried to define outbuffer as "char outbuffer[2]" (the same for inbuffer, as well)? HTH, Steve.
Re: Reading From Serial Port
Author: Yilmaz
Date: Wed, 09 Apr 2003 12:56
Date: Wed, 09 Apr 2003 12:56
43 lines
2303 bytes
2303 bytes
Thanks Steve changing char *outbuffer; to char outbuffer[2] has worked... Thanks a lot "Steve" <steve_alettoANTI@SPAMhotmail.com> wrote in message news:3e94086a$1@newsgroups.borland.com... > > 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... > > Maybe I haven't understood correctly your problem, but > there's something strange in your code. What is the problem > exactly? How can you say you don't receive any data? > > Have you tried the following values instead of yours? > > SerialPort.CommTimeOuts.ReadIntervalTimeout = MAXWORD; > SerialPort.CommTimeOuts.ReadTotalTimeoutMultiplier = 0; > SerialPort.CommTimeOuts.ReadTotalTimeoutConstant = 0; > SerialPort.CommTimeOuts.WriteTotalTimeoutMultiplier = 0; > SerialPort.CommTimeOuts.WriteTotalTimeoutConstant = 0; > > > > inread=ReadFile(SerialPort.hComm,outbuffer,1,&dwBytesRead,NU > LL); > > If I'm not wrong outbuffer is not allocated. Are you sure > this line is correct? Have you tried to define outbuffer as > "char outbuffer[2]" (the same for inbuffer, as well)? > > HTH, > > Steve. > > --- 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
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