using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32.SafeHandles;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Timers;


namespace GenericHid
{
    enum dali_cmd_number : int
    {
        DALI_CMD_OFF                             = 0,
        DALI_CMD_UP,
        DALI_CMD_DOWN,
        DALI_CMD_STEP_UP,
        DALI_CMD_STEP_DOWN,
        DALI_CMD_RECALL_MAX_LEVEL,
        DALI_CMD_RECALL_MIN_LEVEL,
        DALI_CMD_STEP_DOWN_AND_OFF,
        DALI_CMD_ON_AND_STEP_UP,
        DALI_CMD_ENABLE_DAPC_SEQUENCE,
        DALI_CMD_RESERVED_10,
        DALI_CMD_RESERVED_11,
        DALI_CMD_RESERVED_12,
        DALI_CMD_RESERVED_13,
        DALI_CMD_RESERVED_14,
        DALI_CMD_RESERVED_15,
        DALI_CMD_GO_TO_SCENE_0,
        DALI_CMD_GO_TO_SCENE_1,
        DALI_CMD_GO_TO_SCENE_2,
        DALI_CMD_GO_TO_SCENE_3,
        DALI_CMD_GO_TO_SCENE_4,
        DALI_CMD_GO_TO_SCENE_5,
        DALI_CMD_GO_TO_SCENE_6,
        DALI_CMD_GO_TO_SCENE_7,
        DALI_CMD_GO_TO_SCENE_8,
        DALI_CMD_GO_TO_SCENE_9,
        DALI_CMD_GO_TO_SCENE_10,
        DALI_CMD_GO_TO_SCENE_11,
        DALI_CMD_GO_TO_SCENE_12,
        DALI_CMD_GO_TO_SCENE_13,
        DALI_CMD_GO_TO_SCENE_14,
        DALI_CMD_GO_TO_SCENE_15,                                                                                            
        DALI_CMD_RESET,
        DALI_CMD_STORE_ACTUAL_LEVEL_IN_THE_DTR,
        DALI_CMD_RESERVED_34,
        DALI_CMD_RESERVED_35,
        DALI_CMD_RESERVED_36,
        DALI_CMD_RESERVED_37,
        DALI_CMD_RESERVED_38,
        DALI_CMD_RESERVED_39,
        DALI_CMD_RESERVED_40,
        DALI_CMD_RESERVED_41,
        DALI_CMD_STORE_THE_DTR_AS_MAX_LEVEL,
        DALI_CMD_STORE_THE_DTR_AS_MIN_LEVEL,
        DALI_CMD_STORE_THE_DTR_AS_SYSTEM_FAILURE_LEVEL,
        DALI_CMD_STORE_THE_DTR_AS_POWER_ON_LEVEL,
        DALI_CMD_STORE_THE_DTR_AS_FADE_TIME,
        DALI_CMD_STORE_THE_DTR_AS_FADE_RATE,
        DALI_CMD_RESERVED_48,
        DALI_CMD_RESERVED_49,
        DALI_CMD_RESERVED_50,
        DALI_CMD_RESERVED_51,
        DALI_CMD_RESERVED_52,
        DALI_CMD_RESERVED_53,
        DALI_CMD_RESERVED_54,
        DALI_CMD_RESERVED_55,
        DALI_CMD_RESERVED_56,
        DALI_CMD_RESERVED_57,
        DALI_CMD_RESERVED_58,
        DALI_CMD_RESERVED_59,
        DALI_CMD_RESERVED_60,
        DALI_CMD_RESERVED_61,
        DALI_CMD_RESERVED_62,
        DALI_CMD_RESERVED_63,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_0,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_1,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_2,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_3,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_4,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_5,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_6,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_7,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_8,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_9,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_10,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_11,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_12,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_13,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_14,
        DALI_CMD_STORE_THE_DTR_AS_SCENE_15,
        DALI_CMD_REMOVE_FROM_SCENE_0,
        DALI_CMD_REMOVE_FROM_SCENE_1,
        DALI_CMD_REMOVE_FROM_SCENE_2,
        DALI_CMD_REMOVE_FROM_SCENE_3,
        DALI_CMD_REMOVE_FROM_SCENE_4,
        DALI_CMD_REMOVE_FROM_SCENE_5,
        DALI_CMD_REMOVE_FROM_SCENE_6,
        DALI_CMD_REMOVE_FROM_SCENE_7,
        DALI_CMD_REMOVE_FROM_SCENE_8,
        DALI_CMD_REMOVE_FROM_SCENE_9,
        DALI_CMD_REMOVE_FROM_SCENE_10,
        DALI_CMD_REMOVE_FROM_SCENE_11,
        DALI_CMD_REMOVE_FROM_SCENE_12,
        DALI_CMD_REMOVE_FROM_SCENE_13,
        DALI_CMD_REMOVE_FROM_SCENE_14,
        DALI_CMD_REMOVE_FROM_SCENE_15,
        DALI_CMD_ADD_TO_GROUP_0,
        DALI_CMD_ADD_TO_GROUP_1,
        DALI_CMD_ADD_TO_GROUP_2,
        DALI_CMD_ADD_TO_GROUP_3,
        DALI_CMD_ADD_TO_GROUP_4,
        DALI_CMD_ADD_TO_GROUP_5,
        DALI_CMD_ADD_TO_GROUP_6,
        DALI_CMD_ADD_TO_GROUP_7,
        DALI_CMD_ADD_TO_GROUP_8,
        DALI_CMD_ADD_TO_GROUP_9,
        DALI_CMD_ADD_TO_GROUP_10,
        DALI_CMD_ADD_TO_GROUP_11,
        DALI_CMD_ADD_TO_GROUP_12,
        DALI_CMD_ADD_TO_GROUP_13,
        DALI_CMD_ADD_TO_GROUP_14,
        DALI_CMD_ADD_TO_GROUP_15,
        DALI_CMD_REMOVE_FROM_GROUP_0,
        DALI_CMD_REMOVE_FROM_GROUP_1,
        DALI_CMD_REMOVE_FROM_GROUP_2,
        DALI_CMD_REMOVE_FROM_GROUP_3,
        DALI_CMD_REMOVE_FROM_GROUP_4,
        DALI_CMD_REMOVE_FROM_GROUP_5,
        DALI_CMD_REMOVE_FROM_GROUP_6,
        DALI_CMD_REMOVE_FROM_GROUP_7,
        DALI_CMD_REMOVE_FROM_GROUP_8,
        DALI_CMD_REMOVE_FROM_GROUP_9,
        DALI_CMD_REMOVE_FROM_GROUP_10,
        DALI_CMD_REMOVE_FROM_GROUP_11,
        DALI_CMD_REMOVE_FROM_GROUP_12,
        DALI_CMD_REMOVE_FROM_GROUP_13,
        DALI_CMD_REMOVE_FROM_GROUP_14,
        DALI_CMD_REMOVE_FROM_GROUP_15,
        DALI_CMD_STORE_DTR_AS_SHORT_ADDRESS,
        DALI_CMD_ENABLE_WRITE_MEMORY,
        DALI_CMD_RESERVED_130,
        DALI_CMD_RESERVED_131,
        DALI_CMD_RESERVED_132,
        DALI_CMD_RESERVED_133,
        DALI_CMD_RESERVED_134,
        DALI_CMD_RESERVED_135,
        DALI_CMD_RESERVED_136,
        DALI_CMD_RESERVED_137,
        DALI_CMD_RESERVED_138,
        DALI_CMD_RESERVED_139,
        DALI_CMD_RESERVED_140,
        DALI_CMD_RESERVED_141,
        DALI_CMD_RESERVED_142,
        DALI_CMD_RESERVED_143,
        DALI_CMD_QUERY_STATUS,
        DALI_CMD_QUERY_CONTROL_GEAR,
        DALI_CMD_QUERY_LAMP_FAILURE,
        DALI_CMD_QUERY_LAMP_POWER_ON,
        DALI_CMD_QUERY_LIMIT_ERROR,
        DALI_CMD_QUERY_RESET_STATE,
        DALI_CMD_QUERY_MISSING_SHORT_ADDRESS,
        DALI_CMD_QUERY_VERSION_NUMBER,
        DALI_CMD_QUERY_CONTENT_DTR,
        DALI_CMD_QUERY_DEVICE_TYPE,
        DALI_CMD_QUERY_PHYSICAL_MINIMUM_LEVEL,
        DALI_CMD_QUERY_POWER_FAILURE,
        DALI_CMD_QUERY_CONTENT_DTR1,
        DALI_CMD_QUERY_CONTENT_DTR2,
        DALI_CMD_RESERVED_158,
        DALI_CMD_RESERVED_159,
        DALI_CMD_QUERY_ACTUAL_LEVEL,
        DALI_CMD_QUERY_MAX_LEVEL,
        DALI_CMD_QUERY_MIN_LEVEL,
        DALI_CMD_QUERY_POWER_ON_LEVEL,
        DALI_CMD_QUERY_SYSTEM_FAILURE_LEVEL,
        DALI_CMD_QUERY_FADE_TIME_FADE_RATE,
        DALI_CMD_RESERVED_166,
        DALI_CMD_RESERVED_167,
        DALI_CMD_RESERVED_168,
        DALI_CMD_RESERVED_169,
        DALI_CMD_RESERVED_170,
        DALI_CMD_RESERVED_171,
        DALI_CMD_RESERVED_172,
        DALI_CMD_RESERVED_173,
        DALI_CMD_RESERVED_174,
        DALI_CMD_RESERVED_175,
        DALI_CMD_QUERY_SCENE_LEVEL_0,
        DALI_CMD_QUERY_SCENE_LEVEL_1,
        DALI_CMD_QUERY_SCENE_LEVEL_2,
        DALI_CMD_QUERY_SCENE_LEVEL_3,
        DALI_CMD_QUERY_SCENE_LEVEL_4,
        DALI_CMD_QUERY_SCENE_LEVEL_5,
        DALI_CMD_QUERY_SCENE_LEVEL_6,
        DALI_CMD_QUERY_SCENE_LEVEL_7,
        DALI_CMD_QUERY_SCENE_LEVEL_8,
        DALI_CMD_QUERY_SCENE_LEVEL_9,
        DALI_CMD_QUERY_SCENE_LEVEL_10,
        DALI_CMD_QUERY_SCENE_LEVEL_11,
        DALI_CMD_QUERY_SCENE_LEVEL_12,
        DALI_CMD_QUERY_SCENE_LEVEL_13,
        DALI_CMD_QUERY_SCENE_LEVEL_14,
        DALI_CMD_QUERY_SCENE_LEVEL_15,
        DALI_CMD_QUERY_GROUPS_0_7,
        DALI_CMD_QUERY_GROUPS_8_15,
        DALI_CMD_QUERY_RANDOM_ADDRESS_H,
        DALI_CMD_QUERY_RANDOM_ADDRESS_M,
        DALI_CMD_QUERY_RANDOM_ADDRESS_L,
        DALI_CMD_READ_MEMORY_LOCATION,
        DALI_CMD_RESERVED_198,
        DALI_CMD_RESERVED_199,
        DALI_CMD_RESERVED_200,
        DALI_CMD_RESERVED_201,
        DALI_CMD_RESERVED_202,
        DALI_CMD_RESERVED_203,
        DALI_CMD_RESERVED_204,
        DALI_CMD_RESERVED_205,
        DALI_CMD_RESERVED_206,
        DALI_CMD_RESERVED_207,
        DALI_CMD_RESERVED_208,
        DALI_CMD_RESERVED_209,
        DALI_CMD_RESERVED_210,
        DALI_CMD_RESERVED_211,
        DALI_CMD_RESERVED_212,
        DALI_CMD_RESERVED_213,
        DALI_CMD_RESERVED_214,
        DALI_CMD_RESERVED_215,
        DALI_CMD_RESERVED_216,
        DALI_CMD_RESERVED_217,
        DALI_CMD_RESERVED_218,
        DALI_CMD_RESERVED_219,
        DALI_CMD_RESERVED_220,
        DALI_CMD_RESERVED_221,
        DALI_CMD_RESERVED_222,
        DALI_CMD_RESERVED_223,
        DALI_CMD_APP_EXTENDED_224,
        DALI_CMD_APP_EXTENDED_225,
        DALI_CMD_APP_EXTENDED_226,
        DALI_CMD_APP_EXTENDED_227,
        DALI_CMD_APP_EXTENDED_228,
        DALI_CMD_APP_EXTENDED_229,
        DALI_CMD_APP_EXTENDED_230,
        DALI_CMD_APP_EXTENDED_231,
        DALI_CMD_APP_EXTENDED_232,
        DALI_CMD_APP_EXTENDED_233,
        DALI_CMD_APP_EXTENDED_234,
        DALI_CMD_APP_EXTENDED_235,
        DALI_CMD_APP_EXTENDED_236,
        DALI_CMD_APP_EXTENDED_237,
        DALI_CMD_APP_EXTENDED_238,
        DALI_CMD_APP_EXTENDED_239,
        DALI_CMD_APP_EXTENDED_240,
        DALI_CMD_APP_EXTENDED_241,
        DALI_CMD_APP_EXTENDED_242,
        DALI_CMD_APP_EXTENDED_243,
        DALI_CMD_APP_EXTENDED_244,
        DALI_CMD_APP_EXTENDED_245,
        DALI_CMD_APP_EXTENDED_246,
        DALI_CMD_APP_EXTENDED_247,
        DALI_CMD_APP_EXTENDED_248,
        DALI_CMD_APP_EXTENDED_249,
        DALI_CMD_APP_EXTENDED_250,
        DALI_CMD_APP_EXTENDED_251,
        DALI_CMD_APP_EXTENDED_252,
        DALI_CMD_APP_EXTENDED_253,
        DALI_CMD_APP_EXTENDED_254,
        DALI_CMD_QUERY_EXTENDED_VERSION_NUM,

