Example #1
0
        // must be called while holding a lock on sync
        private static void EstablishConnection()
        {
            // the first action also involves passing some SystemCommands and DirectCommands
            if (con == null)
            {
                // try to start up connection (when failing, an exception is thrown)
                con = ConnectionFinder.CreateConnection(false, true);

                String filename = "/tmp/EV3-Basic Session.rbf";

                // download the watchdog program as a file to the brick
                con.CreateEV3File(filename, HexDumpToBytes(EV3Communication.Properties.Resources.WatchDog));

                // before loading the watchdog program, check if there is no other program running
                ByteCodeBuffer c = new ByteCodeBuffer();
                c.OP(0x0C);            // opProgram_Info
                c.CONST(0x16);         // CMD: GET_STATUS = 0x16
                c.CONST(1);            // program slot 1 = user slot
                c.GLOBVAR(8);

                // load and start it
                c.OP(0xC0);       // opFILE
                c.CONST(0x08);    // CMD: LOAD_IMAGE = 0x08
                c.CONST(1);       // slot 1 = user program slot
                c.STRING(filename);
                c.GLOBVAR(0);
                c.GLOBVAR(4);
                c.OP(0x03);       // opPROGRAM_START
                c.CONST(1);       // slot 1 = user program slot
                c.GLOBVAR(0);
                c.GLOBVAR(4);
                c.CONST(0);

                // after starting, check if indeed running now
                c.OP(0x0C);            // opProgram_Info
                c.CONST(0x16);         // CMD: GET_STATUS = 0x16
                c.CONST(1);            // program slot 1 = user slot
                c.GLOBVAR(9);

                // as additional startup-action, reset the hardware (sensors and motors)
                c.OP(0x99);            // opInput_Device (CMD, …)
                c.CONST(0x0A);         // CLR_ALL = 0x0A
                c.CONST(-1);           // LAYER – Specify chain layer number [0-3] (-1 = All)

                byte[] response = con.DirectCommand(c, 10, 0);
                if (response == null || response[8] != 0x0040 || response[9] == 0x0040)
                {
                    throw new Exception("Could not start EV3 remote client on device");
                }

                // set up local ping thread to periodically send a command to the watchdog
                // program to keep it alive (and check if the brick is still operating)
                (new System.Threading.Thread(runpings)).Start();
            }
        }
Example #2
0
        public static void CreateEV3File(String fullname, byte[] content)
        {
            try
            {
                lock (sync)
                {
                    // if not already done, memorize the start time and fire up the communication
                    MemorizeStartTime();
                    EstablishConnection();

                    // finally execute the command
                    con.CreateEV3File(fullname, content);
                }
            }
            catch (Exception)
            {   // no connection or other severe error - must terminate immediately
                System.Environment.Exit(1);
            }
        }