Code for Velleman P8062

Apr 27, 2014 at 3:21 PM
I have an implementation for the Velleman P8062.

P8062DmxPort.cs:
namespace Proxymity.QuickDmx.VellemanP8062
{
    using System;

    public class P8062DmxPort : IDmxPort
    {
        #region Fields

        private bool _isDisposed;

        private bool _isOpened;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Constructor
        /// </summary>
        public P8062DmxPort()
        {
            this.PhysicalPortId = "Velleman P8062-1";
        }

        #endregion

        #region Public Properties

        public string PhysicalPortId { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// </summary>
        public void Close()
        {
            this.CheckDisposed();

            P8062Interop.StopDevice();

            this._isOpened = false;
        }

        public void Dispose()
        {
            this.CheckDisposed();
            if (this._isOpened)
            {
            }
            this._isDisposed = true;
        }

        /// <summary>
        /// </summary>
        public void Open()
        {
            this.CheckDisposed();

            P8062Interop.StopDevice();
            P8062Interop.StartDevice();

            this._isOpened = true;
        }

        /// <summary>
        /// </summary>
        /// <param name="universeData"></param>
        public void Send(byte[] universeData)
        {
            this.CheckPortOpened();

            // Unoptimized code. 

            P8062Interop.SetChannelCount(universeData.Length);

            for (var i = 0; i < universeData.Length; i++)
            {
                P8062Interop.SetData(i, universeData[i]);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// </summary>
        private void CheckDisposed()
        {
            if (this._isDisposed)
            {
                throw new ObjectDisposedException("Already disposed");
            }
        }

        /// <summary>
        /// </summary>
        private void CheckPortOpened()
        {
            if (!this._isOpened)
            {
                throw new InvalidOperationException("Port not opened");
            }
        }

        #endregion
    }
}
P8062Interop.cs:
namespace Proxymity.QuickDmx.VellemanP8062
{
    using System.Runtime.InteropServices;

    internal static class P8062Interop
    {
        #region Methods

        [DllImport("k8062d.dll", EntryPoint = "SetChannelCount")]
        internal static extern void SetChannelCount(int count);

        [DllImport("k8062d.dll", EntryPoint = "SetData")]
        internal static extern void SetData(int channel, int data);

        [DllImport("k8062d.dll", EntryPoint = "StartDevice")]
        internal static extern void StartDevice();

        [DllImport("k8062d.dll", EntryPoint = "StopDevice")]
        internal static extern void StopDevice();

        #endregion
    }
}
Usage:
            var port = new P8062DmxPort();
            if (port != null)
            {
                using (var dmxController = port.CreateController())
                {
                    // Send some levels to channels 1 to 3
                    dmxController[1] = 255;
                    dmxController[2] = 180;
                    dmxController[3] = 180;
                    Thread.Sleep(TimeSpan.FromSeconds(5));
                }
            }