Class TCommPortDriver (unit ComDrv32)

Inherits from

TComponent

Constructors


constructor Create( AOwner: TComponent );

Constructor

+-------------------------------------------------------------------------+ | TCOMMPORTDRIVER | +-------------------------------------------------------------------------+



Functions

function Connect: boolean;

Opens the COM port and takes of it.

function Connected: boolean;

Returns true if COM port has been opened

Returns true if connected


procedure ContinuePolling;

Re-starts polling (after pause)

Re-starts polling (after pause)


function CountRX: integer;

Returns number of received bytes in the RX buffer

Returns number of received bytes in the RX buffer


destructor Destroy;

Destructor

Allocate a window handle to catch timer's notification messages


procedure Disconnect;

Closes the COM port and releases control of it

Start the timer (used for polling)


procedure FlushBuffers( inBuf, outBuf: boolean );

Flushes the rx/tx buffers

Flush rx/tx buffers


function GetLineStatus: TComPortLineStatusSet;

Returns the current state of CTS, DSR, RING and RLSD (CD) lines.

function IsPolling: boolean;

Returns true if polling has not been paused

Returns true if polling has not been paused


function OutFreeSpace: word;

Returns the output buffer free space or 65535 if not connected

Returns the output buffer free space or 65535 if not connected


procedure PausePolling;

Pauses polling

Pauses polling


function ReadByte( var Value: byte ): boolean;

Reads a byte.

function ReadChar( var Value: char ): boolean;

Reads a char.

function ReadData( DataPtr: pchar; MaxDataSize: UINT ): UINT;

Reads binary data.

function SendByte( Value: byte ): boolean;

Sends a byte.

function SendChar( Value: char ): boolean;

Sends a char.

function SendData( DataPtr: pointer; DataSize: UINT ): UINT;

Sends binary data