        //special commands
        DALI_CMD_TERMINATE,
        DALI_CMD_DATA_TRANSFER_REGISTER_DTR,
        DALI_CMD_INITIALISE,
        DALI_CMD_RANDOMISE,
        DALI_CMD_COMPARE,
        DALI_CMD_WITHDRAW,
        DALI_CMD_RESERVED_262,
        DALI_CMD_RESERVED_263,
        DALI_CMD_SEARCH_ADDR_H,
        DALI_CMD_SEARCH_ADDR_M,
        DALI_CMD_SEARCH_ADDR_L,
        DALI_CMD_PROGRAM_SHORT_ADDRESS,
        DALI_CMD_VERIFY_SHORT_ADDRESS,
        DALI_CMD_QUERY_SHORT_ADDRESS,
        DALI_CMD_PHYSICAL_SELECTION,
        DALI_CMD_RESERVED_271,
        DALI_CMD_ENABLE_DEVICE_TYPE_X,
        DALI_CMD_DATA_TRANSFER_REGISTER_1,
        DALI_CMD_DATA_TRANSFER_REGISTER_2,
        DALI_CMD_WRITE_MEMORY_LOCATION,

        DALI_CMD_NO_COMMAND                      = 999
    }

    enum discovery_state : int
    {
        DISCOVERY_IDLE,
        DISCOVERY_START,
        DISCOVERY_TERMINATE,
        DISCOVERY_INITIALISE,
        DISCOVERY_RESET_DTR,
        DISCOVERY_STORE_DTR_AS_SHORT_ADDRESS,
        DISCOVERY_REMOVE_FROM_GROUP,
        DISCOVERY_SEARCH_ADDR,
        DISCOVERY_PROGRAM_SHORT_ADDRESS,
        DISCOVERY_GET_INFO,
        DISCOVERY_STOP
    };

    public partial class DaliForm : Form
    {
        // --------------------------------------------------------------------
        //  Structure to hold info for all Dali commands
        // --------------------------------------------------------------------
        private struct daliCmd_t
        {
            public dali_cmd_number daliCommandNumber;
            public String daliCommandName;
            public Byte daliCommandAddr;
            public Byte daliCommandCode;
            public bool daliNeedsAddressing;
            public bool daliNeedsValue;
            public bool daliExpectedResponse;

            public daliCmd_t(dali_cmd_number p1, String p2, Byte p3, Byte p4, bool p5, bool p6, bool p7)
            {
                daliCommandNumber = p1;
                daliCommandName = p2;
                daliCommandAddr = p3;
                daliCommandCode = p4;
                daliNeedsAddressing = p5;
                daliNeedsValue = p6;
                daliExpectedResponse = p7;
            }
        }

