Skip to main content

Class SerialIdentifier

Provides utility for detecting and communicating with serial devices.

public static class SerialIdentifier

Inheritance​

Fields​

IDENTIFICATION_REQUEST_MSG​

The message sent to the serial device to initiate identification.

public const string IDENTIFICATION_REQUEST_MSG = "HELLOARDUINO"

Field Value​

string

RESPONSE_MSG​

The expected response from the serial device that confirms its identity as an Arduino.

public const string RESPONSE_MSG = "ARDUINO"

Field Value​

string

SERIAL_PORT_READ_TIMEOUT​

The number of milliseconds before a timeout occurs for each individual read or write operation on the serial port.

public const int SERIAL_PORT_READ_TIMEOUT = 50

Field Value​

int

SERIAL_PORT_TOTAL_PING_ATTEMPT​

The maximum number of ping attempts (write-read cycles) performed when probing a serial port.

public const int SERIAL_PORT_TOTAL_PING_ATTEMPT = 20

Field Value​

int

Remarks​

Retries are needed because the first reads after opening a port often return nothing, even though the device received the ping. Probing a port stops as soon as the expected response is received.

Methods​

FindArduinoPort()​

Locate the COM port to which the custom Arduino device is connected by sending a predefined identification message and the matching expected response.

public static Task<string> FindArduinoPort()

Returns​

Task<string> — A Task that resolves to the name of the COM port if found; otherwise, an empty string.

Remarks​

The ports are probed on a worker thread, so opening and reading them never blocks the main thread.

FindPortByPing(string, string)​

Searches available serial ports by sending a ping message and checking for an expected response.

public static string FindPortByPing(string pingMsg, string expectedPongMsg)

Parameters​

NameTypeDescription
pingMsgstringThe message to send to the serial port as a handshake ping.
expectedPongMsgstringThe expected response message that confirms the correct device is connected.

Returns​

string — The name of the COM port if a matching device is found; otherwise, an empty string.

Remarks​

This call blocks while the ports are probed; use FindArduinoPort to probe on a worker thread. Uses synchronous reads and writes with timeouts rather than the serial stream's async methods, whose timeout behavior is unreliable on Mono.

  • Fields
    • IDENTIFICATION_REQUEST_MSG
    • RESPONSE_MSG
    • SERIAL_PORT_READ_TIMEOUT
    • SERIAL_PORT_TOTAL_PING_ATTEMPT
  • Methods
    • FindArduinoPort()
    • FindPortByPing(string, string)