134 lines
3.7 KiB
C#
134 lines
3.7 KiB
C#
using Sick.Tim781s.Colab;
|
|
using Sick.Tim781s.Colab.Commands;
|
|
using Sick.Tim781s.Colab.Communication;
|
|
|
|
namespace Sick.Tim781s.Colab;
|
|
|
|
/// <summary>
|
|
/// Main class for communicating with SICK TiM781s scanner using ColaB protocol
|
|
/// </summary>
|
|
public sealed class Tim781sScanner : IDisposable
|
|
{
|
|
private readonly ColaBSession _session;
|
|
private readonly TcpClient _tcpClient;
|
|
private bool _disposed;
|
|
|
|
public Tim781sScanner(string ipAddress, ushort port = 2112)
|
|
{
|
|
_tcpClient = new TcpClient(ipAddress, port);
|
|
_session = new ColaBSession(_tcpClient);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Connects to the scanner
|
|
/// </summary>
|
|
public void Connect(int timeoutMs = 5000)
|
|
{
|
|
_session.Open(timeoutMs);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disconnects from the scanner
|
|
/// </summary>
|
|
public void Disconnect()
|
|
{
|
|
_session.Close();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets whether the scanner is connected
|
|
/// </summary>
|
|
public bool IsConnected => _session.IsOpen;
|
|
|
|
/// <summary>
|
|
/// Reads device identification
|
|
/// </summary>
|
|
public string? ReadDeviceIdent(int timeoutMs = 5000)
|
|
{
|
|
var command = new ReadCommand("DeviceIdent");
|
|
_session.ExecuteCommand(command, timeoutMs);
|
|
return command.ReplyValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads serial number
|
|
/// </summary>
|
|
public string? ReadSerialNumber(int timeoutMs = 5000)
|
|
{
|
|
var command = new ReadCommand("SerialNumber");
|
|
_session.ExecuteCommand(command, timeoutMs);
|
|
return command.ReplyValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads firmware version
|
|
/// </summary>
|
|
public string? ReadFirmwareVersion(int timeoutMs = 5000)
|
|
{
|
|
var command = new ReadCommand("FirmwareVersion");
|
|
_session.ExecuteCommand(command, timeoutMs);
|
|
return command.ReplyValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts measurement
|
|
/// </summary>
|
|
public bool StartMeasurement(int timeoutMs = 5000)
|
|
{
|
|
var command = new MethodCommand("mNEVAChangeState", "1"); // 1 = measurement mode
|
|
_session.ExecuteCommand(command, timeoutMs);
|
|
return command.WasSuccessful;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stops measurement
|
|
/// </summary>
|
|
public bool StopMeasurement(int timeoutMs = 5000)
|
|
{
|
|
var command = new MethodCommand("mNEVAChangeState", "0"); // 0 = standby mode
|
|
_session.ExecuteCommand(command, timeoutMs);
|
|
return command.WasSuccessful;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes a custom read command
|
|
/// </summary>
|
|
public string? ExecuteRead(string variableName, int timeoutMs = 5000)
|
|
{
|
|
var command = new ReadCommand(variableName);
|
|
_session.ExecuteCommand(command, timeoutMs);
|
|
return command.ReplyValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes a custom write command
|
|
/// </summary>
|
|
public bool ExecuteWrite(string variableName, string value, int timeoutMs = 5000)
|
|
{
|
|
var command = new WriteCommand(variableName, value);
|
|
_session.ExecuteCommand(command, timeoutMs);
|
|
return command.WasSuccessful;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes a custom method command
|
|
/// </summary>
|
|
public string? ExecuteMethod(string methodName, params string[] parameters)
|
|
{
|
|
var command = new MethodCommand(methodName, parameters);
|
|
_session.ExecuteCommand(command);
|
|
return command.ReplyValue;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_disposed)
|
|
return;
|
|
|
|
_session?.Dispose();
|
|
_tcpClient?.Dispose();
|
|
_disposed = true;
|
|
}
|
|
}
|
|
|