Skip to content

API Reference

Max Litruv Boonzaayer edited this page Feb 15, 2026 · 4 revisions

API Reference

Complete API documentation for the MeshCS library.

Classes Overview

Class Description
MeshRadio High-level async API for radio communication
PacketBuilder Static methods to build command packets
PacketParser Static methods to parse response packets
SelfInfo Device self-information
DeviceInfo Firmware and model information
Contact Contact entry from device
Channel Channel configuration
DirectMessage Received direct message
ChannelMessage Received channel message
Advert Received advertisement
BatteryInfo Battery status information
ScanResult Result of port scanning

MeshRadio

High-level async API for communicating with a MeshCore companion radio.

Static Methods

GetAvailablePorts

public static string[] GetAvailablePorts()

Returns all available serial port names on the system.

ScanAllPortsAsync

public static async Task<List<ScanResult>> ScanAllPortsAsync(
    int timeoutMs = 2000,
    int baudRate = 115200,
    bool verbose = false,
    CancellationToken ct = default)

Scans all serial ports for MeshCore devices.

ScanPortAsync

public static async Task<ScanResult> ScanPortAsync(
    string portName,
    int timeoutMs = 2000,
    int baudRate = 115200,
    bool verbose = false,
    CancellationToken ct = default)

Scans a specific serial port for a MeshCore device.

FindFirstDeviceAsync

public static async Task<ScanResult?> FindFirstDeviceAsync(
    int timeoutMs = 2000,
    int baudRate = 115200,
    bool verbose = false,
    CancellationToken ct = default)

Finds the first available MeshCore device.

ConnectFirstAsync

public static async Task<MeshRadio?> ConnectFirstAsync(
    string appName = "MeshCore",
    int timeoutMs = 2000,
    int baudRate = 115200,
    bool verbose = false,
    CancellationToken ct = default)

Creates and connects to the first available device.

Constructor

public MeshRadio(string portName, int baudRate = 115200)

Creates a new MeshRadio instance for the specified port.

Properties

Property Type Description
Self SelfInfo? Device self-info (populated after Connect)
Device DeviceInfo? Device info (populated after DeviceQuery)
IsConnected bool Whether the serial port is open
PendingMessageCount int Number of queued direct messages
PendingChannelMessageCount int Number of queued channel messages
VerboseLogging bool Enable console logging

Connection Methods

ConnectAsync

public async Task ConnectAsync(string appName = "MeshCore", CancellationToken ct = default)

Opens the connection and initializes the companion session.

Disconnect

public void Disconnect()

Closes the serial connection.

Dispose

public void Dispose()

Releases all resources.

Device Methods

DeviceQueryAsync

public async Task<DeviceInfo?> DeviceQueryAsync(CancellationToken ct = default)

Gets device firmware and model information.

GetTimeAsync

public async Task<DateTime?> GetTimeAsync(CancellationToken ct = default)

Gets the device's current time.

SetTimeAsync

public async Task<bool> SetTimeAsync(CancellationToken ct = default)

Syncs device time to system time.

GetBatteryAsync

public async Task<BatteryInfo?> GetBatteryAsync(CancellationToken ct = default)

Gets battery voltage and percentage.

SetNameAsync

public async Task<bool> SetNameAsync(string name, CancellationToken ct = default)

Sets the device's advertised name.

SetCoordsAsync

public async Task<bool> SetCoordsAsync(double latitude, double longitude, CancellationToken ct = default)

Sets the device's GPS coordinates.

SetTxPowerAsync

public async Task<bool> SetTxPowerAsync(sbyte powerDbm, CancellationToken ct = default)

Sets transmit power in dBm.

SendAdvertAsync

public async Task<bool> SendAdvertAsync(CancellationToken ct = default)

Broadcasts a self-advertisement.

RebootAsync

public async Task RebootAsync(CancellationToken ct = default)

Reboots the device.

FactoryResetAsync

public async Task FactoryResetAsync(CancellationToken ct = default)

Factory resets the device (erases all data).

Messaging Methods

SendMessageAsync

public async Task<bool> SendMessageAsync(
    byte[] recipientPubKeyPrefix,
    string text,
    TextType textType = TextType.Plain,
    CancellationToken ct = default)

Sends a direct message to a contact.

SendMessageRawAsync

public async Task<bool> SendMessageRawAsync(
    byte[] recipientPubKeyPrefix,
    byte[] payload,
    TextType textType = TextType.CliData,
    CancellationToken ct = default)

Sends a direct message with raw binary payload.

SendChannelMessageAsync

public async Task<bool> SendChannelMessageAsync(
    byte channelIndex,
    string text,
    CancellationToken ct = default)

Sends a message to a channel.

GetNextMessageAsync

public async Task<DirectMessage?> GetNextMessageAsync(CancellationToken ct = default)

Fetches the next queued message from the device.

TryDequeueMessage

public bool TryDequeueMessage(out DirectMessage? message)

Dequeues a pushed direct message from the local queue.

TryDequeueChannelMessage

public bool TryDequeueChannelMessage(out ChannelMessage? message)

Dequeues a pushed channel message from the local queue.

Contact Methods

GetContactsAsync

public async Task<List<Contact>> GetContactsAsync(uint since = 0, CancellationToken ct = default)

Gets all contacts from the device.

AddContactAsync

public async Task<bool> AddContactAsync(
    byte[] publicKey,
    string name,
    ContactType type = ContactType.Chat,
    CancellationToken ct = default)

Adds or updates a contact.

RemoveContactAsync

public async Task<bool> RemoveContactAsync(byte[] publicKeyPrefix, CancellationToken ct = default)

