Skip to main content

RS485

The RS485 interface provides differential serial communication with configurable baud rates from 300 to 20,000,000 bps. Supports optional termination resistors for proper bus termination.

TIP

As described in the introduction, industrial communication interfaces share GPIOs, meaning only one feature can be enabled at a given time: Either regular GPIOs, or one of the industrial communication interfaces.

RS485_0 shares pins with CAN

The CAN bus and RS485_0 share the same GPIO pins for communication. Therefore, only one of these interfaces can be enabled at a time.

When enabling either the CAN bus or RS485_0, the shared GPIO pins will be automatically configured for the selected interface.

Quick Start​

The RS485 bus supports enabling/disabling, termination resistance, transmitting, and receiving data.

import AT1000 from '@ikalogic/at1000';
let testers = await AT1000.findDevices(500); // Find the device with serial number 12345
let tester = testers[0]; // Target the first detected device
let rs485 = tester.com.rs485(1); // Access RS485_2 bus on AT1000

await rs485.configure({
baud_rate: 9600,
termination_resistors: true,
enabled: true
});

// Start receiving RS485 messages
await rs485.start_rx();

// Transmit a message over RS485
await rs485.tx([0xDE, 0xAD, 0xBE, 0xEF]);

// Receive data (returns an array of bytes)
let data_rx = await rs485.rx();

// Print received bytes
console.log("RS485 Data Received:", data_rx);

// Disable RS485_2 bus
await rs485.stop_rx();

API Reference​

Accessing RS485​

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

Parameters:

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

Returns: Rs485 instance


Methods​

configure(config)​

Configures the RS485 interface parameters without changing the enabled state.

await rs485.configure({ 
baud_rate: 115200,
termination_resistors: true
});

Parameters:

  • config (Rs485ConfigPatch): Configuration object with optional properties:
    • enabled (boolean, optional): Enable or disable the interface
    • baud_rate (number, optional): Baud rate (300 to 20000000)
    • termination_resistors (boolean, optional): Enable/disable 120Ω termination resistors

Returns: Promise<Rs485Config> - The updated configuration

Exceptions:

  • Throws validation error if baud_rate is outside the range [300, 20000000]
  • Throws validation error if parameters don't match expected types

enable([config])​

Enables the RS485 interface and optionally applies configuration.

await rs485.enable({ 
baud_rate: 9600,
termination_resistors: false
});

Parameters:

  • config (Rs485EnableConfig, optional): Optional configuration to apply:
    • baud_rate (number, optional): Baud rate (300 to 20000000)
    • termination_resistors (boolean, optional): Enable/disable termination resistors

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

Exceptions:

  • Throws validation error if baud_rate is outside the range [300, 20000000]

disable()​

Disables the RS485 interface.

await rs485.disable();

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


tx(data)​

Transmits data over the RS485 interface.

// Send string
await rs485.tx("Hello, World!");

// Send byte array
await rs485.tx([0x48, 0x65, 0x6C, 0x6C, 0x6F]);

Parameters:

  • data (SerialDataInput): Data to transmit, either:
    • string: Text data (max 1024 characters)
    • number[]: Byte array (each byte 0-255, max 1024 bytes)

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

Exceptions:

  • Throws validation error if string exceeds 1024 characters
  • Throws validation error if array exceeds 1024 elements
  • Throws validation error if any byte value is outside the range [0, 255]

start_rx()​

Starts buffering received data from the RS485 interface.

await rs485.start_rx();

Returns: Promise<void>

Note: Data received after calling this method will be buffered and can be retrieved using rx().


stop_rx()​

Stops buffering received data from the RS485 interface.

await rs485.stop_rx();

Returns: Promise<void>


rx()​

Reads all buffered data received since start_rx() was called.

const receivedData = await rs485.rx();
console.log(receivedData); // number[]

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

Note: This method returns all data accumulated since the last start_rx() call and clears the buffer.


Types​

Rs485Config​

Complete configuration object returned by configuration methods.

{
enabled: boolean,
baud_rate: number, // 300 to 20000000
termination_resistors: boolean
}

Rs485ConfigPatch​

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

{
enabled?: boolean,
baud_rate?: number, // 300 to 20000000
termination_resistors?: boolean
}

Rs485EnableConfig​

Optional configuration for the enable() method.

{
baud_rate?: number, // 300 to 20000000
termination_resistors?: boolean
}

SerialDataInput​

Input data type for transmission.

string | number[]
  • string: Maximum 1024 characters
  • number[]: Maximum 1024 elements, each byte in range [0, 255]

SerialData​

Output data type, always returned as a byte array.

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

Usage Example​

import AT1000 from '@ikalogic/at1000';

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

// Access RS485 interface 0
const rs485 = tester.com.rs485(0);

// Enable with 115200 baud rate and termination resistors
await rs485.enable({
baud_rate: 115200,
termination_resistors: true
});

// Start receiving data
await rs485.start_rx();

// Transmit data
await rs485.tx([0x01, 0x03, 0x00, 0x00, 0x00, 0x0A]);

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

// Read received data
const response = await rs485.rx();
console.log("Received:", response);

// Stop receiving
await rs485.stop_rx();

// Disable interface
await rs485.disable();

Common Patterns​

Modbus RTU Communication​

// Configure for Modbus (typically 9600, 19200, or 115200)
await rs485.enable({
baud_rate: 9600,
termination_resistors: true
});

// Function to calculate CRC16 (Modbus)
function crc16(buffer) {
let crc = 0xFFFF;
for (let byte of buffer) {
crc ^= byte;
for (let i = 0; i < 8; i++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ 0xA001;
} else {
crc >>= 1;
}
}
}
return [crc & 0xFF, (crc >> 8) & 0xFF];
}

// Send Modbus request
const frame = [0x01, 0x03, 0x00, 0x00, 0x00, 0x0A];
const crc = crc16(frame);
await rs485.tx([...frame, ...crc]);

Half-Duplex Communication​

// RS485 is half-duplex - send then receive
await rs485.start_rx();

// Send query
await rs485.tx("QUERY\r\n");

// Wait for device to respond
await new Promise(resolve => setTimeout(resolve, 50));

// Read response
const response = await rs485.rx();

await rs485.stop_rx();

Multi-Drop Network​

// Configure with termination resistors only on end nodes
const isEndNode = true;
await rs485.enable({
baud_rate: 115200,
termination_resistors: isEndNode
});

// Address specific devices on the bus
const deviceAddress = 0x05;
await rs485.tx([deviceAddress, 0x10, 0x00, 0x01]);