simple_serial

Cross-Platform Serial Port Communication for Eiffel

Phase 1 MIT

Overview

simple_serial provides serial port (COM port) communication for Eiffel applications. It uses inline C externals for Win32 API calls following the Eric Bezault pattern. Supports configurable baud rates, parity, stop bits, and flow control with a fluent builder API.

Part of the Simple Eiffel ecosystem.

Quick Start

Installation

  1. Set the environment variable:
    SIMPLE_EIFFEL=D:\prod
  2. Add to your ECF file:
    <library name="simple_serial" location="$SIMPLE_EIFFEL/simple_serial/simple_serial.ecf"/>

Basic Usage

local
    serial: SIMPLE_SERIAL
    port: detachable SERIAL_PORT
do
    create serial
    port := serial.open_port ("COM3")

    if attached port as p then
        if p.write_string ("Hello device!") then
            print (p.read_string (100))
        end
        p.close
    end
end

Classes

Class Description
SIMPLE_SERIAL Main facade with factory methods for opening ports
SERIAL_PORT Port handle for read/write operations
SERIAL_PORT_CONFIG Configuration (baud rate, parity, stop bits, flow control)
SERIAL_PORT_ENUMERATOR Discover available COM ports

Configuration

Use the fluent builder pattern to configure serial port settings:

local
    config: SERIAL_PORT_CONFIG
do
    create config.make_default
    config := config
        .set_baud_rate (115200)
        .set_data_bits (8)
        .set_parity ({SERIAL_PORT_CONFIG}.parity_none)
        .set_stop_bits ({SERIAL_PORT_CONFIG}.stop_bits_one)
        .set_flow_control ({SERIAL_PORT_CONFIG}.flow_control_hardware)

    port := serial.open_port_with_config ("COM3", config)
end

Configuration Constants

Category Constants
Parity parity_none, parity_odd, parity_even, parity_mark, parity_space
Stop Bits stop_bits_one, stop_bits_one_and_half, stop_bits_two
Flow Control flow_control_none, flow_control_hardware, flow_control_software

API Reference

SERIAL_PORT

Feature Description
open: BOOLEAN Open with default config (9600-8-N-1)
open_with_config (config): BOOLEAN Open with custom configuration
close Close the port
write_string (s): BOOLEAN Write string, returns success
write_bytes (bytes): BOOLEAN Write raw bytes, returns success
read_string (max): STRING_8 Read up to max characters
read_bytes (max): ARRAY [NATURAL_8] Read up to max bytes
read_line: STRING_8 Read until newline or timeout
is_open: BOOLEAN Is port currently open?
last_error: detachable STRING_32 Last error message

Port Discovery

-- List all available ports
across serial.available_ports as p loop
    print (p + "%N")
end

-- Find Bluetooth devices (paired via OS)
across serial.bluetooth_ports as p loop
    print ("Bluetooth: " + p + "%N")
end

-- Find USB serial adapters
across serial.usb_ports as p loop
    print ("USB: " + p + "%N")
end

Features

Cross-Platform API

Windows implementation complete, Linux/macOS planned.

Fluent Configuration

Chain configuration calls for clean, readable code.

Port Enumeration

Discover available COM ports, filter by Bluetooth/USB.

Design by Contract

Full preconditions, postconditions, and invariants.