public static byte[] DirectCommand(ByteCodeBuffer bytecodes, int globalbytes, int localbytes) { try { lock (sync) { // if not already done, memorize the start time and fire up the communication MemorizeStartTime(); EstablishConnection(); // if requested to only queue the command, and the queue is not too full, and there is no expected response if (queue_active && globalbytes == 0 && queue.Length + bytecodes.Length < 900) // prevent to exceed 1024 bytes in single transmission { bytecodes.CopyTo(queue); queue_locals = Math.Max(queue_locals, localbytes); queue_active = false; return(new byte[0]); } else { queue_active = false; // remove queue-status in any case } // if there is data in the queue, try to merge it with the new command if (queue.Length > 0) { // when total length is not too big, can send together with new command if (queue.Length + bytecodes.Length < 900) { bytecodes.CopyTo(queue); byte[] response = con.DirectCommand(queue, globalbytes, Math.Max(localbytes, queue_locals)); queue.Clear(); queue_locals = 0; return(response); } // if can not be merged, send queued commands seperately con.DirectCommand(queue, 0, queue_locals); queue.Clear(); queue_locals = 0; } // finally execute the command return(con.DirectCommand(bytecodes, globalbytes, localbytes)); } } catch (Exception) { // no connection - must terminate immediately System.Environment.Exit(1); } return(null); }
// 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(); } }
private static EV3Connection TestConnection(EV3Connection con) { try { // perform a tiny direct command to check if communication works ByteCodeBuffer c = new ByteCodeBuffer(); c.OP(0x30); // Move8_8 c.CONST(74); c.GLOBVAR(0); byte[] response = con.DirectCommand(c, 1, 0); if (response == null || response.Length != 1 || response[0] != 74) { throw new Exception("Test DirectCommand delivers wrong result"); } return(con); } catch (Exception e) { con.Close(); throw e; } }