Skip to content

gcardi/DelphiSerialPort

Repository files navigation

DelphiSerialPort

DelphiSerialPort is a lightweight, blocking serial port communication library for Windows using Delphi (Object Pascal). It wraps Win32 serial APIs into a clean object-oriented interface (TCommPort) and includes a demo VCL application (Project1) for interactive testing.

Demo screen shot

Features

  • Simple serial port abstraction for COM devices (RS-232/RS-485, modem, microcontroller)
  • Port configuration: baud rate, data bits, stop bits, parity, RTS control
  • Blocking read/write with configurable timeouts
  • Byte/Buffer and text methods (ReadBytes, ReadString, WriteBuffer, WriteString)
  • Error handling via ECommError with clear code and message
  • Utility methods: PurgeCommPort, FlushCommPort, BytesAvailable, GetCommProp

Library API

Core classes

  • ECommError: exception class with error types like OPEN_ERROR, READ_ERROR, WRITE_ERROR, BAD_BAUD_RATE, etc.
  • TCommPort: main class for serial port operations.

TCommPort lifecycle

  1. Create(ReadTimeout, WriteTimeout: DWORD)
  2. SetCommPort('\\.\\COMx')
  3. SetBaudRate, SetByteSize, SetParity, SetStopBits or use DCB
  4. OpenCommPort
  5. Read/Write operations
  6. CloseCommPort
  7. Free

Port settings methods

  • SetCommPort(Port: string) / GetCommPort
  • SetBaudRate(newBaud: DWORD) / GetBaudRate
  • SetParity(newParity: BYTE) / GetParity
  • SetByteSize(newByteSize: BYTE) / GetByteSize
  • SetStopBits(newStopBits: BYTE) / GetStopBits
  • SetRTS(Mode: TCommPort.RTSMode)
  • SetCommDCBProperties / GetCommDCBProperties

I/O methods

  • WriteBuffer(const Buffer: TBytes; NumBytes: integer)
  • WriteString(outString: string) (ASCII)
  • ReadBytes(out Buffer: TBytes; MaxBytes: DWORD): DWORD
  • ReadString(MaxBytes: DWORD): string (ASCII)
  • PutByte(value: BYTE)
  • GetByte: BYTE
  • BytesAvailable: DWORD

Port control

  • PurgeCommPort (clears receive queue)
  • FlushCommPort (flushes OS buffers)
  • GetConnected: BOOL
  • GetHandle: THandle
  • GetCommProp(out properties: COMMPROP)

Error handling

Wrap operations with try/except on ECommError:

try
  comm.OpenCommPort;
except
  on E: ECommError do
    ShowMessage('Serial port failed: ' + E.Message);
end;

Demo application

The demo (Project1.dpr, Unit1.pas) provides a simple VCL form with controls:

  • COM port string input
  • baud rate, byte size, parity, stop bits comboboxes
  • read and write timeout inputs
  • manual open/close buttons
  • text entry for WriteString
  • read button with output in TMemo

demo flow

  1. User selects port settings
  2. actOpen creates TCommPort, configures and opens the connection
  3. actWriteString sends text to device
  4. Button4Click reads text from device and appends to memo
  5. actClose closes and frees the port

Quick start example

uses
  CommPort;

var
  comm: TCommPort;
  bytes: TBytes;
  n: DWORD;
begin
  comm := TCommPort.Create(500, 500); // 500ms read/write timeout
  try
    comm.SetCommPort('\\.\COM3');
    comm.SetBaudRate(115200);
    comm.SetByteSize(8);
    comm.SetParity(NOPARITY);
    comm.SetStopBits(ONESTOPBIT);
    comm.OpenCommPort;

    comm.WriteString('Hello');

    n := comm.ReadBytes(bytes, 128);
    ShowMessage(Format('Received %d bytes', [n]));

    comm.CloseCommPort;
  finally
    comm.Free;
  end;
end;

Notes

  • Windows-only (uses Winapi.Windows serial APIs)
  • Blocking operations may block UI thread; run on background thread for responsive GUI.
  • Use full device name for COM ports >= COM10 (\\.\\COM10).

License

See LICENSE.

Releases

No releases published

Packages

 
 
 

Contributors

Languages