Send data (breaks the data in small packets if it doesn't fit in the output buffer)


function SendDataEx( DataPtr: pchar; DataSize, Timeout: UINT ): UINT;

Sends binary data.

function SendString( s: string ): boolean;

Sends a pascal string (NULL terminated if $H+ (default))

Sends a pascal string (NULL terminated if $H+ (default))


function SendZString( s: pchar ): boolean;

Sends a C-style strings (NULL terminated)

Sends a C-style string (NULL terminated)


procedure ToggleDTR( onOff: boolean );

Set DTR line high (onOff=TRUE) or low (onOff=FALSE).

procedure ToggleRTS( onOff: boolean );

Set RTS line high (onOff=TRUE) or low (onOff=FALSE).

procedure ApplyCOMSettings;

Applies current settings to open COM port

Apply COM settings


procedure SetComHandle( Value: THANDLE );

Sets the COM port handle

The COM port handle made public and writeable.


procedure SetComPort( Value: TComPortNumber );

Selects the COM port to use

Start the timer (used for polling)


procedure SetComPortBaudRate( Value: TComPortBaudRate );

Selects the baud rate

Change COM port


procedure SetComPortDataBits( Value: TComPortDataBits );

Selects the number of data bits

Apply changes


procedure SetComPortHwHandshaking( Value: TComPortHwHandshaking );

Selects the kind of hardware flow control

Apply changes


procedure SetComPortInBufSize( Value: word );

Sets the RX buffer size

Apply changes


procedure SetComPortOutBufSize( Value: word );

Sets the TX buffer size

Adjust the RX packet size


procedure SetComPortParity( Value: TComPortParity );

Selects the kind of parity

Apply changes


procedure SetComPortPollingDelay( Value: word );

Sets the delay between polling checks

PacketTimeout cannot be less than polling delay + some extra ms


procedure SetComPortStopBits( Value: TComPortStopBits );

Selects the number of stop bits

Apply changes


procedure SetComPortSwHandshaking( Value: TComPortSwHandshaking );

Selects the kind of software flow control

Apply changes


procedure SetPacketSize( Value: smallint );

Sets the size of incoming packets

Set new output buffer size


procedure SetPacketTimeout( Value: integer );

Sets the timeout for incoming packets

If the PacketSize if greater than then RX buffer size then increase the RX buffer size


procedure TimerWndProc( var msg: TMessage );

Polling proc

COM port polling proc


Properties

property CheckLineStatus : boolean

Set to TRUE to prevent hangs when not device connected or device is OFF

property ComPort : TComPortNumber

# of the COM Port to use

property ComPortDataBits : TComPortDataBits

Data bits to used (5.

property ComPortHwHandshaking : TComPortHwHandshaking

Kind of Hardware Handshaking to use: hhNONE no handshaking hhNONERTSON no handshaking but keep RTS line on hhCTSRTS RTS/CTS

property ComPortInBufSize : word

Input Buffer size

property ComPortOutBufSize : word

Output Buffer size

property ComPortParity : TComPortParity

Kind of Parity to use (none,odd,even,mark,space)

property ComPortPollingDelay : word

ms of delay between COM port pollings

property ComPortSpeed : TComPortBaudRate

COM Port speed (bauds)

property ComPortStopBits : TComPortStopBits

Stop bits to use (1, 1.

property ComPortSwHandshaking : TComPortSwHandshaking

Kind of Software Handshaking to use: shNONE no handshaking shXONXOFF XON/XOFF handshaking

property EnableDTROnOpen : boolean

Set to TRUE to enable DTR line on connect and to leave it on until disconnect.

property InputTimeout : UINT

Input timeout (milliseconds)

property OutputTimeout : word

Output timeout (milliseconds)

property PacketMode : TPacketMode

What to do with incomplete packets (in RX)

property PacketSize : smallint

RX packet size (this value must be less than ComPortInBufSize)

property PacketTimeout : integer

Timeout (ms) for a complete packet (in RX)

property ComHandle : THANDLE

Make the Handle of the COM port public (for TAPI.

Events

event OnReceiveData : TComPortReceiveDataEvent

Event to raise when there is data available (input buffer has data) (called only if PacketSize = 0)

event OnReceivePacket : TComPortReceivePacketEvent

Event to raise when there is data packet available (called only if PacketSize <> 0)

Variables

FCkLineStatus : boolean;

Set to TRUE to prevent hangs when no device connected or device is OFF

FComPort : TComPortNumber;

# of the COM Port to use

FComPortBaudRate : TComPortBaudRate;

COM Port speed (brXXX)

FComPortDataBits : TComPortDataBits;

Data bits size (dbXXX)

FComPortHandle : THANDLE;

COM Port Device Handle

FComPortHwHandshaking : TComPortHwHandshaking;

Type of hw handshaking (hw flow control) to use (hhXXX)

FComPortInBufSize : word;

Size of the input buffer

FComPortOutBufSize : word;

Size of the output buffer

FComPortParity : TComPortParity;

Type of parity to use (ptXXX)

FComPortPollingDelay : word;

ms of delay between COM port pollings

FComPortReceiveData : TComPortReceiveDataEvent;

Event to raise on data reception (asynchronous)

FComPortReceivePacket : TComPortReceivePacketEvent;

Event to raise on packet reception (asynchronous)

FComPortStopBits : TComPortStopBits;

How many stop bits to use (sbXXX)

FComPortSwHandshaking : TComPortSwHandshaking;

Type of sw handshaking (sw flow control) to use (shXXX)

FEnableDTROnOpen : boolean;

Specifies if the DTR line must be enabled/disabled on connect

FFirstByteOfPacketTime : DWORD;

Time of the first byte of current RX packet

FInputTimeout : UINT;

Timeout for ReadData

FNotifyWnd : HWND;

This is used for the timer

FOutputTimeout : word;

Output timeout - milliseconds

FPacketMode : TPacketMode;

What to do with incomplete packets (pmXXX)

FPacketSize : smallint;

Size of a data packet

FPacketTimeout : integer;

ms to wait for a complete packet (<=0 = disabled)

FRXPollingPauses : integer;

Number of RX polling timer pauses

FTempInBuffer : pointer;

Temporary buffer (RX) - used internally


Constructors


constructor Create( AOwner: TComponent );

Constructor

+-------------------------------------------------------------------------+ | TCOMMPORTDRIVER | +-------------------------------------------------------------------------+


Functions


function Connect: boolean;

Opens the COM port and takes of it. Returns false if something goes wrong.

Setup buffers size


function Connected: boolean;

Returns true if COM port has been opened

Returns true if connected


procedure ContinuePolling;

Re-starts polling (after pause)

Re-starts polling (after pause)


function CountRX: integer;

Returns number of received bytes in the RX buffer

Returns number of received bytes in the RX buffer


destructor Destroy;

Destructor

Allocate a window handle to catch timer's notification messages


procedure Disconnect;

Closes the COM port and releases control of it

Start the timer (used for polling)


procedure FlushBuffers( inBuf, outBuf: boolean );

Flushes the rx/tx buffers

Flush rx/tx buffers


function GetLineStatus: TComPortLineStatusSet;

Returns the current state of CTS, DSR, RING and RLSD (CD) lines. The function fails if the hardware does not support the control-register values (that is, returned set is always empty).

Returns CTS, DSR, RING and RLSD (CD) signals status


function IsPolling: boolean;

Returns true if polling has not been paused

Returns true if polling has not been paused


function OutFreeSpace: word;

Returns the output buffer free space or 65535 if not connected

Returns the output buffer free space or 65535 if not connected


procedure PausePolling;

Pauses polling

Pauses polling


function ReadByte( var Value: byte ): boolean;

Reads a byte. Returns true if the byte has been read

Reads a byte. Returns true if the byte has been read


function ReadChar( var Value: char ): boolean;

Reads a char. Returns true if char has been read

Reads a char. Returns true if char has been read


function ReadData( DataPtr: pchar; MaxDataSize: UINT ): UINT;

Reads binary data. Returns number of bytes read

Reads binary data. Returns number of bytes read


function SendByte( Value: byte ): boolean;

Sends a byte. Returns true if the byte has been sent

Sends a byte. Returns true if the byte has been sent


function SendChar( Value: char ): boolean;

Sends a char. Returns true if the char has been sent

Sends a char. Returns true if the char has been sent


function SendData( DataPtr: pointer; DataSize: UINT ): UINT;

Sends binary data

Send data (breaks the data in small packets if it doesn't fit in the output buffer)


function SendDataEx( DataPtr: pchar; DataSize, Timeout: UINT ): UINT;

Sends binary data. Returns number of bytes sent. Timeout overrides the value specifiend in the OutputTimeout property

Sends binary data. Returns number of bytes sent. Timeout overrides the value specifiend in the OutputTimeout property


function SendString( s: string ): boolean;

Sends a pascal string (NULL terminated if $H+ (default))

Sends a pascal string (NULL terminated if $H+ (default))


function SendZString( s: pchar ): boolean;

Sends a C-style strings (NULL terminated)

Sends a C-style string (NULL terminated)


procedure ToggleDTR( onOff: boolean );

Set DTR line high (onOff=TRUE) or low (onOff=FALSE). You must not use HW handshaking.

Set DTR line high (onOff=TRUE) or low (onOff=FALSE). You must not use HW handshaking.


procedure ToggleRTS( onOff: boolean );

Set RTS line high (onOff=TRUE) or low (onOff=FALSE). You must not use HW handshaking.

Set RTS line high (onOff=TRUE) or low (onOff=FALSE). You must not use HW handshaking.


procedure ApplyCOMSettings;

Applies current settings to open COM port

Apply COM settings


procedure SetComHandle( Value: THANDLE );

Sets the COM port handle

The COM port handle made public and writeable. This lets you connect to external opened com port. Setting ComPortHandle to 0 acts as Disconnect.


procedure SetComPort( Value: TComPortNumber );

Selects the COM port to use

Start the timer (used for polling)


procedure SetComPortBaudRate( Value: TComPortBaudRate );

Selects the baud rate

Change COM port


procedure SetComPortDataBits( Value: TComPortDataBits );

Selects the number of data bits

Apply changes


procedure SetComPortHwHandshaking( Value: TComPortHwHandshaking );

Selects the kind of hardware flow control

Apply changes


procedure SetComPortInBufSize( Value: word );

Sets the RX buffer size

Apply changes


procedure SetComPortOutBufSize( Value: word );

Sets the TX buffer size

Adjust the RX packet size


procedure SetComPortParity( Value: TComPortParity );

Selects the kind of parity

Apply changes


procedure SetComPortPollingDelay( Value: word );

Sets the delay between polling checks

PacketTimeout cannot be less than polling delay + some extra ms


procedure SetComPortStopBits( Value: TComPortStopBits );

Selects the number of stop bits

Apply changes


procedure SetComPortSwHandshaking( Value: TComPortSwHandshaking );

Selects the kind of software flow control

Apply changes


procedure SetPacketSize( Value: smallint );

Sets the size of incoming packets

Set new output buffer size


procedure SetPacketTimeout( Value: integer );

Sets the timeout for incoming packets

If the PacketSize if greater than then RX buffer size then increase the RX buffer size


procedure TimerWndProc( var msg: TMessage );

Polling proc

COM port polling proc


Properties


property CheckLineStatus : boolean

Set to TRUE to prevent hangs when not device connected or device is OFF


property ComPort : TComPortNumber

# of the COM Port to use


property ComPortDataBits : TComPortDataBits

Data bits to used (5..8, for the 8250 the use of 5 data bits with 2 stop bits is an invalid combination, as is 6, 7, or 8 data bits with 1.5 stop bits)


property ComPortHwHandshaking : TComPortHwHandshaking

Kind of Hardware Handshaking to use: hhNONE no handshaking hhNONERTSON no handshaking but keep RTS line on hhCTSRTS RTS/CTS


property ComPortInBufSize : word

Input Buffer size


property ComPortOutBufSize : word

Output Buffer size


property ComPortParity : TComPortParity

Kind of Parity to use (none,odd,even,mark,space)


property ComPortPollingDelay : word

ms of delay between COM port pollings


property ComPortSpeed : TComPortBaudRate

COM Port speed (bauds)


property ComPortStopBits : TComPortStopBits

Stop bits to use (1, 1.5, 2)


property ComPortSwHandshaking : TComPortSwHandshaking

Kind of Software Handshaking to use: shNONE no handshaking shXONXOFF XON/XOFF handshaking


property EnableDTROnOpen : boolean

Set to TRUE to enable DTR line on connect and to leave it on until disconnect. Set to FALSE to disable DTR line on connect.


property InputTimeout : UINT

Input timeout (milliseconds)


property OutputTimeout : word

Output timeout (milliseconds)


property PacketMode : TPacketMode

What to do with incomplete packets (in RX)


property PacketSize : smallint

RX packet size (this value must be less than ComPortInBufSize)


property PacketTimeout : integer

Timeout (ms) for a complete packet (in RX)


property ComHandle : THANDLE

Make the Handle of the COM port public (for TAPI...) [read/write]


Events


event OnReceiveData : TComPortReceiveDataEvent

Event to raise when there is data available (input buffer has data) (called only if PacketSize = 0)


event OnReceivePacket : TComPortReceivePacketEvent

Event to raise when there is data packet available (called only if PacketSize <> 0)


Variables


FCkLineStatus : boolean;

Set to TRUE to prevent hangs when no device connected or device is OFF


FComPort : TComPortNumber;

# of the COM Port to use


FComPortBaudRate : TComPortBaudRate;

COM Port speed (brXXX)


FComPortDataBits : TComPortDataBits;

Data bits size (dbXXX)


FComPortHandle : THANDLE;

COM Port Device Handle


FComPortHwHandshaking : TComPortHwHandshaking;

Type of hw handshaking (hw flow control) to use (hhXXX)


FComPortInBufSize : word;

Size of the input buffer


FComPortOutBufSize : word;

Size of the output buffer


FComPortParity : TComPortParity;

Type of parity to use (ptXXX)


FComPortPollingDelay : word;

ms of delay between COM port pollings


FComPortReceiveData : TComPortReceiveDataEvent;

Event to raise on data reception (asynchronous)


FComPortReceivePacket : TComPortReceivePacketEvent;

Event to raise on packet reception (asynchronous)


FComPortStopBits : TComPortStopBits;

How many stop bits to use (sbXXX)


FComPortSwHandshaking : TComPortSwHandshaking;

Type of sw handshaking (sw flow control) to use (shXXX)


FEnableDTROnOpen : boolean;

Specifies if the DTR line must be enabled/disabled on connect


FFirstByteOfPacketTime : DWORD;

Time of the first byte of current RX packet


FInputTimeout : UINT;

Timeout for ReadData


FNotifyWnd : HWND;

This is used for the timer


FOutputTimeout : word;

Output timeout - milliseconds


FPacketMode : TPacketMode;

What to do with incomplete packets (pmXXX)


FPacketSize : smallint;

Size of a data packet


FPacketTimeout : integer;

ms to wait for a complete packet (<=0 = disabled)


FRXPollingPauses : integer;

Number of RX polling timer pauses


FTempInBuffer : pointer;

Temporary buffer (RX) - used internally