Example #1
0
        /// <summary>
        /// Reads the type and mode of the connected device
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <param name="port">Port [0-31]</param>
        /// <returns>TypeMode</returns>
        /// <remarks>
        /// Instruction opInput_Device (CMD, …)
        /// Opcode 0x99
        /// CMD: GET_TYPEMODE = 0x05
        /// Arguments
        /// (Data8) LAYER – Specify chain layer number [0-3]
        /// (Data8) NO – Port number
        /// Returns
        /// (Data8) TYPE – See device type list (Please reference section 0)
        /// (Data8) MODE – Device mode [0-7]
        /// </remarks>
        internal static async Task <DeviceTypeMode> GetTypeMode(ISocket socket, int port)
        {
            if (port < 0 || port > 31)
            {
                throw new ArgumentException("Number of port must be between 0 and 31", nameof(port));
            }
            ChainLayer layer = GetLayer(port);

            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 2, 0))
            {
                cb.OpCode(OP.opINPUT_DEVICE);
                cb.Raw((byte)INPUT_DEVICE_SUBCODE.GET_TYPEMODE);
                cb.PAR8((byte)layer);
                cb.PAR8((byte)port);
                cb.GlobalIndex(0);
                cb.GlobalIndex(1);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            byte[] data = response.PayLoad;
            return(new DeviceTypeMode((DeviceType)data[0], (DeviceMode)data[1]));
        }
Example #2
0
        /// <summary>
        /// Read information about external device
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <param name="port">Port number [0-31]</param>
        /// <returns>Format</returns>
        /// <remarks>
        /// Instruction opInput_Device (CMD, …)
        /// Opcode 0x99
        /// CMD: GET_FORMAT = 0x02
        /// Arguments
        /// (Data8) LAYER – Specify chain layer number [0-3]
        /// (Data8) NO – Port number
        /// Returns
        /// (Data8) Datasets – Number of data sets
        /// (Data8) FORMAT – Format [0-3], (0: 8-bit, 1: 16-bit, 2: 32-bit, 3: Float point)
        /// (Data8) MODES – Number of modes [1-8]
        /// (Data8) VIEW – Number of modes visible within port view app [1-8]
        /// </remarks>
        internal static async Task <Format> GetFormat(ISocket socket, ChainLayer layer, int port)
        {
            if (port < 0 || port > 31)
            {
                throw new ArgumentException("Number of port must be between 0 and 31", nameof(port));
            }

            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 4, 0))
            {
                cb.OpCode(OP.opINPUT_DEVICE);
                cb.Raw((byte)INPUT_DEVICE_SUBCODE.GET_FORMAT);
                cb.PAR8((byte)layer);
                cb.PAR8((byte)port);
                cb.GlobalIndex(0);
                cb.GlobalIndex(1);
                cb.GlobalIndex(2);
                cb.GlobalIndex(3);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            byte[] data = response.PayLoad;
            return(new Format(data[0], (DataType)data[1], data[2], data[3]));
        }
Example #3
0
        /// <summary>
        /// Gets the connection type for the given port
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <param name="port">Port number [0 - 31]</param>
        /// <returns></returns>
        /// <remarks>
        /// Instruction opInput_Device (CMD, …)
        /// Opcode 0x99
        /// CMD: GET_CONNECTION = 0x0C
        /// Arguments
        /// (Data8) LAYER – Specify chain layer number [0-3]
        /// (Data8) NO – Port number
        /// Returns
        /// (Data8) CONN – Connection type
        /// </remarks>
        internal static async Task <ConnectionType> GetConnection(ISocket socket, int port)
        {
            if (port < 0 || port > 31)
            {
                throw new ArgumentException("Number of port must be between 0 and 31", "port");
            }
            ChainLayer layer = GetLayer(port);

            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 1, 0))
            {
                cb.OpCode(OP.opINPUT_DEVICE);
                cb.Raw((byte)INPUT_DEVICE_SUBCODE.GET_CONNECTION);
                cb.PAR8((byte)layer);
                cb.PAR8((ushort)port);
                cb.GlobalIndex(0);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            ConnectionType type = ConnectionType.CONN_UNKNOW;

            if (response.Type == ResponseType.OK)
            {
                byte[] data = response.PayLoad;
                if (data.Length > 0)
                {
                    type = (ConnectionType)data[0];
                }
            }
            return(type);
        }
        /// <summary>
        /// Gets the memory usage total and free
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <returns>MemoryInfo</returns>
        /// <exception cref="FirmwareException"/>
        public static async Task <MemoryInfo> GetMemoryInfo(ISocket socket)
        {
            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 8, 0))
            {
                cb.OpCode(OP.opMEMORY_USAGE);
                cb.GlobalIndex(0);
                cb.GlobalIndex(4);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            byte[] data  = response.PayLoad;
            int    total = BitConverter.ToInt32(data, 0);
            int    free  = BitConverter.ToInt32(data, 4);

            return(new MemoryInfo(total, free));
        }
