Skip to main content

UART

The UART (Universal Asynchronous Receiver/Transmitter) interface provides asynchronous serial communication. Supports configurable baud rates from 300 to 3,000,000 bps with various data formats and voltage levels from 1.6V to 5.0V.

Multiplexing

Some interfaces share the same GPIO pins, so only one can be used at a time. Check out the multiplexing tables in the introduction for more details.

Quick Start​

The example code below shows how to enable the UART interface, configure it, and transmit/receive data.

import AT1000 from '@ikalogic/at1000';
let devices = await AT1000.findDevices(500); // Find devices with a 500ms timeout
let tester = devices[0]; // Target the first detected device
let uart = tester.com.uart(0); // Select the first UART interface

await uart.enable({
vcc: 3.3,
baud_rate: 115200,
data_bits: 8,
parity: 'none',
stop_bits: 1
}); // Enable UART with standard 115200 8N1 configuration

// Transmit data
await uart.tx("Hello, World!\r\n");

// Start continuous receive
await uart.start_rx();

// Wait for some data
await new Promise(resolve => setTimeout(resolve, 100));

// Read received data
let data = await uart.rx();
console.log("Received:", data);

// Stop reception
await uart.stop_rx();

// Disable UART interface
await uart.disable();

API Reference​

Accessing UART​

const uart = tester.com.uart(id);

Parameters:

  • id (number): The UART interface identifier (0-based index)

Returns: Uart instance


Methods​

configure(config)​

Configures the UART interface parameters without changing the enabled state.

await uart.configure({ 
baud_rate: 9600,
data_bits: 8,
parity: 'none',
stop_bits: 1,
vcc: 3.3
});

Parameters:

  • config (UartConfigPatch): Configuration object with optional properties:
    • enabled (boolean, optional): Enable or disable the interface
    • baud_rate (number, optional): Baud rate (300 to 3,000,000)
    • data_bits (number, optional): Data bits per character (5, 6, 7, 8, or 9)
    • parity (string, optional): Parity mode - 'none', 'even', or 'odd'
    • stop_bits (number, optional): Stop bits (1 or 2)
    • vcc (number, optional): Logic level voltage (1.6 to 5.0)

Returns: Promise<UartConfig> - The updated configuration

Exceptions:

  • Throws validation error if baud_rate is outside [300, 3000000]
  • Throws validation error if data_bits is not one of [5, 6, 7, 8, 9]
  • Throws validation error if parity is not 'none', 'even', or 'odd'
  • Throws validation error if stop_bits is not 1 or 2
  • Throws validation error if vcc is outside [1.6, 5.0]

enable([config])​

Enables the UART interface and optionally applies configuration.

await uart.enable({ 
baud_rate: 115200,
data_bits: 8,
parity: 'none',
stop_bits: 1,
vcc: 3.3
});

Parameters:

  • config (UartEnableConfig, optional): Optional configuration to apply

Returns: Promise<UartConfig> - The updated configuration with enabled: true


disable()​

Disables the UART interface.

await uart.disable();

Returns: Promise<UartConfig> - The updated configuration with enabled: false


tx(data)​

Transmits data over the UART interface.

// Transmit string data
await uart.tx("Hello\r\n");

// Transmit raw bytes
await uart.tx([0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x0D, 0x0A]);

Parameters:

  • data (string | number[]): Data to transmit (up to 1024 bytes)

Returns: Promise<number> - Number of bytes transmitted

Exceptions:

  • Throws validation error if data exceeds 1024 bytes

start_rx()​

Starts continuous reception. Data is buffered internally until read with rx().

await uart.start_rx();

Returns: Promise<void>


stop_rx()​

Stops continuous reception.

await uart.stop_rx();

Returns: Promise<void>


rx()​

Reads received data from the internal buffer.

// Read all available data
const data = await uart.rx();
console.log("Received:", data);

Returns: Promise<SerialData> - Received data as byte array (number[])

Notes:

  • Returns immediately with available data (non-blocking)
  • Clears the receive buffer after reading
  • Must call start_rx() before receiving data

Types​

UartConfig​

Complete configuration object returned by configuration methods.

{
enabled: boolean,
baud_rate: number, // 300 to 3,000,000
data_bits: number, // 5, 6, 7, 8, or 9
parity: 'none' | 'even' | 'odd',
stop_bits: number, // 1 or 2
vcc: number // 1.6 to 5.0
}

UartConfigPatch​

Partial configuration for the configure() method. All properties are optional.

{
enabled?: boolean,
baud_rate?: number,
data_bits?: number,
parity?: 'none' | 'even' | 'odd',
stop_bits?: number,
vcc?: number
}

UartEnableConfig​

Optional configuration for the enable() method.

{
baud_rate?: number,
data_bits?: number,
parity?: 'none' | 'even' | 'odd',
stop_bits?: number,
vcc?: number
}

SerialData​

Output data type, always returned as a byte array.

number[]  // Array of bytes (0-255)

Common Baud Rates​

Baud RateDescription
300Low-speed legacy
1200Legacy modems
2400Legacy modems
4800Legacy devices
9600Common default
19200Common
38400Common
57600Common
115200Most common high-speed
230400High-speed
460800High-speed
921600Very high-speed
10000001 Mbps
20000002 Mbps
30000003 Mbps (maximum)

