Table of Contents

Class OpnDevice

Namespace
Opticon
Assembly
Opticon.csp2.dll

High-level wrapper around the Opticon.csp2 API for Opticon OPN Companion devices.

public sealed class OpnDevice : IDisposable
Inheritance
OpnDevice
Implements
Inherited Members

Remarks

This class simplifies usage of the low-level API functions by providing a more intuitive object-oriented interface.

Constructors

OpnDevice(int)

Initializes a new instance of the OpnDevice class.

public OpnDevice(int port)

Parameters

port int

The COM port number (e.g. 3 for COM3).

Properties

IsConnected

Checks whether data is available to be read from the device.

public bool IsConnected { get; }

Property Value

bool

true if data is available; otherwise false.

IsDataAvailable

Checks whether data is available to be read from the device.

public bool IsDataAvailable { get; }

Property Value

bool

true if data is available; otherwise false.

Port

Gets the COM port number associated with this device.

public int Port { get; }

Property Value

int

Methods

ClearBarcodes()

Clears all barcode data stored on the device.

public void ClearBarcodes()

Exceptions

OpnException

When the command fails.

Connect()

Opens the connection to the device.

public void Connect()

Exceptions

OpnException

Thrown when the connection fails.

DisablePolling()

Temporarily pauses polling.

public static int DisablePolling()

Returns

int

Disconnect()

Closes the connection to the device.

public void Disconnect()

Exceptions

OpnException

Thrown when the disconnection fails.

Dispose()

Releases the device and closes the connection.

public void Dispose()

Exceptions

OpnException

Thrown when the disconnection fails.

EnablePolling()

Resumes polling if previously paused.

public static int EnablePolling()

Returns

int

EnumerateBarcodes()

Enumerates barcode data as a stream.

public IEnumerable<BarcodeRecord> EnumerateBarcodes()

Returns

IEnumerable<BarcodeRecord>

An enumerable sequence of barcode data packets.

Remarks

This method allows lazy iteration over barcode data.

FindDevices()

Scans for connected Opticon OPN Companion devices.

public static IReadOnlyList<OpnDevice> FindDevices()

Returns

IReadOnlyList<OpnDevice>

A list of detected OpnDevice instances.

Remarks

This method queries all available COM ports and returns those compatible with Opticon devices.

GetDeviceId()

Retrieves the unique device ID. Returns cached value if disconnected.

public string GetDeviceId()

Returns

string

The 6-digit or 7-digit serial number.

Exceptions

OpnException

When the command fails and no cached data exists.

GetModel()

Retrieves the device model name based on the software version string.

public string GetModel()

Returns

string

The model name (e.g., "OPN2001", "PX20").

Exceptions

OpnException

When the interrogate command or data retrieval fails.

GetParameter(OpnParameter, out bool)

Gets a parameter as a boolean value. Throws an exception if the operation fails.

public void GetParameter(OpnParameter parameter, out bool enabled)

Parameters

parameter OpnParameter

The parameter to retrieve.

enabled bool

The retrieved boolean value.

Exceptions

OpnException

Thrown if the underlying hardware call fails.

GetParameter(OpnParameter, out byte[])

Gets a parameter as raw byte data. Throws an exception if the operation fails.

public void GetParameter(OpnParameter parameter, out byte[] data)

Parameters

parameter OpnParameter

The parameter to retrieve.

data byte[]

The retrieved raw byte data.

Exceptions

OpnException

Thrown if the underlying hardware call fails.

GetParameter(OpnParameter, out int)

Gets a parameter as an integer value. Throws an exception if the operation fails.

public void GetParameter(OpnParameter parameter, out int value)

Parameters

parameter OpnParameter

The parameter to retrieve.

value int

The retrieved integer value.

Exceptions

OpnException

Thrown if the underlying hardware call fails.

GetParameter(OpnParameter, out string)

Gets a parameter as a string. Throws an exception if the operation fails.

public void GetParameter(OpnParameter parameter, out string value)

Parameters

parameter OpnParameter

The parameter to retrieve.

value string

The retrieved string value.

Exceptions

OpnException

Thrown if the underlying hardware call fails.