Example #5
0
        /// <summary>
        /// Enables a program to read all available devices on input chain
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <param name="maximum">Maximum number of devices types (Normally 32)</param>
        /// <returns>List of devices on chain and ports</returns>
        /// <remarks>
        /// Instruction opInput_Device_List (LENGTH, ARRAY, CHANGED)
        /// Opcode 0x98
        /// Arguments (Data8) LENGTH – Maximum number of devices types (Normally 32)
        /// Return (Data8) ARRAY – First element of data8 array of types (Normally 32)
        /// (Data8) CHANGED – Changed status
        /// Dispatch status Unchanged
        /// Description Enables a program to read all available devices on input chain
        /// </remarks>
        internal static async Task <IEnumerable <PortInfo> > PortScan(ISocket socket, ushort maximum = 32)
        {
            if (maximum < 1 || maximum > 32)
            {
                throw new ArgumentException("Maximum number of devices must be between 1 and 32", nameof(maximum));
            }
            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, maximum, 0))
            {
                cb.OpCode(OP.opINPUT_DEVICE_LIST);
                cb.PAR8(maximum);
                cb.GlobalIndex(0);
                cb.GlobalIndex(1);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            List <PortInfo> list = new List <PortInfo>();

            byte[] data = response.PayLoad;
            for (int i = 0; i < maximum; i++) //32 = 4 bricks * 8 ports.
            {
                int d = data[i];
                if (data[i] <= 120 && Enum.IsDefined(typeof(DeviceType), d))
                {
                    list.Add(new PortInfo(i, (DeviceType)d));
                }
                else
                {
                    list.Add(new PortInfo(i, d));
                }
            }

            //TODO full return includes change status
            //(Data8) ARRAY – First element of data8 array of types (Normally 32) (Data8) CHANGED – Changed status
            return(list);
        }
        /// <summary>
        /// Gets directoryinfo item count and total size
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <param name="brickDirectoryPath">relative path to folder on brick</param>
        /// <returns>DirectoryInfo</returns>
        /// <exception cref="ArgumentException"/>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="FirmwareException"/>
        public static async Task <DirectoryInfo> GetDirectoryInfo(ISocket socket, string brickDirectoryPath)
        {
            brickDirectoryPath = FileSystem.ToBrickDirectoryPath(brickDirectoryPath);
            brickDirectoryPath.IsBrickDirectoryPath();

            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 8, 0))
            {
                cb.OpCode(OP.opFILENAME);
                cb.Raw((byte)FILENAME_SUBCODE.TOTALSIZE);
                cb.PARS(brickDirectoryPath);
                cb.GlobalIndex(0);
                cb.GlobalIndex(4);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            byte[] data  = response.PayLoad;
            int    items = BitConverter.ToInt32(data, 0);
            int    size  = BitConverter.ToInt32(data, 4);

            return(new DirectoryInfo(items, size));
        }
