// 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(Clever.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; } }
public static EV3Connection CreateConnection(bool isUIThread, bool automaticallyUseSingleUSB) { // return new EV3ConnectionWiFi("10.0.0.140"); // retry multiple times to open connection for (; ;) { // check which EV3 devices are connected via USB int[] usbdevices = EV3ConnectionUSB.FindEV3s(); // if there is exactly one, try to open it if (automaticallyUseSingleUSB && usbdevices.Length == 1) { try { return(TestConnection(new EV3ConnectionUSB(usbdevices[0]))); } catch (Exception) { } } // when not able to open the one single USB connection, try also the serial ports String[] ports = System.IO.Ports.SerialPort.GetPortNames(); // in this case also check if there are some IP addresses configured as possible connection targets IPAddress[] addresses = LoadPossibleIPAddresses(); // because of a strange bug in .net 3.5, sometimes the port name gets an extra letter of unpredicable content - try to fix it in some cases for (int i = 0; i < ports.Length; i++) { String n = ports[i]; if (n.StartsWith("COM")) { char last = n[n.Length - 1]; // trim away last letter if it is not a digit (but this does not always help) if (last < '0' || last > '9') { ports[i] = n.Substring(0, n.Length - 1); } } } Array.Sort(ports, StringComparer.InvariantCulture); // Create and show the window to select one of the connection possibilities object port_or_device = DoModalConnectionTypeDialog(isUIThread, usbdevices, ports, addresses); if (port_or_device == null) { throw new Exception("User canceled connection selection"); } try { if (port_or_device is String) { EV3Connection c = TestConnection(new EV3ConnectionBluetooth((String)port_or_device)); SavePreferredConnection((String)port_or_device); return(c); } else if (port_or_device is int) { EV3Connection c = TestConnection(new EV3ConnectionUSB((int)port_or_device)); SavePreferredConnection("USB " + port_or_device); return(c); } else if (port_or_device is IPAddress) { IPAddress addr = (IPAddress)port_or_device; EV3Connection c = TestConnection(new EV3ConnectionWiFi(addr)); if (!addresses.Contains(addr)) { AddPossibleIPAddress(addr); } SavePreferredConnection(addr.ToString()); return(c); } } catch (Exception) { } // if not possible, retry } }