GetParameter<TEnum>(OpnParameter, out TEnum)

Gets a parameter as an enumeration value. Performs validation for defined members.

public void GetParameter<TEnum>(OpnParameter parameter, out TEnum value) where TEnum : struct, Enum

Parameters

parameter OpnParameter

The parameter to retrieve.

value TEnum

The retrieved enumeration value.

Type Parameters

TEnum

The type of the enumeration.

Exceptions

OpnException

Thrown if the underlying hardware call fails.

GetProtocolVersion()

Retrieves the protocol version number.

public int GetProtocolVersion()

Returns

int

The protocol version number.

Exceptions

OpnException

When the command fails and no cached data exists.

GetRtcMode()

Retrieves the current RTC (Real-Time Clock) mode.

public int GetRtcMode()

Returns

int

The RTC mode value (0 = TimeStamp is disabled, 1 = Timestamp is enabled).

Exceptions

OpnException

When the command fails.

GetSoftwareVersion()

Retrieves the firmware/software version string.

public string GetSoftwareVersion()

Returns

string

The software version string.

Exceptions

OpnException

When the command fails and no cached data exists.

GetTime(out DateTime)

Retrieves the current device time.

public void GetTime(out DateTime dateTime)

Parameters

dateTime DateTime

Exceptions

OpnException

When the command fails.

Interrogate()

Requests the device to send its current status and identification data.

public void Interrogate()

Remarks

GetDeviceId() GetSoftwareVersion() and GetProtocolVersion() automatically call this function if required

Exceptions

OpnException

When the command fails.

ReadBarcodes()

Reads all barcode data currently stored on the device.

public IReadOnlyList<BarcodeRecord> ReadBarcodes()

Returns

IReadOnlyList<BarcodeRecord>

A read-only list of barcode data packets.

Remarks

This method also retrieves device information such as ID, protocol, or firmware version.

Exceptions

OpnException

When the command fails.

SetParameter(OpnParameter, bool)

Sets a specific parameter on the device using a boolean value.

public void SetParameter(OpnParameter parameter, bool enabled)

Parameters

parameter OpnParameter

The parameter to set.

enabled bool

Indicates whether the parameter should be enabled (1) or disabled (0).

Exceptions

OpnException

Thrown when the command fails.

SetParameter(OpnParameter, byte[])

Sets a specific parameter on the device using raw byte data.

public void SetParameter(OpnParameter parameter, byte[] data)

Parameters

parameter OpnParameter

The parameter to set.

data byte[]

The raw byte data to set for the parameter.

Exceptions

OpnException

Thrown when the command fails.

ArgumentNullException

Thrown when data is null.

SetParameter(OpnParameter, int)

Sets a specific parameter on the device using an integer value.

public void SetParameter(OpnParameter parameter, int value)

Parameters

parameter OpnParameter

The parameter to set.

value int

The value to set for the parameter.

Exceptions

OpnException

Thrown when the command fails.

SetParameter(OpnParameter, string)

Sets a specific parameter on the device using a string value (e.g., scratch pad data).

public void SetParameter(OpnParameter parameter, string data)

Parameters

parameter OpnParameter

The parameter to set.

data string

The string value to set. Converted to ASCII before being sent.

Exceptions

OpnException

Thrown when the command fails.

SetParameter<TEnum>(OpnParameter, TEnum)

Sets a specific parameter on the device using an enumeration value.

public void SetParameter<TEnum>(OpnParameter parameter, TEnum value) where TEnum : struct, Enum

Parameters

parameter OpnParameter

The parameter to set.

value TEnum

The enumeration value to set for the parameter.

Type Parameters

TEnum

The enumeration type.

Exceptions

OpnException

Thrown when the command fails.

SetTime(DateTime)

Sets the device real-time clock.

public void SetTime(DateTime time)

Parameters

time DateTime

The DateTime to set on the device.

Exceptions

OpnException

When the command fails.

SetToDefault()

Resets the device to its factory default mode.

public void SetToDefault()

Exceptions

OpnException

When the command fails.

StartPolling(DeviceDetectedHandler)

Starts polling all COM ports for devices.

