RpLidarSerialDevice

Namespace: RpLidar.NET

Low-level serial device implementation of ILidarService with full protocol control.

Table of Contents

  1. Definition
  2. Constructors
    1. RpLidarSerialDevice(ILidarSettings settings)
  3. Events
    1. LidarPointScanEvent
    2. LidarPointGroupScanEvent
  4. Methods — Lifecycle
    1. Connect()
    2. Disconnect()
    3. Start()
    4. Stop()
    5. Dispose()
  5. Methods — Motor Control
    1. StartMotor()
    2. StopMotor()
  6. Methods — Scanning
    1. StartScan()
    2. ForceScan()
    3. ForceScanExpress()
    4. StopScan()
  7. Methods — Protocol
    1. SendRequest(Command command)
    2. SendCommand(byte command, byte[] data = null)
    3. Read(int len, int timeout)
  8. Remarks
  9. See Also

Definition

public class RpLidarSerialDevice : ILidarService

RpLidarSerialDevice implements the complete SLAMTEC serial protocol v2.1. It gives you explicit control over connection, disconnection, scan mode, motor speed, and request/response cycles.


Constructors

RpLidarSerialDevice(ILidarSettings settings)

Creates a new device instance. Does not connect to the port until Connect() is called.

Parameter Type Description
settings ILidarSettings Configuration: port, baud rate, scan mode, PWM, etc.
var settings = new LidarSettings
{
    Port     = "/dev/ttyUSB0",
    ScanMode = ScanMode.Standard
};
var device = new RpLidarSerialDevice(settings);

Events

LidarPointScanEvent

public event LidarPointScanEvenHandler LidarPointScanEvent;

Fired every settings.ElapsedMilliseconds (default 400 ms) with raw points from the current scan window.

LidarPointGroupScanEvent

public event LidarPointGroupScanEvenHandler LidarPointGroupScanEvent;

Fired alongside LidarPointScanEvent with the same points wrapped in a LidarPointGroup (angle-indexed at 0.1° resolution).


Methods — Lifecycle

Connect()

public void Connect()

Opens the serial port and initialises the device. Call before any other method.


Disconnect()

public void Disconnect()

Closes the serial port. Call after Stop().


Start()

public void Start()

Convenience method that:

  1. Calls Connect() (if not already connected)
  2. Sends StopScan
  3. Detects the scan mode from settings.ScanMode
  4. Starts the appropriate scan command
  5. Launches the background scan thread

Stop()

public void Stop()

Sends StopScan and terminates the background scan thread.


Dispose()

public void Dispose()

Stops the scan, stops the motor, disconnects, and releases all resources.


Methods — Motor Control

StartMotor()

public void StartMotor()

Sends the motor-start PWM command using settings.Pwm (default 660).


StopMotor()

public void StopMotor()

Sends the motor-stop command (PWM = 0). The sensor will stop rotating.


Methods — Scanning

StartScan()

public void StartScan()

Sends the standard Scan command (0x20). Compatible with all supported models.


ForceScan()

public void ForceScan()

Sends the ForceScan command (0x21). Bypasses the motor-sync check — use when motor synchronisation is not needed.


ForceScanExpress()

public void ForceScanExpress()

Sends the ExpressScan command using settings.ScanMode. Issues the Boost (working_mode=3) or Sensitivity (working_mode=4) sub-mode as appropriate. Only supported on A2M8 and A3M1.


StopScan()

public void StopScan()

Sends the Stop command (0x25) to halt the current scan without stopping the motor.


Methods — Protocol

SendRequest(Command command)

public IDataResponse SendRequest(Command command)

Sends a single-response command and returns the parsed response.

Parameter Type Description
command Command The protocol command to send

Returns IDataResponse — cast to InfoDataResponse or RpHealthResponse as appropriate.

var info = device.SendRequest(Command.GetInfo) as InfoDataResponse;
Console.WriteLine(info?.ModelId);

var health = device.SendRequest(Command.GetHealth) as RpHealthResponse;
Console.WriteLine(health?.Status); // 0=OK, 1=Warning, 2=Error

SendCommand(byte command, byte[] data = null)

public IDataResponse SendCommand(byte command, byte[] data = null)

Low-level raw command send. Prefer SendRequest with the Command enum for standard commands.


Read(int len, int timeout)

public byte[] Read(int len, int timeout)

Reads exactly len bytes from the serial port, waiting up to timeout milliseconds.


Remarks

  • Call Connect() before any other method (or call Start() which calls it internally).
  • All events fire on a background thread. Do not update UI controls directly from event handlers.
  • To change scan mode after construction, create a new RpLidarSerialDevice with updated settings.

See Also


Copyright © 2024 Asheesh Maheshwari. Distributed under the MIT license.

This site uses Just the Docs, a documentation theme for Jekyll.