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
portintThe 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
trueif data is available; otherwisefalse.
IsDataAvailable
Checks whether data is available to be read from the device.
public bool IsDataAvailable { get; }
Property Value
- bool
trueif data is available; otherwisefalse.
Port
Gets the COM port number associated with this device.
public int Port { get; }
Property Value
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
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
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
parameterOpnParameterThe parameter to retrieve.
enabledboolThe 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
parameterOpnParameterThe parameter to retrieve.
databyte[]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
parameterOpnParameterThe parameter to retrieve.
valueintThe 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
parameterOpnParameterThe parameter to retrieve.
valuestringThe 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
parameterOpnParameterThe parameter to retrieve.
valueTEnumThe retrieved enumeration value.
Type Parameters
TEnumThe 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
dateTimeDateTime
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
parameterOpnParameterThe parameter to set.
enabledboolIndicates 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
parameterOpnParameterThe parameter to set.
databyte[]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
parameterOpnParameterThe parameter to set.
valueintThe 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
parameterOpnParameterThe parameter to set.
datastringThe 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
parameterOpnParameterThe parameter to set.
valueTEnumThe enumeration value to set for the parameter.
Type Parameters
TEnumThe enumeration type.
Exceptions
- OpnException
Thrown when the command fails.
SetTime(DateTime)
Sets the device real-time clock.
public void SetTime(DateTime time)
Parameters
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
callbackOpnDevice.DeviceDetectedHandler
Returns
StopPolling()
Stops polling completely.
public static int StopPolling()
Returns
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
parameterOpnParameterThe parameter to retrieve.
enabledboolThe 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
parameterOpnParameterThe parameter to retrieve.
databyte[]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
parameterOpnParameterThe parameter to retrieve.
valueintThe 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
parameterOpnParameterThe parameter to retrieve.
valuestringThe 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
parameterOpnParameterThe parameter to retrieve.
valueTEnumThe retrieved enumeration value. Default if failed.
Returns
- bool
True if the parameter was successfully retrieved; otherwise, false.
Type Parameters
TEnumThe 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
parameterOpnParameterThe parameter to set.
enabledboolIndicates 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
parameterOpnParameterThe parameter to set.
databyte[]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
parameterOpnParameterThe parameter to set.
valueintThe 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
parameterOpnParameterThe parameter to set.
datastringThe 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
parameterOpnParameterThe parameter to set.
valueTEnumThe enumeration value to set for the parameter.
Returns
- bool
True if the parameter was successfully set; otherwise, false.
Type Parameters
TEnumThe enumeration type.