// 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); } }
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; } }