Example #7
0
        ///// <summary>
        ///// This function enables reading current motor speed and tacho count level.
        ///// </summary>
        ///// <param name="socket">socket for executing command to brick</param>
        ///// <param name="layer">Specify chain layer number [0 - 3]</param>
        ///// <param name="port">Port number [0 - 3]</param>
        ///// <remarks>
        ///// Instruction opOutput_Read (LAYER, NO, *SPEED, *TACHO)
        ///// Opcode 0xA8
        ///// Arguments (Data8) LAYER – Specify chain layer number [0 - 3]
        ///// (Data8) NO – Port number [0 - 3]
        ///// (Data8) *SPEED – Output speed level detected, [-100 - 100]
        ///// (Data32) *TACHO – Current output tacho count
        ///// Dispatch status Unchanged
        ///// Description This function enables reading current motor speed and tacho count level.
        ///// </remarks>
        //public static async Task Read(ISocket socket, ChainLayer layer, OutputPortName port)
        //{
        //    Command cmd = null;
        //    using (CommandBuilder cb = new CommandBuilder(DIRECT_COMMAND_TYPE.DIRECT_COMMAND_NO_REPLY,0,2))
        //    {
        //        cb.OpCode(OpCode.opOutput_Read);
        //        cb.Argument((byte)layer);
        //        cb.Argument((byte)port);
        //        cb.LocalIndex(0);
        //        cb.LocalIndex32(1);
        //        cmd = cb.ToCommand();
        //    }
        //    await socket.Execute(cmd);

        //    CommandReply reply = cmd.Reply;
        //}

        /// <summary>
        /// This function enables the program to test if a output port is busy.
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <param name="layer">Specify chain layer number [0 - 3]</param>
        /// <param name="ports">Output bit field [0x00 – 0x0F]</param>
        /// <remarks>
        /// Instruction opOutput_Test (LAYER, NOS, BUSY)
        /// Opcode 0xA9
        /// Arguments (Data8) LAYER – Specify chain layer number [0 - 3]
        /// (Data8) NOS – Output bit field [0x00 – 0x0F]
        /// Return (Data8) BUSY – Output busy flag, [0 = Ready, 1 = Busy]
        /// Dispatch status Unchanged
        /// Description This function enables the program to test if a output port is busy.
        /// </remarks>
        public static async Task <bool> IsBusy(ISocket socket, ChainLayer layer, OutputPortFlag ports)
        {
            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 1, 0))
            {
                cb.OpCode(OP.opOUTPUT_TEST);
                cb.PAR8((int)layer);
                cb.PAR8((int)ports);
                cb.GlobalIndex(0);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            return(BitConverter.ToBoolean(response.PayLoad, 0));
        }
        /// <summary>
        /// Check if file or folder exists
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <param name="brickPath">relative path to file or folder on brick</param>
        /// <returns><c>true</c> if file or folder exists otherwise <c>false</c></returns>
        /// <exception cref="ArgumentException"/>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="FirmwareException"/>
        public static async Task <bool> Exists(ISocket socket, string brickPath)
        {
            brickPath.IsBrickPath();

            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 1, 0))
            {
                cb.OpCode(OP.opFILENAME);
                cb.Raw((byte)FILENAME_SUBCODE.EXIST);
                cb.PARS(brickPath);
                cb.GlobalIndex(0);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            return(response.PayLoad[0] == 1);
        }
Example #9
0
        /// <summary>
        /// Gets the Operating System version
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <returns></returns>
        public static async Task <string> GetOSVersion(ISocket socket)
        {
            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 255, 0))
            {
                cb.OpCode(OP.opUI_READ);
                cb.Raw((byte)UI_READ_SUBCODE.GET_OS_VERS);
                cb.PAR32(255);
                cb.GlobalIndex(0);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            byte[] data  = response.PayLoad;
            int    index = Array.IndexOf(data, (byte)0);

            return(Encoding.UTF8.GetString(data, 0, index));
        }
        /// <summary>
        /// This function enables the program to test if sound is busy (Playing sound or tone)
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <returns>Output busy flag, [0 = Ready, 1 = Busy]</returns>
        /// <remarks>
        /// Instruction opSound_Test (BUSY)
        /// Opcode 0x95
        /// Arguments
        /// Return (Data8) BUSY – Output busy flag, [0 = Ready, 1 = Busy]
        /// Dispatch status Unchanged
        /// Description This function enables the program to test if sound is busy (Playing sound or tone)
        /// </remarks>
        internal static async Task <bool> Test(ISocket socket)
        {
            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_REPLY, 1, 0))
            {
                cb.OpCode(OP.opSOUND_TEST);
                cb.GlobalIndex(0);
                cmd = cb.ToCommand();
            }
            Response response = await socket.Execute(cmd);

            bool isBusy = false;

            if (response.Type == ResponseType.OK)
            {
                byte[] data = response.PayLoad;
                isBusy = BitConverter.ToBoolean(data, 0);
            }
            return(isBusy);
        }