        // --------------------------------------------------------------------
        //  Filled in structure that holds info for all Dali commands
        //  Derived from Dali specifications
        // --------------------------------------------------------------------
        private daliCmd_t[] daliCmd =
        {
            //                            Number                                         ,Name                                        ,Addr ,Code ,NAddr ,NValue,Response
            new daliCmd_t(dali_cmd_number.DALI_CMD_NO_COMMAND                            ,"-   DIRECT ARC POWER CONTROL"              ,0x00 ,0x00 ,true  ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_OFF                                   ,"0   OFF"                                   ,0x01 ,0x00 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_UP                                    ,"1   UP"                                    ,0x01 ,0x01 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_DOWN                                  ,"2   DOWN"                                  ,0x01 ,0x02 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STEP_UP                               ,"3   STEP UP"                               ,0x01 ,0x03 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STEP_DOWN                             ,"4   STEP DOWN"                             ,0x01 ,0x04 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RECALL_MAX_LEVEL                      ,"5   RECALL MAX LEVEL"                      ,0x01 ,0x05 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RECALL_MIN_LEVEL                      ,"6   RECALL MIN LEVEL"                      ,0x01 ,0x06 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STEP_DOWN_AND_OFF                     ,"7   STEP DOWN AND OFF"                     ,0x01 ,0x07 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ON_AND_STEP_UP                        ,"8   ON AND STEP UP"                        ,0x01 ,0x08 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ENABLE_DAPC_SEQUENCE                  ,"9   ENABLE DAPC SEQUENCE"                  ,0x01 ,0x09 ,true  ,false ,false),
                                                                                                              
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_0                         ,"16  GO TO SCENE  0"                        ,0x01 ,0x10 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_1                         ,"17  GO TO SCENE  1"                        ,0x01 ,0x11 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_2                         ,"18  GO TO SCENE  2"                        ,0x01 ,0x12 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_3                         ,"19  GO TO SCENE  3"                        ,0x01 ,0x13 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_4                         ,"20  GO TO SCENE  4"                        ,0x01 ,0x14 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_5                         ,"21  GO TO SCENE  5"                        ,0x01 ,0x15 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_6                         ,"22  GO TO SCENE  6"                        ,0x01 ,0x16 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_7                         ,"23  GO TO SCENE  7"                        ,0x01 ,0x17 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_8                         ,"24  GO TO SCENE  8"                        ,0x01 ,0x18 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_9                         ,"25  GO TO SCENE  9"                        ,0x01 ,0x19 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_10                        ,"26  GO TO SCENE 10"                        ,0x01 ,0x1a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_11                        ,"27  GO TO SCENE 11"                        ,0x01 ,0x1b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_12                        ,"28  GO TO SCENE 12"                        ,0x01 ,0x1c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_13                        ,"29  GO TO SCENE 13"                        ,0x01 ,0x1d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_14                        ,"30  GO TO SCENE 14"                        ,0x01 ,0x1e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_GO_TO_SCENE_15                        ,"31  GO TO SCENE 15"                        ,0x01 ,0x1f ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESET                                 ,"32  RESET"                                 ,0x01 ,0x20 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_ACTUAL_LEVEL_IN_THE_DTR         ,"33  STORE ACTUAL LEVEL IN THE DTR"         ,0x01 ,0x21 ,true  ,false ,false),
                                                                                                              
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_MAX_LEVEL            ,"42  STORE THE DTR AS MAX LEVEL"            ,0x01 ,0x2a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_MIN_LEVEL            ,"43  STORE THE DTR AS MIN LEVEL"            ,0x01 ,0x2b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SYSTEM_FAILURE_LEVEL ,"44  STORE THE DTR AS SYSTEM FAILURE LEVEL" ,0x01 ,0x2c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_POWER_ON_LEVEL       ,"45  STORE THE DTR AS POWER ON LEVEL"       ,0x01 ,0x2d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_FADE_TIME            ,"46  STORE THE DTR AS FADE TIME"            ,0x01 ,0x2e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_FADE_RATE            ,"47  STORE THE DTR AS FADE RATE"            ,0x01 ,0x2f ,true  ,false ,false),
                                                                                                              
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_0              ,"64  STORE THE DTR AS SCENE  0"             ,0x01 ,0x40 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_1              ,"65  STORE THE DTR AS SCENE  1"             ,0x01 ,0x41 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_2              ,"66  STORE THE DTR AS SCENE  2"             ,0x01 ,0x42 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_3              ,"67  STORE THE DTR AS SCENE  3"             ,0x01 ,0x43 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_4              ,"68  STORE THE DTR AS SCENE  4"             ,0x01 ,0x44 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_5              ,"69  STORE THE DTR AS SCENE  5"             ,0x01 ,0x45 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_6              ,"70  STORE THE DTR AS SCENE  6"             ,0x01 ,0x46 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_7              ,"71  STORE THE DTR AS SCENE  7"             ,0x01 ,0x47 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_8              ,"72  STORE THE DTR AS SCENE  8"             ,0x01 ,0x48 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_9              ,"73  STORE THE DTR AS SCENE  9"             ,0x01 ,0x49 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_10             ,"74  STORE THE DTR AS SCENE 10"             ,0x01 ,0x4a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_11             ,"75  STORE THE DTR AS SCENE 11"             ,0x01 ,0x4b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_12             ,"76  STORE THE DTR AS SCENE 12"             ,0x01 ,0x4c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_13             ,"77  STORE THE DTR AS SCENE 13"             ,0x01 ,0x4d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_14             ,"78  STORE THE DTR AS SCENE 14"             ,0x01 ,0x4e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_THE_DTR_AS_SCENE_15             ,"79  STORE THE DTR AS SCENE 15"             ,0x01 ,0x4f ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_0                   ,"80  REMOVE FROM SCENE  0"                  ,0x01 ,0x50 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_1                   ,"81  REMOVE FROM SCENE  1"                  ,0x01 ,0x51 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_2                   ,"82  REMOVE FROM SCENE  2"                  ,0x01 ,0x52 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_3                   ,"83  REMOVE FROM SCENE  3"                  ,0x01 ,0x53 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_4                   ,"84  REMOVE FROM SCENE  4"                  ,0x01 ,0x54 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_5                   ,"85  REMOVE FROM SCENE  5"                  ,0x01 ,0x55 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_6                   ,"86  REMOVE FROM SCENE  6"                  ,0x01 ,0x56 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_7                   ,"87  REMOVE FROM SCENE  7"                  ,0x01 ,0x57 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_8                   ,"88  REMOVE FROM SCENE  8"                  ,0x01 ,0x58 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_9                   ,"89  REMOVE FROM SCENE  9"                  ,0x01 ,0x59 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_10                  ,"90  REMOVE FROM SCENE 10"                  ,0x01 ,0x5a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_11                  ,"91  REMOVE FROM SCENE 11"                  ,0x01 ,0x5b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_12                  ,"92  REMOVE FROM SCENE 12"                  ,0x01 ,0x5c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_13                  ,"93  REMOVE FROM SCENE 13"                  ,0x01 ,0x5d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_14                  ,"94  REMOVE FROM SCENE 14"                  ,0x01 ,0x5e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_SCENE_15                  ,"95  REMOVE FROM SCENE 15"                  ,0x01 ,0x5f ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_0                        ,"96  ADD TO GROUP  0"                       ,0x01 ,0x60 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_1                        ,"97  ADD TO GROUP  1"                       ,0x01 ,0x61 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_2                        ,"98  ADD TO GROUP  2"                       ,0x01 ,0x62 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_3                        ,"99  ADD TO GROUP  3"                       ,0x01 ,0x63 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_4                        ,"100 ADD TO GROUP  4"                       ,0x01 ,0x64 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_5                        ,"101 ADD TO GROUP  5"                       ,0x01 ,0x65 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_6                        ,"102 ADD TO GROUP  6"                       ,0x01 ,0x66 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_7                        ,"103 ADD TO GROUP  7"                       ,0x01 ,0x67 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_8                        ,"104 ADD TO GROUP  8"                       ,0x01 ,0x68 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_9                        ,"105 ADD TO GROUP  9"                       ,0x01 ,0x69 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_10                       ,"106 ADD TO GROUP 10"                       ,0x01 ,0x6a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_11                       ,"107 ADD TO GROUP 11"                       ,0x01 ,0x6b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_12                       ,"108 ADD TO GROUP 12"                       ,0x01 ,0x6c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_13                       ,"109 ADD TO GROUP 13"                       ,0x01 ,0x6d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_14                       ,"110 ADD TO GROUP 14"                       ,0x01 ,0x6e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ADD_TO_GROUP_15                       ,"111 ADD TO GROUP 15"                       ,0x01 ,0x6f ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_0                   ,"112 REMOVE FROM GROUP  0"                  ,0x01 ,0x70 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_1                   ,"113 REMOVE FROM GROUP  1"                  ,0x01 ,0x71 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_2                   ,"114 REMOVE FROM GROUP  2"                  ,0x01 ,0x72 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_3                   ,"115 REMOVE FROM GROUP  3"                  ,0x01 ,0x73 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_4                   ,"116 REMOVE FROM GROUP  4"                  ,0x01 ,0x74 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_5                   ,"117 REMOVE FROM GROUP  5"                  ,0x01 ,0x75 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_6                   ,"118 REMOVE FROM GROUP  6"                  ,0x01 ,0x76 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_7                   ,"119 REMOVE FROM GROUP  7"                  ,0x01 ,0x77 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_8                   ,"120 REMOVE FROM GROUP  8"                  ,0x01 ,0x78 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_9                   ,"121 REMOVE FROM GROUP  9"                  ,0x01 ,0x79 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_10                  ,"122 REMOVE FROM GROUP 10"                  ,0x01 ,0x7a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_11                  ,"123 REMOVE FROM GROUP 11"                  ,0x01 ,0x7b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_12                  ,"124 REMOVE FROM GROUP 12"                  ,0x01 ,0x7c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_13                  ,"125 REMOVE FROM GROUP 13"                  ,0x01 ,0x7d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_14                  ,"126 REMOVE FROM GROUP 14"                  ,0x01 ,0x7e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_15                  ,"127 REMOVE FROM GROUP 15"                  ,0x01 ,0x7f ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_STORE_DTR_AS_SHORT_ADDRESS            ,"128 STORE DTR AS SHORT ADDRESS"            ,0x01 ,0x80 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_ENABLE_WRITE_MEMORY                   ,"129 ENABLE WRITE MEMORY"                   ,0x01 ,0x81 ,true  ,false ,false),

            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_STATUS                          ,"144 QUERY STATUS"                          ,0x01 ,0x90 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_CONTROL_GEAR                    ,"145 QUERY CONTROL GEAR"                    ,0x01 ,0x91 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_LAMP_FAILURE                    ,"146 QUERY LAMP FAILURE"                    ,0x01 ,0x92 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_LAMP_POWER_ON                   ,"147 QUERY LAMP POWER ON"                   ,0x01 ,0x93 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_LIMIT_ERROR                     ,"148 QUERY LIMIT ERROR"                     ,0x01 ,0x94 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_RESET_STATE                     ,"149 QUERY RESET STATE"                     ,0x01 ,0x95 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_MISSING_SHORT_ADDRESS           ,"150 QUERY MISSING SHORT ADDRESS"           ,0x01 ,0x96 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_VERSION_NUMBER                  ,"151 QUERY VERSION NUMBER"                  ,0x01 ,0x97 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_CONTENT_DTR                     ,"152 QUERY CONTENT DTR"                     ,0x01 ,0x98 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_DEVICE_TYPE                     ,"153 QUERY DEVICE TYPE"                     ,0x01 ,0x99 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_PHYSICAL_MINIMUM_LEVEL          ,"154 QUERY PHYSICAL MINIMUM LEVEL"          ,0x01 ,0x9a ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_POWER_FAILURE                   ,"155 QUERY POWER FAILURE"                   ,0x01 ,0x9b ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_CONTENT_DTR1                    ,"156 QUERY CONTENT DTR1"                    ,0x01 ,0x9c ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_CONTENT_DTR2                    ,"157 QUERY CONTENT DTR2"                    ,0x01 ,0x9d ,true  ,false ,true),
                                                                                                              
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_ACTUAL_LEVEL                    ,"160 QUERY ACTUAL LEVEL"                    ,0x01 ,0xa0 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_MAX_LEVEL                       ,"161 QUERY MAX LEVEL"                       ,0x01 ,0xa1 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_MIN_LEVEL                       ,"162 QUERY MIN LEVEL"                       ,0x01 ,0xa2 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_POWER_ON_LEVEL                  ,"163 QUERY POWER ON LEVEL"                  ,0x01 ,0xa3 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SYSTEM_FAILURE_LEVEL            ,"164 QUERY SYSTEM FAILURE LEVEL"            ,0x01 ,0xa4 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_FADE_TIME_FADE_RATE             ,"165 QUERY FADE TIME/FADE RATE"             ,0x01 ,0xa5 ,true  ,false ,true),
                                                                                                              
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_0                   ,"176 QUERY SCENE LEVEL  0"                  ,0x01 ,0xb0 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_1                   ,"177 QUERY SCENE LEVEL  1"                  ,0x01 ,0xb1 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_2                   ,"178 QUERY SCENE LEVEL  2"                  ,0x01 ,0xb2 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_3                   ,"179 QUERY SCENE LEVEL  3"                  ,0x01 ,0xb3 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_4                   ,"180 QUERY SCENE LEVEL  4"                  ,0x01 ,0xb4 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_5                   ,"181 QUERY SCENE LEVEL  5"                  ,0x01 ,0xb5 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_6                   ,"182 QUERY SCENE LEVEL  6"                  ,0x01 ,0xb6 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_7                   ,"183 QUERY SCENE LEVEL  7"                  ,0x01 ,0xb7 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_8                   ,"184 QUERY SCENE LEVEL  8"                  ,0x01 ,0xb8 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_9                   ,"185 QUERY SCENE LEVEL  9"                  ,0x01 ,0xb9 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_10                  ,"186 QUERY SCENE LEVEL 10"                  ,0x01 ,0xba ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_11                  ,"187 QUERY SCENE LEVEL 11"                  ,0x01 ,0xbb ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_12                  ,"188 QUERY SCENE LEVEL 12"                  ,0x01 ,0xbc ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_13                  ,"189 QUERY SCENE LEVEL 13"                  ,0x01 ,0xbd ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_14                  ,"190 QUERY SCENE LEVEL 14"                  ,0x01 ,0xbe ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SCENE_LEVEL_15                  ,"191 QUERY SCENE LEVEL 15"                  ,0x01 ,0xbf ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_GROUPS_0_7                      ,"192 QUERY GROUPS 0-7"                      ,0x01 ,0xc0 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_GROUPS_8_15                     ,"193 QUERY GROUPS 8-15"                     ,0x01 ,0xc1 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_RANDOM_ADDRESS_H                ,"194 QUERY RANDOM ADDRESS H"                ,0x01 ,0xc2 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_RANDOM_ADDRESS_M                ,"195 QUERY RANDOM ADDRESS M"                ,0x01 ,0xc3 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_RANDOM_ADDRESS_L                ,"196 QUERY RANDOM ADDRESS L"                ,0x01 ,0xc4 ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_READ_MEMORY_LOCATION                  ,"197 READ MEMORY LOCATION"                  ,0x01 ,0xc5 ,true  ,false ,true),

            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_224                      ,"224 APPLICATION EXTENDED CMD"              ,0x01 ,0xe0 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_225                      ,"225 APPLICATION EXTENDED CMD"              ,0x01 ,0xe1 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_226                      ,"226 APPLICATION EXTENDED CMD"              ,0x01 ,0xe2 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_227                      ,"227 APPLICATION EXTENDED CMD"              ,0x01 ,0xe3 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_228                      ,"228 APPLICATION EXTENDED CMD"              ,0x01 ,0xe4 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_229                      ,"229 APPLICATION EXTENDED CMD"              ,0x01 ,0xe5 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_230                      ,"230 APPLICATION EXTENDED CMD"              ,0x01 ,0xe6 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_231                      ,"231 APPLICATION EXTENDED CMD"              ,0x01 ,0xe7 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_232                      ,"232 APPLICATION EXTENDED CMD"              ,0x01 ,0xe8 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_233                      ,"233 APPLICATION EXTENDED CMD"              ,0x01 ,0xe9 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_234                      ,"234 APPLICATION EXTENDED CMD"              ,0x01 ,0xea ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_235                      ,"235 APPLICATION EXTENDED CMD"              ,0x01 ,0xeb ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_236                      ,"236 APPLICATION EXTENDED CMD"              ,0x01 ,0xec ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_237                      ,"237 APPLICATION EXTENDED CMD"              ,0x01 ,0xed ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_238                      ,"238 APPLICATION EXTENDED CMD"              ,0x01 ,0xee ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_239                      ,"239 APPLICATION EXTENDED CMD"              ,0x01 ,0xef ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_240                      ,"240 APPLICATION EXTENDED CMD"              ,0x01 ,0xf0 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_241                      ,"241 APPLICATION EXTENDED CMD"              ,0x01 ,0xf1 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_242                      ,"242 APPLICATION EXTENDED CMD"              ,0x01 ,0xf2 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_243                      ,"243 APPLICATION EXTENDED CMD"              ,0x01 ,0xf3 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_244                      ,"244 APPLICATION EXTENDED CMD"              ,0x01 ,0xf4 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_245                      ,"245 APPLICATION EXTENDED CMD"              ,0x01 ,0xf5 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_246                      ,"246 APPLICATION EXTENDED CMD"              ,0x01 ,0xf6 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_247                      ,"247 APPLICATION EXTENDED CMD"              ,0x01 ,0xf7 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_248                      ,"248 APPLICATION EXTENDED CMD"              ,0x01 ,0xf8 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_249                      ,"249 APPLICATION EXTENDED CMD"              ,0x01 ,0xf9 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_250                      ,"250 APPLICATION EXTENDED CMD"              ,0x01 ,0xfa ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_251                      ,"251 APPLICATION EXTENDED CMD"              ,0x01 ,0xfb ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_252                      ,"252 APPLICATION EXTENDED CMD"              ,0x01 ,0xfc ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_253                      ,"253 APPLICATION EXTENDED CMD"              ,0x01 ,0xfd ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_APP_EXTENDED_254                      ,"254 APPLICATION EXTENDED CMD"              ,0x01 ,0xfe ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_EXTENDED_VERSION_NUM            ,"255 QUERY EXTENDED VERSION NUM"            ,0x01 ,0xff ,true  ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_TERMINATE                             ,"256 TERMINATE"                             ,0xa1 ,0x00 ,false ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_DATA_TRANSFER_REGISTER_DTR            ,"257 DATA TRANSFER REGISTER"                ,0xa3 ,0x00 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_INITIALISE                            ,"258 INITIALISE"                            ,0xa5 ,0x00 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RANDOMISE                             ,"259 RANDOMISE"                             ,0xa7 ,0x00 ,false ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_COMPARE                               ,"260 COMPARE"                               ,0xa9 ,0x00 ,false ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_WITHDRAW                              ,"261 WITHDRAW"                              ,0xab ,0x00 ,false ,false ,false),
                                                                                                              
            new daliCmd_t(dali_cmd_number.DALI_CMD_SEARCH_ADDR_H                         ,"264 SEARCHADDRH"                           ,0xb1 ,0x00 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_SEARCH_ADDR_M                         ,"265 SEARCHADDRM"                           ,0xb3 ,0x00 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_SEARCH_ADDR_L                         ,"266 SEARCHADDRL"                           ,0xb5 ,0x00 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_PROGRAM_SHORT_ADDRESS                 ,"267 PROGRAM SHORT ADDRESS"                 ,0xb7 ,0x01 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_VERIFY_SHORT_ADDRESS                  ,"268 VERIFY SHORT ADDRESS"                  ,0xb9 ,0x01 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_QUERY_SHORT_ADDRESS                   ,"269 QUERY SHORT ADDRESS"                   ,0xbb ,0x00 ,false ,false ,true),
            new daliCmd_t(dali_cmd_number.DALI_CMD_PHYSICAL_SELECTION                    ,"270 PHYSICAL SELECTION"                    ,0xbd ,0x00 ,false ,false ,false),
                                                                                                              
            new daliCmd_t(dali_cmd_number.DALI_CMD_ENABLE_DEVICE_TYPE_X                  ,"272 ENABLE DEVICE TYPE X"                  ,0xc1 ,0x00 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_DATA_TRANSFER_REGISTER_1              ,"273 DATA TRANSFER REGISTER 1"              ,0xc3 ,0x00 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_DATA_TRANSFER_REGISTER_2              ,"274 DATA TRANSFER REGISTER 2"              ,0xc5 ,0x00 ,false ,true  ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_WRITE_MEMORY_LOCATION                 ,"275 WRITE MEMORY LOCATION"                 ,0xc7 ,0x00 ,false ,true  ,false),

            // reserved commands
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_10                           ,"10 Reserved"                               ,0x01 ,0x0a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_11                           ,"11 Reserved"                               ,0x01 ,0x0b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_12                           ,"12 Reserved"                               ,0x01 ,0x0c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_13                           ,"13 Reserved"                               ,0x01 ,0x0d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_14                           ,"14 Reserved"                               ,0x01 ,0x0e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_15                           ,"15 Reserved"                               ,0x01 ,0x0f ,true  ,false ,false),

            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_34                           ,"34 Reserved"                               ,0x01 ,0x22 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_35                           ,"35 Reserved"                               ,0x01 ,0x23 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_36                           ,"36 Reserved"                               ,0x01 ,0x24 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_37                           ,"37 Reserved"                               ,0x01 ,0x25 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_38                           ,"38 Reserved"                               ,0x01 ,0x26 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_39                           ,"39 Reserved"                               ,0x01 ,0x27 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_40                           ,"40 Reserved"                               ,0x01 ,0x28 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_41                           ,"41 Reserved"                               ,0x01 ,0x29 ,true  ,false ,false),

            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_48                           ,"48 Reserved"                               ,0x01 ,0x30 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_49                           ,"49 Reserved"                               ,0x01 ,0x31 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_50                           ,"50 Reserved"                               ,0x01 ,0x32 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_51                           ,"51 Reserved"                               ,0x01 ,0x33 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_52                           ,"52 Reserved"                               ,0x01 ,0x34 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_53                           ,"53 Reserved"                               ,0x01 ,0x35 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_54                           ,"54 Reserved"                               ,0x01 ,0x36 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_55                           ,"55 Reserved"                               ,0x01 ,0x37 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_56                           ,"56 Reserved"                               ,0x01 ,0x38 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_57                           ,"57 Reserved"                               ,0x01 ,0x39 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_58                           ,"58 Reserved"                               ,0x01 ,0x3a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_59                           ,"59 Reserved"                               ,0x01 ,0x3b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_60                           ,"60 Reserved"                               ,0x01 ,0x3c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_61                           ,"61 Reserved"                               ,0x01 ,0x3d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_62                           ,"62 Reserved"                               ,0x01 ,0x3e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_63                           ,"63 Reserved"                               ,0x01 ,0x3f ,true  ,false ,false),

            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_130                          ,"130 Reserved"                              ,0x01 ,0x82 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_131                          ,"131 Reserved"                              ,0x01 ,0x83 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_132                          ,"132 Reserved"                              ,0x01 ,0x84 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_133                          ,"133 Reserved"                              ,0x01 ,0x85 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_134                          ,"134 Reserved"                              ,0x01 ,0x86 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_135                          ,"135 Reserved"                              ,0x01 ,0x87 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_136                          ,"136 Reserved"                              ,0x01 ,0x88 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_137                          ,"137 Reserved"                              ,0x01 ,0x89 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_138                          ,"138 Reserved"                              ,0x01 ,0x8a ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_139                          ,"139 Reserved"                              ,0x01 ,0x8b ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_140                          ,"140 Reserved"                              ,0x01 ,0x8c ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_141                          ,"141 Reserved"                              ,0x01 ,0x8d ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_142                          ,"142 Reserved"                              ,0x01 ,0x8e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_143                          ,"143 Reserved"                              ,0x01 ,0x8f ,true  ,false ,false),

            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_158                          ,"158 Reserved"                              ,0x01 ,0x9e ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_159                          ,"159 Reserved"                              ,0x01 ,0x9f ,true  ,false ,false),

            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_166                          ,"166 Reserved"                              ,0x01 ,0xa6 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_167                          ,"167 Reserved"                              ,0x01 ,0xa7 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_168                          ,"168 Reserved"                              ,0x01 ,0xa8 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_169                          ,"169 Reserved"                              ,0x01 ,0xa9 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_170                          ,"170 Reserved"                              ,0x01 ,0xaa ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_171                          ,"171 Reserved"                              ,0x01 ,0xaa ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_172                          ,"172 Reserved"                              ,0x01 ,0xab ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_173                          ,"173 Reserved"                              ,0x01 ,0xac ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_174                          ,"174 Reserved"                              ,0x01 ,0xad ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_175                          ,"175 Reserved"                              ,0x01 ,0xae ,true  ,false ,false),

            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_198                          ,"198 Reserved"                              ,0x01 ,0xc6 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_199                          ,"199 Reserved"                              ,0x01 ,0xc7 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_200                          ,"200 Reserved"                              ,0x01 ,0xc8 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_201                          ,"201 Reserved"                              ,0x01 ,0xc9 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_202                          ,"202 Reserved"                              ,0x01 ,0xca ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_203                          ,"203 Reserved"                              ,0x01 ,0xcb ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_204                          ,"204 Reserved"                              ,0x01 ,0xcc ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_205                          ,"205 Reserved"                              ,0x01 ,0xcd ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_206                          ,"206 Reserved"                              ,0x01 ,0xce ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_207                          ,"207 Reserved"                              ,0x01 ,0xcf ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_208                          ,"208 Reserved"                              ,0x01 ,0xd0 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_209                          ,"209 Reserved"                              ,0x01 ,0xd1 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_210                          ,"210 Reserved"                              ,0x01 ,0xd2 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_211                          ,"211 Reserved"                              ,0x01 ,0xd3 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_212                          ,"212 Reserved"                              ,0x01 ,0xd4 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_213                          ,"213 Reserved"                              ,0x01 ,0xd5 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_214                          ,"214 Reserved"                              ,0x01 ,0xd6 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_215                          ,"215 Reserved"                              ,0x01 ,0xd7 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_216                          ,"216 Reserved"                              ,0x01 ,0xd8 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_217                          ,"217 Reserved"                              ,0x01 ,0xd9 ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_218                          ,"218 Reserved"                              ,0x01 ,0xda ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_219                          ,"219 Reserved"                              ,0x01 ,0xdb ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_220                          ,"220 Reserved"                              ,0x01 ,0xdc ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_221                          ,"221 Reserved"                              ,0x01 ,0xdd ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_222                          ,"222 Reserved"                              ,0x01 ,0xde ,true  ,false ,false),
            new daliCmd_t(dali_cmd_number.DALI_CMD_RESERVED_223                          ,"223 Reserved"                              ,0x01 ,0xdf ,true  ,false ,false),
        };

        private DaliForm FrmMy;
        private Boolean myDeviceDetected;
        private FileStream fileStreamDeviceData;
        private SafeFileHandle hidHandle;
        private Hid MyHid;
        private String myDevicePathName;
        private IntPtr deviceNotificationHandle;
        private String hidUsage;
        private Stopwatch stopWatch;
        private DeviceManagement MyDeviceManagement;

        public DaliForm()
        {
            InitializeComponent();
        }

        private void DaliForm_Load(System.Object eventSender, System.EventArgs eventArgs)
        {
            FrmMy = this;
            myDeviceDetected = false;
            MyHid = new Hid();
            stopWatch = new Stopwatch();
            DialogErrorMsg = new FormErrorMsg();
            MyDeviceManagement = new DeviceManagement();

            //  Default USB Vendor ID and Product ID:
            txtVendorID.Text = "04cc";
            txtProductID.Text = "0802";

            //  Set drop down box to first command in list
            for (int i = 0; i < daliCmd.Length; i++)
            {
                comboBoxDaliCmds.Items.Add(daliCmd[i].daliCommandName);
            }
            comboBoxDaliCmds.SelectedIndex = 0;

            FindTheHid();
        }

        private void CloseCommunications()
        {
            // The next attempt to communicate will get new handles and FileStreams.
            myDeviceDetected = false;
            if (fileStreamDeviceData != null)
            {
                fileStreamDeviceData.Close();
            }
            if ((hidHandle != null) && (!(hidHandle.IsInvalid)))
            {
                hidHandle.Close();
            }
        }

        ///  <summary>
        ///  Uses a series of API calls to locate a HID-class device
        ///  by its Vendor ID and Product ID.
        ///  </summary>          
        private void FindTheHid()
        {
            Boolean deviceFound = false;
            String[] devicePathName = new String[128];
            String functionName = "";
            Guid hidGuid = Guid.Empty;
            Int32 memberIndex = 0;
            Int32 myProductID = 0;
            Int32 myVendorID = 0;
            Boolean success = false;

            try
            {
                CloseCommunications();

                //  Get the device's Vendor ID and Product ID from the form's text boxes.

                GetVendorAndProductIDsFromTextBoxes(ref myVendorID, ref myProductID);

                //  ***
                //  API function: 'HidD_GetHidGuid

                //  Purpose: Retrieves the interface class GUID for the HID class.

                //  Accepts: 'A System.Guid object for storing the GUID.
                //  ***

                Hid.HidD_GetHidGuid(ref hidGuid);

                functionName = "GetHidGuid";

                //  Fill an array with the device path names of all attached HIDs.

                deviceFound = MyDeviceManagement.FindDeviceFromGuid(hidGuid, ref devicePathName);

                //  If there is at least one HID, attempt to read the Vendor ID and Product ID
                //  of each device until there is a match or all devices have been examined.

                if (deviceFound)
                {
                    memberIndex = 0;

                    do
                    {
                        //  ***
                        //  API function:
                        //  CreateFile

                        //  Purpose:
                        //  Retrieves a handle to a device.

                        //  Accepts:
                        //  A device path name returned by SetupDiGetDeviceInterfaceDetail
                        //  The type of access requested (read/write).
                        //  FILE_SHARE attributes to allow other processes to access the device while this handle is open.
                        //  A Security structure or IntPtr.Zero. 
                        //  A creation disposition value. Use OPEN_EXISTING for devices.
                        //  Flags and attributes for files. Not used for devices.
                        //  Handle to a template file. Not used.

                        //  Returns: a handle without read or write access.
                        //  This enables obtaining information about all HIDs, even system
                        //  keyboards and mice. 
                        //  Separate handles are used for reading and writing.
                        //  ***

                        // Open the handle without read/write access to enable getting information about any HID, even system keyboards and mice.

                        hidHandle = FileIO.CreateFile(devicePathName[memberIndex], 0, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);

                        functionName = "CreateFile";

                        if (!hidHandle.IsInvalid)
                        {
                            //  The returned handle is valid, 
                            //  so find out if this is the device we're looking for.

                            //  Set the Size property of DeviceAttributes to the number of bytes in the structure.

                            MyHid.DeviceAttributes.Size = Marshal.SizeOf(MyHid.DeviceAttributes);

                            //  ***
                            //  API function:
                            //  HidD_GetAttributes

                            //  Purpose:
                            //  Retrieves a HIDD_ATTRIBUTES structure containing the Vendor ID, 
                            //  Product ID, and Product Version Number for a device.

                            //  Accepts:
                            //  A handle returned by CreateFile.
                            //  A pointer to receive a HIDD_ATTRIBUTES structure.

                            //  Returns:
                            //  True on success, False on failure.
                            //  ***                            

                            success = Hid.HidD_GetAttributes(hidHandle, ref MyHid.DeviceAttributes);

                            if (success)
                            {
                                //  Find out if the device matches the one we're looking for.

                                if ((MyHid.DeviceAttributes.VendorID == myVendorID) && (MyHid.DeviceAttributes.ProductID == myProductID))
                                {
                                    //  Display the information in form's list box.

                                    updateStatusBar_HID("detected");
                                    lstResults.Items.Add("Device detected:");
                                    lstResults.Items.Add("  Vendor ID= " + Convert.ToString(MyHid.DeviceAttributes.VendorID, 16));
                                    lstResults.Items.Add("  Product ID = " + Convert.ToString(MyHid.DeviceAttributes.ProductID, 16));
                                    buttonClearInfo.Enabled = true;

                                    myDeviceDetected = true;

                                    //  Save the DevicePathName for OnDeviceChange().

                                    myDevicePathName = devicePathName[memberIndex];
                                }
                                else
                                {
                                    //  It's not a match, so close the handle.

                                    myDeviceDetected = false;
                                    hidHandle.Close();
                                }
                            }
                            else
                            {
                                //  There was a problem in retrieving the information.

                                myDeviceDetected = false;
                                hidHandle.Close();
                            }
                        }

                        //  Keep looking until we find the device or there are no devices left to examine.

                        memberIndex = memberIndex + 1;
                    }
                    while (!((myDeviceDetected || (memberIndex == devicePathName.Length))));
                }

                if (myDeviceDetected)
                {
                    //  The device was detected.
                    //  Register to receive notifications if the device is removed or attached.

                    success = MyDeviceManagement.RegisterForDeviceNotifications(myDevicePathName, FrmMy.Handle, hidGuid, ref deviceNotificationHandle);

                    //  Learn the capabilities of the device.

                    MyHid.Capabilities = MyHid.GetDeviceCapabilities(hidHandle);

                    if (success)
                    {
                        //  Find out if the device is a system mouse or keyboard.

                        hidUsage = MyHid.GetHidUsage(MyHid.Capabilities);

                        //  Get the Input report buffer size.

                        Int32 numberOfInputBuffers = 0;
                        try
                        {
                            MyHid.GetNumberOfInputBuffers(hidHandle, ref numberOfInputBuffers);
                        }
                        catch (Exception ex)
                        {
                            DisplayException(this.Name, ex);
                        }

                        //Close the handle and reopen it with read/write access.

                        hidHandle.Close();
                        hidHandle = FileIO.CreateFile(myDevicePathName, FileIO.GENERIC_READ | FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);

                        if (hidHandle.IsInvalid)
                        {
                            lstResults.Items.Add("The device is a system " + hidUsage + ".");
                            lstResults.Items.Add("Windows 2000 and Windows XP obtain exclusive access to Input and Output reports for this devices.");
                            lstResults.Items.Add("Applications can access Feature reports only.");
                            buttonClearInfo.Enabled = true;
                        }
                        else
                        {
                            if (MyHid.Capabilities.InputReportByteLength > 0)
                            {
                                //  Set the size of the Input report buffer. 

                                Byte[] inputReportBuffer = null;

                                inputReportBuffer = new Byte[MyHid.Capabilities.InputReportByteLength];

                                fileStreamDeviceData = new FileStream(hidHandle, FileAccess.Read | FileAccess.Write, inputReportBuffer.Length, false);
                            }

                            if (MyHid.Capabilities.OutputReportByteLength > 0)
                            {
                                Byte[] outputReportBuffer = null;
                                outputReportBuffer = new Byte[MyHid.Capabilities.OutputReportByteLength];
                            }

                            //  Flush any waiting reports in the input buffer. (optional)

                            MyHid.FlushQueue(hidHandle);
                        }
                    }
                }
                else
                {
                    //  The device wasn't detected.
                    lstResults.Items.Add("Device not found.");
                    updateStatusBar_HID("not found");
                    buttonClearInfo.Enabled = true;
                }
                ScrollToBottomOfListBox();
            }
            catch (Exception ex)
            {
                DisplayException(this.Name, ex);
            }
        }

        ///  <summary>
        ///   Overrides WndProc to enable checking for and handling WM_DEVICECHANGE messages.
        ///  </summary>
        ///  
        ///  <param name="m"> a Windows Message </param>
        protected override void WndProc(ref Message m)
        {
            try
            {
                //  The OnDeviceChange routine processes WM_DEVICECHANGE messages.
                if (m.Msg == DeviceManagement.WM_DEVICECHANGE)
                {
                    OnDeviceChange(m);
                }
                //  Let the base form process the message.
                base.WndProc(ref m);
            }
            catch (Exception ex)
            {
                DisplayException(this.Name, ex);
                throw;
            }
        }

        ///  <summary>
        ///  Called when a WM_DEVICECHANGE message has arrived,
        ///  indicating that a device has been attached or removed.
        ///  </summary>
        ///  
        ///  <param name="m"> a message with information about the device </param>
        private void OnDeviceChange(Message m)
        {
            try
            {
                if ((m.WParam.ToInt32() == DeviceManagement.DBT_DEVICEARRIVAL))
                {
                    //  If WParam contains DBT_DEVICEARRIVAL, a device has been attached.
                    //  Find out if it's the device we're communicating with.
                    if (MyDeviceManagement.DeviceNameMatch(m, myDevicePathName))
                    {
                        lstResults.Items.Add("My device attached.");
                        updateStatusBar_HID("attached");
                        buttonClearInfo.Enabled = true;
                    }
                }
                else if ((m.WParam.ToInt32() == DeviceManagement.DBT_DEVICEREMOVECOMPLETE))
                {
                    //  If WParam contains DBT_DEVICEREMOVAL, a device has been removed.
                    //  Find out if it's the device we're communicating with.
                    if (MyDeviceManagement.DeviceNameMatch(m, myDevicePathName))
                    {
                        lstResults.Items.Add("My device removed.");
                        updateStatusBar_HID("removed");
                        buttonClearInfo.Enabled = true;

                        //  Set MyDeviceDetected to False, so on the next data-transfer attempt
                        //  FindTheHid() will be called to look for the device and get a new handle.

                        FrmMy.myDeviceDetected = false;
                    }
                }
                ScrollToBottomOfListBox();
            }
            catch (Exception ex)
            {
                DisplayException(this.Name, ex);
                throw;
            }
        }

        // ******************************************************************************************************************************************
        // Helper methods
        // ******************************************************************************************************************************************

        ///  <summary>
        ///  Provides a central mechanism for exception handling.
        ///  Displays a message box that describes the exception.
        ///  </summary>
        ///  
        ///  <param name="moduleName"> the module where the exception occurred. </param>
        ///  <param name="e"> the exception </param>
        private void DisplayException(String moduleName, Exception e)
        {
            String message = null;
            String caption = null;

            //  Create an error message.

            message = "Exception: " + e.Message + ControlChars.CrLf + "Module: " + moduleName + ControlChars.CrLf + "Method: " + e.TargetSite.Name;
            //message = "Exception: " + e.Message + "\r\n" + "Module: " + moduleName + "\r\n" + "Method: " + e.TargetSite.Name;

            caption = "Unexpected Exception";

            MessageBox.Show(message, caption, MessageBoxButtons.OK);
        }

        private void showErrorDialog(String ErrorMsg)
        {
            FormErrorMsg.ErrorMsg = ErrorMsg;
            DialogErrorMsg.ShowDialog();
        }

        ///  <summary>
        //  Only accept Hexadecimal input in textbox
        ///  </summary>
        private void textBoxOnlyAcceptHexadecimalInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;

            if (c != '\b' && !((c <= 'F' && c >= 'A') || (c <= 'f' && c >= 'a') || (c >= '0' && c <= '9')))
            {
                e.Handled = true;
            }
        }

        ///  <summary>
        //  Only accept decimal input in textbox
        ///  </summary>
        private void textBoxOnlyAcceptDecimalInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;

            if (c != '\b' && !(c >= '0' && c <= '9'))
            {
                e.Handled = true;
            }
        }

        ///  <summary>
        ///  Converts a string representing a hexadecimal value to a Byte.
        ///  Invalid strings will return value 0 (zero).
        ///  </summary>
        private byte GetHexStringToByte(string value)
        {
            Byte b = 0;
            try
            {
                b = Byte.Parse(value, System.Globalization.NumberStyles.HexNumber);
            }
            catch (Exception ex) { }
            return b;
        }

        ///  <summary>
        ///  Converts a string representing a decimal value to an integer.
        ///  Invalid strings will return value 0 (zero).
        ///  </summary>
        private int GetDecStringToInt(string value)
        {
            int i = 0;
            try
            {
                i = int.Parse(value, System.Globalization.NumberStyles.Integer);
            }
            catch (Exception ex) { }
            return i;
        }

        private void ScrollToBottomOfListBox()
        {
            while (lstResults.Items.Count > 5000)
            {
                lstResults.Items.RemoveAt(0);
            }
            lstResults.SelectedIndex = lstResults.Items.Count - 1;
        }

        // ******************************************************************************************************************************************
        // Menu Items methods
        // ******************************************************************************************************************************************

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogAbout.ShowDialog();
        }

        // ******************************************************************************************************************************************
        // Status bar methods
        // ******************************************************************************************************************************************

        private void updateStatusBar_HID(String message)
        {
            toolStripStatusLabel_HID.Text = "DALI Master: " + message;
        }

        // ******************************************************************************************************************************************
        // HID communication
        // ******************************************************************************************************************************************

        private Byte[] outputReportBuffer = new Byte[3];
        private Byte[] inputReportBuffer = new Byte[3];
        private int dali_msg_count = 0;

        ///  <summary>
        ///  Sends a Dali command as an Output report.
        ///  Assumes report ID = 0 for report.
        ///  </summary>
        private bool WriteToDali(Byte daliAddress, Byte daliCommand, bool responseExpected, bool waitAfterSend, out Byte response, out Byte responseStatus)
        {
            bool answerReceived = false;
            String infoOutputString = null;
            Int32 loopcount = 0;

            response = 0;
            responseStatus = 0;
            dali_msg_count++;

            //  If the device hasn't been detected, was removed, or timed out on a previous attempt
            //  to access it, look for the device.

            if (!myDeviceDetected)
            {
                FindTheHid();
            }
            if (myDeviceDetected)
            {
                //  Don't attempt to exchange reports if valid handles aren't available
                //  (as for a mouse or keyboard under Windows 2000/XP.)

                if (hidHandle.IsInvalid)
                {
                    lstResults.Items.Add("Invalid handle. The device is probably a system mouse or keyboard.");
                    lstResults.Items.Add("No attempt to write an Output report or read an Input report was made.");
                    buttonClearInfo.Enabled = true;
                }
                else
                {
                    //  Store the report ID in the first byte of the buffer:
                    outputReportBuffer[0] = 0;
                    //  Store the report data following the report ID.
                    outputReportBuffer[1] = daliAddress;
                    outputReportBuffer[2] = daliCommand;

                    // If the HID has an interrupt OUT endpoint, the host uses an 
                    // interrupt transfer to send the report. 
                    // If not, the host uses a control transfer.

                    if (fileStreamDeviceData.CanWrite)
                    {
                        try
                        {
                            fileStreamDeviceData.Write(outputReportBuffer, 0, outputReportBuffer.Length);

                            //  Display the report data in the form's list box.

                            if (checkBoxTimestamp.Checked)
                            {
                                DateTime datetime = DateTime.Now;
                                infoOutputString = datetime.Hour.ToString("D2") + "h" +
                                                   datetime.Minute.ToString("D2") + "m" +
                                                   datetime.Second.ToString("D2") + "s" +
                                                   datetime.Millisecond.ToString("D3") + "ms : ";
                            }
                            else
                            {
                                infoOutputString = "";
                            }
                            infoOutputString += "Output report " + dali_msg_count.ToString() + " has been written:";
                            lstResults.Items.Add(infoOutputString);
                            infoOutputString = " Output Report Data : ";
                            infoOutputString += String.Format("{0:X2} ", outputReportBuffer[1]);
                            infoOutputString += String.Format("{0:X2} ", outputReportBuffer[2]);
                            lstResults.Items.Add(infoOutputString);
                            buttonClearInfo.Enabled = true;

                            if (waitAfterSend || responseExpected)
                            {
                                stopWatch.Reset();
                                stopWatch.Start();
                                while (stopWatch.ElapsedMilliseconds < 25) ;
                            }

                            if (responseExpected)
                            {
                                //  Read an Input report.

                                //  Don't attempt to send an Input report if the HID has no Input report.
                                //  (The HID spec requires all HIDs to have an interrupt IN endpoint,
                                //  which suggests that all HIDs must support Input reports.)

                                if (MyHid.Capabilities.InputReportByteLength > 0)
                                {
                                    //  Read a report using interrupt transfers.                

                                    if (fileStreamDeviceData.CanRead)
                                    {
                                        inputReportBuffer[0] = 0;
                                        inputReportBuffer[1] = 0;
                                        inputReportBuffer[2] = 0;
                                        do
                                        {
                                            fileStreamDeviceData.Read(inputReportBuffer, 0, inputReportBuffer.Length);
                                            if (inputReportBuffer[1] == 0)
                                            {
                                                stopWatch.Reset();
                                                stopWatch.Start();
                                                while (stopWatch.ElapsedMilliseconds < 15) ;
                                            }
                                            loopcount++;
                                        }
                                        while ((inputReportBuffer[1] == 0) && (loopcount < 40));

                                        infoOutputString = " Input  Report Data : ";
                                        infoOutputString += String.Format("{0:X2} ", inputReportBuffer[1]);
                                        infoOutputString += String.Format("{0:X2} ", inputReportBuffer[2]);
                                        lstResults.Items.Add(infoOutputString);
                                        buttonClearInfo.Enabled = true;

                                        responseStatus = inputReportBuffer[1];
                                        response = inputReportBuffer[2];
                                        switch (responseStatus)
                                        {
                                        case 0: // "No Data Available"
                                            break;
                                        case 1: // "No Answer"
                                            break;
                                        case 2: // "OK"
                                            answerReceived = true;
                                            break;
                                        case 3: // "Invalid Answer"
                                            answerReceived = true;
                                            break;
                                        default:
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        CloseCommunications();
                                        lstResults.Items.Add("The attempt to read an Input report has failed.");
                                        buttonClearInfo.Enabled = true;
                                    }
                                }
                                else
                                {
                                    lstResults.Items.Add("No attempt to read an Input report was made.");
                                    lstResults.Items.Add("The HID doesn't have an Input report.");
                                    buttonClearInfo.Enabled = true;
                                }
                            }
                        }
                        catch (IOException) { }
                        catch (OperationCanceledException) { }
                    }
                    else
                    {
                        CloseCommunications();
                        lstResults.Items.Add("The attempt to write an Output report failed.");
                        buttonClearInfo.Enabled = true;
                    }
                }
                ScrollToBottomOfListBox();
            }
            return answerReceived;
        }

        // ******************************************************************************************************************************************
        // Discovery TAB
        // ******************************************************************************************************************************************

        private discovery_state discoveryState = discovery_state.DISCOVERY_IDLE;
        private byte discoveryGroupAddress = 0;
        private uint discoverySearchAddrCurrent = 0;
        private uint discoverySearchAddrNew = 0;
        private uint discoverySearchAddrUpper = 0;
        private uint discoverySearchAddrLower = 0;
        private byte dev_addr = 0;

        private bool findDaliCmdDetails(dali_cmd_number cmd_nr, out byte addr, out byte code, out bool responseExpected)
        {
            addr = 0;
            code = 0;
            responseExpected = false;
            for (int i = 0; i < daliCmd.Length; i++)
            {
                if (cmd_nr == daliCmd[i].daliCommandNumber)
                {
                    addr = daliCmd[i].daliCommandAddr;
                    code = daliCmd[i].daliCommandCode;
                    responseExpected = daliCmd[i].daliExpectedResponse;
                    return true;
                }
            }
            return false;
        }

        private String getDaliDeviceTypeString(byte type)
        {
            String type_str = "";
            switch (type)
            {
                case 0:
                    type_str = "Fluorescent lamp";
                    break;
                case 1:
                    type_str = "Self-contained emergency lighting";
                    break;
                case 2:
                    type_str = "Discharge lamp";
                    break;
                case 3:
                    type_str = "Low voltage halogen lamp";
                    break;
                case 4:
                    type_str = "Supply Voltage controller for incandescent lamp";
                    break;
                case 5:
                    type_str = "Conversion from digital into DC volatge";
                    break;
                case 6:
                    type_str = "LED module";
                    break;
                case 7:
                    type_str = "Switching function";
                    break;
                case 8:
                    type_str = "Colour control";
                    break;
                case 9:
                    type_str = "Sequencer";
                    break;
                case 10:
                    type_str = "Optical control";
                    break;
                default:
                    type_str = "Unknown";
                    break;
            }
            return type_str;
        }

        private byte getNewDaliAddress()
        {
            byte newAddr = 0;

            while (newAddr < dataGridView1.RowCount)
            {
                if (newAddr < int.Parse(dataGridView1.Rows[newAddr].Cells[0].Value.ToString()))
                {
                    break;
                }
                newAddr++;
            }
            return newAddr;
        }

        private void createNewPlaceInDeviceList(uint shortAddress)
        {
            dataGridView1.Rows.Add();
            textBoxDaliDevices.Text = dataGridView1.RowCount.ToString();

            int index_in_list = 0;
            if (dataGridView1.RowCount > 1)
            {
                while (index_in_list < (dataGridView1.RowCount - 1))
                {
                    if (shortAddress < int.Parse(dataGridView1.Rows[index_in_list].Cells[0].Value.ToString()))
                    {
                        break;
                    }
                    index_in_list++;
                }
                for (int i = (dataGridView1.RowCount - 1); i > index_in_list; i--)
                {
                    dataGridView1.Rows[i].Cells[0].Value = dataGridView1.Rows[i - 1].Cells[0].Value;
                    dataGridView1.Rows[i].Cells[1].Value = dataGridView1.Rows[i - 1].Cells[1].Value;
                    dataGridView1.Rows[i].Cells[2].Value = dataGridView1.Rows[i - 1].Cells[2].Value;
                    dataGridView1.Rows[i].Cells[3].Value = dataGridView1.Rows[i - 1].Cells[3].Value;
                    dataGridView1.Rows[i].Cells[4].Value = dataGridView1.Rows[i - 1].Cells[4].Value;
                }
            }
            dataGridView1.Rows[index_in_list].Cells[0].Value = shortAddress.ToString();
            dataGridView1.Rows[index_in_list].Cells[1].Value = "unknown";
            dataGridView1.Rows[index_in_list].Cells[2].Value = "?";
            dataGridView1.Rows[index_in_list].Cells[3].Value = "?";
            dataGridView1.Rows[index_in_list].Cells[4].Value = "yes";
        }

        private void clearDaliDeviceList()
        {
            while (dataGridView1.RowCount > 0)
            {
                dataGridView1.Rows.RemoveAt(0);
            }
            textBoxDaliDevices.Text = dataGridView1.RowCount.ToString();
            buttonClear.Enabled = false;
        }

        private void setDaliDeviceListRandomAddr(uint shortAddress, uint randomAddr)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (shortAddress == int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()))
                {
                    dataGridView1.Rows[i].Cells[1].Value = randomAddr.ToString("X");
                    break;
                }
            }
        }

        private void setDaliDeviceListType(uint shortAddress, byte type)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (shortAddress == int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()))
                {
                    dataGridView1.Rows[i].Cells[2].Value = getDaliDeviceTypeString(type);
                    break;
                }
            }
        }

        private void setDaliDeviceListVersion(uint shortAddress, byte version)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (shortAddress == int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()))
                {
                    dataGridView1.Rows[i].Cells[3].Value = version.ToString();
                    break;
                }
            }
        }

        private void setDaliDeviceListConnected(uint shortAddress, bool connected)
        {
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                if (shortAddress == int.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()))
                {
                    if (connected)
                    {
                        dataGridView1.Rows[i].Cells[4].Value = "yes";
                    }
                    else
                    {
                        dataGridView1.Rows[i].Cells[4].Value = "no";
                    }
                    break;
                }
            }
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            clearDaliDeviceList();
        }

        private void buttonDiscover_Click(object sender, EventArgs e)
        {
            buttonDiscover.Enabled = false;
            buttonClear.Enabled = false;
            checkBoxNewInitialisation.Enabled = false;
            tabControl1.UseWaitCursor = true;
            groupBoxDaliMasterID.Enabled = false;
            groupBoxDaliCommandsCmd.Enabled = false;
            timer_query_level.Enabled = false;
            if (!myDeviceDetected)
            {
                FindTheHid();
            }
            discoveryState = discovery_state.DISCOVERY_START;
            timerDiscovery.Interval = 15; // continue as quick as possible
            timerDiscovery.Enabled = true;
        }

        private void timerDiscovery_Tick(object sender, EventArgs e)
        {
            bool daliExpectedResponse;
            byte daliCommandAddr, daliCommandCode;
            bool discoveryAnswerReceived;
            byte daliResponse, daliResponseStatus;
            uint i;

            timerDiscovery.Enabled = false;
            if (!myDeviceDetected)
            {
                discoveryState = discovery_state.DISCOVERY_STOP;
            }
            switch (discoveryState)
            {
                case discovery_state.DISCOVERY_START:
                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_TERMINATE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                    WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                    discoveryState = discovery_state.DISCOVERY_TERMINATE;
                    timerDiscovery.Interval = 30;
                    break;
                case discovery_state.DISCOVERY_TERMINATE:
                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_INITIALISE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                    WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                    discoveryState = discovery_state.DISCOVERY_INITIALISE;
                    timerDiscovery.Interval = 60;  // 'initialise' command is repeated by DALI master
                    break;
                case discovery_state.DISCOVERY_INITIALISE:
                    // check if new initialisation is requested
                    if (checkBoxNewInitialisation.Checked)
                    {
                        clearDaliDeviceList();
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_DATA_TRANSFER_REGISTER_DTR, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, 255, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        discoveryState = discovery_state.DISCOVERY_RESET_DTR;
                        timerDiscovery.Interval = 30;
                    }
                    else
                    {
                        // Remove no longer connected devices from the list,and add already initialised devices
                        byte index_in_list = 0;
                        for (i = 0; i < 64; i++)
                        {
                            bool i_in_list = false;
                            if (index_in_list < dataGridView1.RowCount)
                            {
                                dev_addr = byte.Parse(dataGridView1.Rows[index_in_list].Cells[0].Value.ToString());
                                if (i == (int)dev_addr)
                                {
                                    i_in_list = true;
                                }
                            }
                            findDaliCmdDetails(dali_cmd_number.DALI_CMD_QUERY_CONTROL_GEAR, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                            daliCommandAddr |= (byte)(i << 1);
                            discoveryAnswerReceived = WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                            if (discoveryAnswerReceived)
                            {
                                // when not in list, add it to list
                                if (!i_in_list)
                                {
                                    createNewPlaceInDeviceList(i);
                                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_QUERY_DEVICE_TYPE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                                    daliCommandAddr |= (byte)(i << 1);
                                    discoveryAnswerReceived = WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                                    if (discoveryAnswerReceived)
                                    {
                                        setDaliDeviceListType(i, daliResponse);
                                    }
                                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_QUERY_VERSION_NUMBER, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                                    daliCommandAddr |= (byte)(i << 1);
                                    discoveryAnswerReceived = WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                                    if (discoveryAnswerReceived)
                                    {
                                        setDaliDeviceListVersion(i, daliResponse);
                                    }
                                }
                                else
                                {
                                    setDaliDeviceListConnected(i, true);
                                }
                                index_in_list += 1;
                            }
                            else
                            {
                                // when in list, change connection state
                                if (i_in_list)
                                {
                                    setDaliDeviceListConnected(i, false);
                                    if (checkBoxRemove.Checked)
                                    {
                                        dataGridView1.Rows.RemoveAt(index_in_list);
                                        textBoxDaliDevices.Text = dataGridView1.RowCount.ToString();
                                    }
                                    else
                                    {
                                        index_in_list += 1;
                                    }
                                }
                            }
                        }
                        // now find not yet initialised devices
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_TERMINATE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, true, out daliResponse, out daliResponseStatus);
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_INITIALISE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, 255, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        discoveryGroupAddress = 15; // do not remove any devices from group
                        discoveryState = discovery_state.DISCOVERY_REMOVE_FROM_GROUP;
                        timerDiscovery.Interval = 60;
                    }
                    break;
                case discovery_state.DISCOVERY_RESET_DTR:
                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_STORE_DTR_AS_SHORT_ADDRESS, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                    WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                    discoveryState = discovery_state.DISCOVERY_STORE_DTR_AS_SHORT_ADDRESS;
                    timerDiscovery.Interval = 60;  // 'store_dtr' command is repeated by DALI master
                    break;
                case discovery_state.DISCOVERY_STORE_DTR_AS_SHORT_ADDRESS:
                    discoveryGroupAddress = 0;
                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_0, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                    WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                    discoveryState = discovery_state.DISCOVERY_REMOVE_FROM_GROUP;
                    timerDiscovery.Interval = 60;  // 'remove_from_group' command is repeated by DALI master
                    break;
                case discovery_state.DISCOVERY_REMOVE_FROM_GROUP:
                    discoveryGroupAddress += 1;
                    switch (discoveryGroupAddress)
                    {
                    case 1:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_1, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 2:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_2, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 3:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_3, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 4:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_4, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 5:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_5, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 6:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_6, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 7:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_7, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 8:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_8, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 9:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_9, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 10:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_10, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 11:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_11, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 12:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_12, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 13:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_13, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 14:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_14, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 15:
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_REMOVE_FROM_GROUP_15, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali((byte)(daliCommandAddr | 0xFE), daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        break;
                    case 16:
                        discoverySearchAddrCurrent = 0;
                        discoverySearchAddrNew = 0xFFFFFF;
                        discoverySearchAddrUpper = 0x01000000;
                        discoverySearchAddrLower = 0;
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_RANDOMISE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        discoveryState = discovery_state.DISCOVERY_SEARCH_ADDR;
                        timerDiscovery.Interval = 60;  // 'randomise' command is repeated by DALI master
                        break;
                    default:
                        break;
                    }
                    break;
                case discovery_state.DISCOVERY_SEARCH_ADDR:
                    if ((byte)((discoverySearchAddrCurrent >> 0) & 0xFF) != (byte)((discoverySearchAddrNew >> 0) & 0xFF))
                    {
                        discoverySearchAddrCurrent &= 0x00FFFF00;
                        discoverySearchAddrCurrent |= (discoverySearchAddrNew & 0x000000FF);
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_SEARCH_ADDR_L, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, (byte)((discoverySearchAddrCurrent >> 0) & 0xFF), daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        timerDiscovery.Interval = 30;
                    }
                    else if ((byte)((discoverySearchAddrCurrent >> 8) & 0xFF) != (byte)((discoverySearchAddrNew >> 8) & 0xFF))
                    {
                        discoverySearchAddrCurrent &= 0x00FF00FF;
                        discoverySearchAddrCurrent |= (discoverySearchAddrNew & 0x0000FF00);
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_SEARCH_ADDR_M, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, (byte)((discoverySearchAddrCurrent >> 8) & 0xFF), daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        timerDiscovery.Interval = 30;
                    }
                    else if ((byte)((discoverySearchAddrCurrent >> 16) & 0xFF) != (byte)((discoverySearchAddrNew >> 16) & 0xFF))
                    {
                        discoverySearchAddrCurrent &= 0x0000FFFF;
                        discoverySearchAddrCurrent |= (discoverySearchAddrNew & 0x00FF0000);
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_SEARCH_ADDR_H, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, (byte)((discoverySearchAddrCurrent >> 16) & 0xFF), daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        timerDiscovery.Interval = 30;
                    }
                    else
                    {
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_COMPARE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        discoveryAnswerReceived = WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        if (discoverySearchAddrCurrent == 0xFFFFFF)
                        {
                            if (discoveryAnswerReceived)
                            {
                                discoverySearchAddrNew = (discoverySearchAddrUpper - discoverySearchAddrLower) / 2;
                                timerDiscovery.Interval = 15; // continue as quick as possible
                            }
                            else
                            {
                                // Nobody answered, no more 'not yet found' DALI slaves connected on the bus
                                findDaliCmdDetails(dali_cmd_number.DALI_CMD_TERMINATE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                                WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                                discoveryState = discovery_state.DISCOVERY_STOP;
                                timerDiscovery.Interval = 30;
                            }
                        }
                        else
                        {
                            if (discoveryAnswerReceived)
                            {
                                discoverySearchAddrUpper = discoverySearchAddrCurrent;
                                if ((discoverySearchAddrUpper == (discoverySearchAddrLower + 1)) ||
                                    (discoverySearchAddrUpper == 0))
                                {
                                    // we found a DALI slave at discoverySearchAddrUpper
                                    if (dataGridView1.RowCount < 64)
                                    {
                                        byte new_dali_device_addr = getNewDaliAddress();
                                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_PROGRAM_SHORT_ADDRESS, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                                        WriteToDali(daliCommandAddr, (byte)((new_dali_device_addr << 1) | 0x01), daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                                        discoveryState = discovery_state.DISCOVERY_PROGRAM_SHORT_ADDRESS;
                                        timerDiscovery.Interval = 30;
                                    }
                                    else
                                    {
                                        showErrorDialog("Too many DALI devices on the bus. Discovery terminated.");
                                        // stop the search
                                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_TERMINATE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                                        WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                                        discoveryState = discovery_state.DISCOVERY_STOP;
                                        timerDiscovery.Interval = 30;
                                    }
                                }
                                else
                                {
                                    discoverySearchAddrNew = discoverySearchAddrUpper - ((discoverySearchAddrUpper - discoverySearchAddrLower) / 2);
                                    timerDiscovery.Interval = 15; // continue as quick as possible
                                }
                            }
                            else
                            {
                                // Nobody answered
                                discoverySearchAddrLower = discoverySearchAddrCurrent;
                                if (discoverySearchAddrUpper == (discoverySearchAddrLower + 1))
                                {
                                    // we found a DALI slave at discoverySearchAddrUpper
                                    discoverySearchAddrNew = discoverySearchAddrUpper;
                                }
                                else
                                {
                                    discoverySearchAddrNew = discoverySearchAddrLower + ((discoverySearchAddrUpper - discoverySearchAddrLower) / 2);
                                }
                                timerDiscovery.Interval = 15; // continue as quick as possible
                            }
                        }
                    }
                    break;
                case discovery_state.DISCOVERY_PROGRAM_SHORT_ADDRESS:
                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_QUERY_SHORT_ADDRESS, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                    discoveryAnswerReceived = WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                    if (discoveryAnswerReceived)
                    {
                        dev_addr = (byte)(daliResponse >> 1);
                        createNewPlaceInDeviceList(dev_addr);
                        setDaliDeviceListRandomAddr(dev_addr, discoverySearchAddrCurrent);
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_WITHDRAW, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        discoveryState = discovery_state.DISCOVERY_GET_INFO;
                    }
                    else
                    {
                        // This should not happen, lets stop the discovery process
                        findDaliCmdDetails(dali_cmd_number.DALI_CMD_TERMINATE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                        WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                        discoveryState = discovery_state.DISCOVERY_STOP;
                    }
                    timerDiscovery.Interval = 30;
                    break;
                case discovery_state.DISCOVERY_GET_INFO:
                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_QUERY_DEVICE_TYPE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                    daliCommandAddr |= (byte)(dev_addr << 1);
                    discoveryAnswerReceived = WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                    if (discoveryAnswerReceived)
                    {
                        setDaliDeviceListType(dev_addr, daliResponse);
                    }
                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_QUERY_VERSION_NUMBER, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                    daliCommandAddr |= (byte)(dev_addr << 1);
                    discoveryAnswerReceived = WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                    if (discoveryAnswerReceived)
                    {
                        setDaliDeviceListVersion(dev_addr, daliResponse);
                    }
                    // start search for next device
                    discoverySearchAddrCurrent = 0;
                    discoverySearchAddrNew = 0xFFFFFF;
                    discoverySearchAddrUpper = 0x01000000;
                    discoverySearchAddrLower = 0;
                    discoveryState = discovery_state.DISCOVERY_SEARCH_ADDR;
                    timerDiscovery.Interval = 15; // continue as quick as possible
                    break;
                case discovery_state.DISCOVERY_STOP:
                    findDaliCmdDetails(dali_cmd_number.DALI_CMD_TERMINATE, out daliCommandAddr, out daliCommandCode, out daliExpectedResponse);
                    WriteToDali(daliCommandAddr, daliCommandCode, daliExpectedResponse, false, out daliResponse, out daliResponseStatus);
                    discoveryState = discovery_state.DISCOVERY_IDLE;
                    tabControl1.UseWaitCursor = false;
                    buttonDiscover.Enabled = true;
                    checkBoxNewInitialisation.Enabled = true;
                    if (dataGridView1.RowCount > 0)
                    {
                        buttonClear.Enabled = true;
                    }
                    groupBoxDaliMasterID.Enabled = true;
                    groupBoxDaliCommandsCmd.Enabled = true;
                    break;
                default:
                    break;
            }
            if (discoveryState != discovery_state.DISCOVERY_IDLE)
            {
                timerDiscovery.Enabled = true;
            }
        }

        // ******************************************************************************************************************************************
        // Dali Commands TAB
        // ******************************************************************************************************************************************

        private void comboBoxDaliCmds_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = comboBoxDaliCmds.SelectedIndex;

            // For selected command, enable or disable entry fields.
            if (daliCmd[i].daliNeedsAddressing)
            {
                groupBoxDaliCommandsAddress.Enabled = true;
            }
            else
            {
                groupBoxDaliCommandsAddress.Enabled = false;
            }
            if (daliCmd[i].daliNeedsValue)
            {
                groupBoxDaliCommandDataValue.Enabled = true;
            }
            else
            {
                groupBoxDaliCommandDataValue.Enabled = false;
            }
        }

        private void hScrollBarDaliCommandsValue_Scroll(object sender, ScrollEventArgs e)
        {
            textBoxDaliCommandsValueDec.Text = hScrollBarDaliCommandsValue.Value.ToString();
            textBoxDaliCommandsValueHex.Text = hScrollBarDaliCommandsValue.Value.ToString("X");
        }

        private void textBoxDaliCommandsValueDec_TextChanged(object sender, EventArgs e)
        {
            textBoxDaliCommandsValueHex.Text = GetDecStringToInt(textBoxDaliCommandsValueDec.Text).ToString("X");
            hScrollBarDaliCommandsValue.Value = GetDecStringToInt(textBoxDaliCommandsValueDec.Text);
        }

        private void textBoxDaliCommandsValueHex_TextChanged(object sender, EventArgs e)
        {
            textBoxDaliCommandsValueDec.Text = GetHexStringToByte(textBoxDaliCommandsValueHex.Text).ToString();
            hScrollBarDaliCommandsValue.Value = GetHexStringToByte(textBoxDaliCommandsValueHex.Text);
        }

        private void radioButtonDaliAddress_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDaliAddressBroadcast.Checked)
            {
                numericUpDownDaliCommandGroup.Enabled = false;
                numericUpDownDaliCommandAddress.Enabled = false;
            }
            else if (radioButtonDaliAddressGroup.Checked)
            {
                numericUpDownDaliCommandGroup.Enabled = true;
                numericUpDownDaliCommandAddress.Enabled = false;
            }
            else
            {
                numericUpDownDaliCommandGroup.Enabled = false;
                numericUpDownDaliCommandAddress.Enabled = true;
            }
        }

        private void buttonDaliCommandsSend_Click(object sender, EventArgs e)
        {
            Byte DaliAddress, DaliCommand, DaliResponse, DaliResponseStatus;

            int i = comboBoxDaliCmds.SelectedIndex;

            textBoxDaliCommandsSendDec.Text = "";
            textBoxDaliCommandsSendHex.Text = "";
            textBoxDaliCommandsResponse.Text = "";
            textBoxDaliCommandsResponseDec.Text = "";
            textBoxDaliCommandsResponseHex.Text = "";

            if (daliCmd[i].daliNeedsAddressing)
            {
                if (radioButtonDaliAddressBroadcast.Checked)
                {
                    DaliAddress = (Byte)(daliCmd[i].daliCommandAddr | 0xFE);
                }
                else if (radioButtonDaliAddressGroup.Checked)
                {
                    DaliAddress = (Byte)(daliCmd[i].daliCommandAddr | (0x1E & (Byte)(numericUpDownDaliCommandGroup.Value * 2)) | 0x80);
                }
                else
                {
                    DaliAddress = (Byte)(daliCmd[i].daliCommandAddr | (0x7E & (Byte)(numericUpDownDaliCommandAddress.Value * 2)));
                }
            }
            else
            {
                DaliAddress = daliCmd[i].daliCommandAddr;
            }

            if (daliCmd[i].daliNeedsValue)
            {
                DaliCommand = (Byte)(daliCmd[i].daliCommandCode | (Byte)GetDecStringToInt(textBoxDaliCommandsValueDec.Text));
            }
            else
            {
                DaliCommand = daliCmd[i].daliCommandCode;
            }

            textBoxDaliCommandsSendDec.Text = DaliAddress.ToString() + " " + DaliCommand.ToString();
            textBoxDaliCommandsSendHex.Text = DaliAddress.ToString("X2") + " " + DaliCommand.ToString("X2");

            WriteToDali((Byte)(DaliAddress), DaliCommand, daliCmd[i].daliExpectedResponse, true, out DaliResponse, out DaliResponseStatus);
            if (daliCmd[i].daliExpectedResponse)
            {
                switch (DaliResponseStatus)
                {
                    case 0:
                        textBoxDaliCommandsResponse.Text = "No Data Available";
                        break;
                    case 1:
                        textBoxDaliCommandsResponse.Text = "No Answer";
                        break;
                    case 2:
                        textBoxDaliCommandsResponse.Text = "OK";
                        // fade time / fade rate
                        if (daliCmd[i].daliCommandNumber == dali_cmd_number.DALI_CMD_QUERY_FADE_TIME_FADE_RATE)
                        {
                            textBoxDaliCommandsResponse.Text = "Fade time: " + (DaliResponse >> 4).ToString() + ", fade rate: " + (DaliResponse & 0x0F).ToString() + ".";
                        }
                        textBoxDaliCommandsResponseDec.Text = DaliResponse.ToString();
                        textBoxDaliCommandsResponseHex.Text = DaliResponse.ToString("X2");
                        break;
                    case 3:
                        textBoxDaliCommandsResponse.Text = "Invalid Answer";
                        break;
                    default:
                        break;
                }
            }
            else
            {
                textBoxDaliCommandsResponse.Text = "No Answer Expected";
            }

        }

        // ******************************************************************************************************************************************
        // Monitoring TAB
        // ******************************************************************************************************************************************

        private void GetVendorAndProductIDsFromTextBoxes(ref Int32 myVendorID, ref Int32 myProductID)
        {
            myVendorID = 0x04cc;
            try
            {
                myVendorID = Int32.Parse(txtVendorID.Text, NumberStyles.AllowHexSpecifier);
            }
            catch (Exception ex) { }
            txtVendorID.Text = myVendorID.ToString("X4");

            myProductID = 0x0802;
            try
            {
                myProductID = Int32.Parse(txtProductID.Text, NumberStyles.AllowHexSpecifier);
            }
            catch (Exception ex) { }
            txtProductID.Text = myProductID.ToString("X4");
        }

        private void buttonClearInfo_Click(object sender, EventArgs e)
        {
            while (lstResults.Items.Count > 0)
            {
                lstResults.Items.RemoveAt(0);
            }
            buttonClearInfo.Enabled = false;
        }

        private void buttonFindDevice_Click(object sender, EventArgs e)
        {
            FindTheHid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Byte DaliAddress, DaliCommand, DaliResponse, DaliResponseStatus;

            DaliAddress = (Byte)(0xA3 | (0x7E & (Byte)(numericUpDown1.Value * 2)));
            DaliCommand = (Byte)(0x00 | (((((Byte)(numericUpDown2.Value))<<1)&0x7F)|0x01));

            WriteToDali((Byte)(DaliAddress), DaliCommand, false, true, out DaliResponse, out DaliResponseStatus);

            DaliAddress = (Byte)(0x01 | (0x7E & (Byte)(numericUpDown1.Value * 2)));
            DaliCommand = 0x80;

            WriteToDali((Byte)(DaliAddress), DaliCommand, false, true, out DaliResponse, out DaliResponseStatus);
        }

    }
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  