public static int StartPolling(OpnDevice.DeviceDetectedHandler callback)

Parameters

callback OpnDevice.DeviceDetectedHandler

Returns

int

StopPolling()

Stops polling completely.

public static int StopPolling()

Returns

int

TryGetParameter(OpnParameter, out bool)

Attempts to get a parameter as a boolean value without throwing an exception.

public bool TryGetParameter(OpnParameter parameter, out bool enabled)

Parameters

parameter OpnParameter

The parameter to retrieve.

enabled bool

The retrieved boolean value. False if failed.

Returns

bool

True if the parameter was successfully retrieved; otherwise, false.

TryGetParameter(OpnParameter, out byte[])

Attempts to get a parameter as raw byte data without throwing an exception.

public bool TryGetParameter(OpnParameter parameter, out byte[] data)

Parameters

parameter OpnParameter

The parameter to retrieve.

data byte[]

The retrieved raw byte data. Default if failed.

Returns

bool

True if the parameter was successfully retrieved; otherwise, false.

TryGetParameter(OpnParameter, out int)

Attempts to get a parameter as an integer value without throwing an exception.

public bool TryGetParameter(OpnParameter parameter, out int value)

Parameters

parameter OpnParameter

The parameter to retrieve.

value int

The retrieved integer value. Default if failed.

Returns

bool

True if the parameter was successfully retrieved; otherwise, false.

TryGetParameter(OpnParameter, out string)

Attempts to get a parameter as a string without throwing an exception.

public bool TryGetParameter(OpnParameter parameter, out string value)

Parameters

parameter OpnParameter

The parameter to retrieve.

value string

The retrieved string value. Empty if failed.

Returns

bool

True if the parameter was successfully retrieved; otherwise, false.

TryGetParameter<TEnum>(OpnParameter, out TEnum)

Attempts to get a parameter as an enumeration value without throwing an exception.

public bool TryGetParameter<TEnum>(OpnParameter parameter, out TEnum value) where TEnum : struct, Enum

Parameters

parameter OpnParameter

The parameter to retrieve.

value TEnum

The retrieved enumeration value. Default if failed.

Returns

bool

True if the parameter was successfully retrieved; otherwise, false.

Type Parameters

TEnum

The type of the enumeration.

TrySetParameter(OpnParameter, bool)

Attempts to set a specific parameter on the device using a boolean value without throwing an exception.

public bool TrySetParameter(OpnParameter parameter, bool enabled)

Parameters

parameter OpnParameter

The parameter to set.

enabled bool

Indicates whether the parameter should be enabled or disabled.

Returns

bool

True if the parameter was successfully set; otherwise, false.

TrySetParameter(OpnParameter, byte[])

Attempts to set a specific parameter on the device using raw byte data without throwing an exception.

public bool TrySetParameter(OpnParameter parameter, byte[] data)

Parameters

parameter OpnParameter

The parameter to set.

data byte[]

The raw byte data to set for the parameter.

Returns

bool

True if the parameter was successfully set; otherwise, false.

TrySetParameter(OpnParameter, int)

Attempts to set a specific parameter on the device using an integer value without throwing an exception.

public bool TrySetParameter(OpnParameter parameter, int value)

Parameters

parameter OpnParameter

The parameter to set.

value int

The value to set for the parameter.

Returns

bool

True if the parameter was successfully set; otherwise, false.

TrySetParameter(OpnParameter, string)

Attempts to set a specific parameter on the device using a string value without throwing an exception.

public bool TrySetParameter(OpnParameter parameter, string data)

Parameters

parameter OpnParameter

The parameter to set.

data string

The string value to set.

Returns

bool

True if the parameter was successfully set; otherwise, false.

TrySetParameter<TEnum>(OpnParameter, TEnum)

Attempts to set a specific parameter on the device using an enumeration value without throwing an exception.

public bool TrySetParameter<TEnum>(OpnParameter parameter, TEnum value) where TEnum : struct, Enum

Parameters

parameter OpnParameter

The parameter to set.

value TEnum

The enumeration value to set for the parameter.

Returns

bool

True if the parameter was successfully set; otherwise, false.

Type Parameters

TEnum

The enumeration type.