Removes a contact.

Channel Methods

GetChannelAsync

public async Task<Channel?> GetChannelAsync(byte channelIndex, CancellationToken ct = default)

Gets channel configuration.

SetChannelAsync

public async Task<bool> SetChannelAsync(
    byte channelIndex,
    string name,
    byte[] secret,
    CancellationToken ct = default)

Configures a channel.

Events

See Events for complete event documentation.


PacketBuilder

Static class for building MeshCore command packets.

Methods

Method Description
AppStart(appName, targetVersion) Initialize companion session
DeviceQuery(targetVersion) Query device info
SendMsg(recipient, text, textType, timestamp) Build DM packet
SendMsgRaw(recipient, payload, textType, timestamp) Build raw DM packet
SendChannelMsg(channelIndex, text, textType, timestamp) Build channel message
GetMsg() Fetch next message
GetContacts(since) Request contacts
UpdateContact(publicKey, name, type, flags) Add/update contact
RemoveContact(publicKeyPrefix) Remove contact
GetChannel(channelIndex) Get channel config
SetChannel(channelIndex, name, secret) Set channel config
GetTime() Get device time
SetTime(unixTime) Set device time
SetTimeNow() Set to current time
GetBattery() Get battery info
SendAdvert() Broadcast advertisement
SetName(name) Set device name
SetCoords(latitude, longitude) Set GPS coordinates
SetTxPower(powerDbm) Set TX power
Reboot() Reboot device
FactoryReset() Factory reset

PacketParser

Static class for parsing MeshCore response packets.

Methods

Method Description
GetCode(packet) Get response code
GetBaseCode(packet) Get base code (strips push bit)
IsPush(packet) Check if push notification
ParseSelfInfo(packet) Parse SelfInfo response
ParseDeviceInfo(packet) Parse DeviceInfo response
ParseContact(packet) Parse Contact response
ParseChannelInfo(packet) Parse Channel response
ParseDirectMessage(packet) Parse DM (V2 or V3)
ParseChannelMessage(packet) Parse channel message
ParseAdvert(packet) Parse advertisement
ParseTime(packet) Parse time response
ParseBatteryInfo(packet) Parse battery response
IsOk(packet) Check for OK response
IsError(packet) Check for error response
IsSent(packet) Check for sent acknowledgment
IsNoMoreMessages(packet) Check for empty queue
IsMessageWaiting(packet) Check for message waiting push

Model Classes

SelfInfo

Device self-information returned after connection.

Property Type Description
PublicKey byte[32] Device public key
Name string Advertised name
Latitude int Latitude (millionths of degrees)
Longitude int Longitude (millionths of degrees)
Frequency uint Radio frequency in Hz
Bandwidth byte LoRa bandwidth (0-9)
SpreadingFactor byte LoRa SF (6-12)
CodingRate byte LoRa CR (5-8)
TxPower sbyte TX power in dBm
PublicKeyHex string Full public key as hex
PublicKeyPrefix string First 6 bytes as hex

DeviceInfo

Firmware and model information.

Property Type Description
FirmwareVersion string Firmware version string
Model string Device model/type
MaxPacketSize ushort Max packet size
ProtocolVersion byte Protocol version

Contact

Contact entry from the device.

Property Type Description
PublicKey byte[32] Contact's public key
Type ContactType Chat, Room, or Repeater
Flags byte Contact flags
PathLen byte Path length
Path byte[] Routing path
Name string Display name
LastSeen uint Unix timestamp
PublicKeyHex string Full key as hex
PublicKeyPrefix string First 6 bytes as hex

Channel

Channel configuration.

Property Type Description
Index byte Channel index (0-7)
Name string Channel name
Secret byte[16] Channel secret
ForwardEnabled bool Forward messages
SecretHex string Secret as hex

DirectMessage

Received direct message.

Property Type Description
SenderPrefix byte[6] Sender's key prefix
TextType TextType Plain or CliData
PathLen byte Hop count
Timestamp uint Sender's timestamp
Snr sbyte Signal-to-noise (V3)
Payload byte[] Raw payload
SenderName string Sender name (V3)
SenderPrefixHex string Prefix as hex
Text string Payload as UTF-8
IsV3 bool V3 format flag

ChannelMessage

Received channel message.

Property Type Description
ChannelIndex byte Channel index
TextType TextType Plain or CliData
PathLen byte Hop count
Timestamp uint Sender's timestamp
Snr sbyte Signal-to-noise (V3)
Payload byte[] Raw payload
SenderName string Sender name (V3)
Text string Payload as UTF-8
IsV3 bool V3 format flag

Advert

Received advertisement from the mesh.

Property Type Description
PublicKey byte[32] Sender's public key
Name string Advertised name
Latitude int Latitude (millionths)
Longitude int Longitude (millionths)
PathLen byte Hop count
Snr sbyte Signal-to-noise
PublicKeyHex string Key as hex

BatteryInfo

Battery status information.

Property Type Description
VoltageMillivolts ushort Battery voltage
Percentage byte 0-100%
IsCharging bool Charging status
FreeStorageBytes uint Free storage

ScanResult

Port scan result.

Property Type Description
PortName string Port name
Found bool Device found
Self SelfInfo? Device info
Device DeviceInfo? Firmware info
Error string? Error message

Enums

TextType

public enum TextType : byte
{
    Plain = 0x00,   // Plain text
    CliData = 0x01  // CLI/binary data
}

ContactType

public enum ContactType : byte
{
    Room = 0x00,     // Group/room
    Chat = 0x01,     // Direct message
    Repeater = 0x02  // Repeater node
}

Clone this wiki locally