Usage Example​

import AT1000 from '@ikalogic/at1000';

// Find and connect to device
const devices = await AT1000.findDevices(500);
const tester = devices[0];

// Access UART interface 0
const uart = tester.com.uart(0);

// Enable with standard 115200 8N1 configuration
await uart.enable({
baud_rate: 115200,
data_bits: 8,
parity: 'none',
stop_bits: 1,
vcc: 3.3
});

// Start continuous reception
await uart.start_rx();

// Send a command
await uart.tx("AT\r\n");

// Wait for response
await new Promise(resolve => setTimeout(resolve, 100));

// Read response
const response = await uart.rx();
console.log("Response:", Buffer.from(response).toString());

// Stop reception and disable
await uart.stop_rx();
await uart.disable();

Common Patterns​

AT Command Interface​

async function sendATCommand(uart, command, timeout = 1000) {
// Clear any pending data
await uart.rx();

// Send command
await uart.tx(command + "\r\n");

// Wait for response
await new Promise(resolve => setTimeout(resolve, timeout));

// Read response
const response = await uart.rx();
return Buffer.from(response).toString().trim();
}

// Usage
await uart.start_rx();

const info = await sendATCommand(uart, "ATI");
console.log("Device info:", info);

const signal = await sendATCommand(uart, "AT+CSQ");
console.log("Signal quality:", signal);

GPS NMEA Receiver​

// Configure for GPS module (9600 baud typical)
await uart.enable({
baud_rate: 9600,
data_bits: 8,
parity: 'none',
stop_bits: 1,
vcc: 3.3
});

await uart.start_rx();

// Read NMEA sentences
let buffer = '';

while (true) {
const data = await uart.rx();
if (data.length > 0) {
buffer += Buffer.from(data).toString();

// Process complete sentences
const lines = buffer.split('\r\n');
buffer = lines.pop(); // Keep incomplete sentence

for (const line of lines) {
if (line.startsWith('$GPGGA')) {
console.log("Position:", line);
} else if (line.startsWith('$GPRMC')) {
console.log("Navigation:", line);
}
}
}
await new Promise(resolve => setTimeout(resolve, 100));
}

Binary Protocol​

// Send binary command with header and checksum
function buildPacket(command, data) {
const packet = [0xAA, 0x55, command, data.length, ...data];
const checksum = packet.reduce((a, b) => a ^ b, 0);
return [...packet, checksum];
}

// Read binary response
async function readPacket(uart, timeout = 1000) {
const startTime = Date.now();
let buffer = [];

while (Date.now() - startTime < timeout) {
const data = await uart.rx();
buffer.push(...data);

// Check for complete packet
if (buffer.length >= 4 && buffer[0] === 0xAA && buffer[1] === 0x55) {
const length = buffer[3];
if (buffer.length >= 5 + length) {
return buffer.slice(0, 5 + length);
}
}

await new Promise(resolve => setTimeout(resolve, 10));
}

throw new Error("Timeout waiting for packet");
}

// Usage
const packet = buildPacket(0x01, [0x10, 0x20]);
await uart.tx(packet);
const response = await readPacket(uart);

Line-Based Protocol​

class LineReader {
constructor(uart) {
this.uart = uart;
this.buffer = '';
}

async readLine(timeout = 5000) {
const startTime = Date.now();

while (Date.now() - startTime < timeout) {
const data = await this.uart.rx();
this.buffer += Buffer.from(data).toString();

const newlineIndex = this.buffer.indexOf('\n');
if (newlineIndex !== -1) {
const line = this.buffer.substring(0, newlineIndex).replace('\r', '');
this.buffer = this.buffer.substring(newlineIndex + 1);
return line;
}

await new Promise(resolve => setTimeout(resolve, 10));
}

throw new Error("Timeout waiting for line");
}

async writeLine(text) {
await this.uart.tx(text + '\r\n');
}
}

// Usage
const reader = new LineReader(uart);
await reader.writeLine("GET STATUS");
const status = await reader.readLine();
console.log("Status:", status);

Echo Test​

// Loop TX to RX for echo test
async function echoTest(uart, testData) {
await uart.start_rx();

// Send test data
await uart.tx(testData);

// Wait for echo
await new Promise(resolve => setTimeout(resolve, 100));

// Read response
const received = await uart.rx();

// Compare
const sent = typeof testData === 'string'
? Array.from(Buffer.from(testData))
: testData;

const match = sent.length === received.length &&
sent.every((v, i) => v === received[i]);

console.log(`Echo test: ${match ? 'PASS' : 'FAIL'}`);
return match;
}

await echoTest(uart, "Hello, World!");

Notes​

  • The UART interface supports TTL-level signals only (not RS-232 voltage levels)
  • Use appropriate level shifters for 5V logic when vcc is set higher
  • Flow control (RTS/CTS) is not supported
  • The receive buffer has limited capacity; read frequently to avoid overflow
  • For very high baud rates (> 1 Mbps), ensure signal integrity and short cable lengths
  • Always call start_rx() before expecting to receive data
  • The maximum single transmission is 1024 bytes