size : 1635
uploaded_on : Thu Nov 5 00:00:00 1998
modified_on : Wed Dec 8 14:03:26 1999
title : Serial port reading
org_filename : SerPortReading.txt
author : Radu Ialovoi
authoremail : polystar@icnet.ro
description : How to read data from the serial port
keywords :
tested : not tested yet
submitted_by : The CKB Crew
submitted_by_email : ckb@netalive.org
uploaded_by : nobody
modified_by : nobody
owner : nobody
lang : plain
file-type : text/plain
category : delphi-system32bit
__END_OF_HEADER__
> I need some help for read data sent to the serial port.
> Can someone give me a example?
procedure TForm1.Button1Click(Sender: TObject);
var
lpszNumeCom : PChar;
dwAccessType : DWORD;
dwCreationType : DWORD;
creationDCB : TDCB;
hPort : THandle;
buffer : array[0..55] of char;
actRead : integer;
begin
{Some initial settings: I am working with COM2 in read/write}
lpszNumeCom := '//./COM2';
dwAccessType := GENERIC_READ or GENERIC_WRITE;
dwCreationType := OPEN_EXISTING;
hPort := CreateFile(lpszNumeCom,dwAccessType,0,Nil,dwCreationType,0,0);
{here I have the handle for the COM port}
try
if hPort <> INVALID_HANDLE_VALUE then
begin
{Settings for COM : 600bauds, 8,E,1}
GetCommState(hport,creationDCB);
creationDCB.BaudRate := 600;
creationDCB.Parity := EVENPARITY;
creationDCB.ByteSize := 8;
creationDCB.StopBits := ONESTOPBIT;
{Settings for the control type, here I am reading from a Proximity
card
reader and use only the RX line}
creationDCB.Flags := creationDCB.Flags + DTR_CONTROL_DISABLE
+ RTS_CONTROL_DISABLE;
if SetCommState(hPort,creationDCB) then
begin
PurgeComm(hPort,PURGE_RXCLEAR);
{Here I actually can read data from COM}
{The next function call will not return until it actually reads
data
from the serial port}
ReadFile(hPort,buffer,56,actRead,nil);
PurgeComm(hPort,PURGE_RXCLEAR);
{in buffer you have data readed from the port}
begin
{Do something usefull with data}
end
end
end;
finally
CloseHandle(hPort);
end;
end;