public static byte[] ReadEV3File(String fullname) { try { lock (sync) { // if not already done, memorize the start time and fire up the communication MemorizeStartTime(); EstablishConnection(); // prepare the pinger packet to keep the remote-controll target alive during large file transfer ByteCodeBuffer c = new ByteCodeBuffer(); c.OP(0x3A); // Move32_32 c.CONST(42); // move this value c.GLOBVAR(0); // to global variable 0-3 c.OP(0x7E); // Memory_Write c.CONST(1); // program slot 1 = user slot c.CONST(0); // write to global variables c.CONST(0); // to global variable 0 c.CONST(4); // write 4 bytes c.GLOBVAR(0); // take the prepared value 42 // finally execute the command return(con.ReadEV3File(fullname, c)); } } catch (Exception) { // no connection or other severe error - must terminate immediately System.Environment.Exit(1); } return(null); }
// Periodically modify a flag in the global variables of program slot 1. // The program in slot 1 is a watchdog program that will notice when this "pings" no longer // arrive and will stop itself and shut down motors and sensors gracefully. // On the other side, the pinger will monitor if the watchdog is still running (could have // been stopped manually by the user) and if so, terminates the Basic program also. private static void runpings() { ByteCodeBuffer c = new ByteCodeBuffer(); c.OP(0x3A); // Move32_32 c.CONST(42); // move this value c.GLOBVAR(0); // to gloval variable 0-3 c.OP(0x7E); // Memory_Write c.CONST(1); // program slot 1 = user slot c.CONST(0); // write to global variables c.CONST(0); // to global variable 0 c.CONST(4); // write 4 bytes c.GLOBVAR(0); // take the prepared value 42 c.OP(0x0C); // opProgram_Info c.CONST(0x16); // CMD: GET_STATUS = 0x16 c.CONST(1); // program slot 1 = user slot c.GLOBVAR(0); for (; ;) { lock (sync) { byte[] packet = null; try { packet = con.DirectCommand(c, 4, 0); } catch (Exception e) { throw e; } // detected communication error or watchdog progam is no longer running if (packet == null || packet.Length <= 0 || packet[0] == 0x40) { System.Environment.Exit(1); } } System.Threading.Thread.Sleep(500); } }
// 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; } }