/// <summary> /// Connects to a device using native USB. Throws an exception if it fails. /// </summary> void Connect() { if (device != null) { // We are already connected. return; } // Get a list of all connected devices of this type. List <DeviceListItem> connectedDevices = Smc.getConnectedDevices(); foreach (DeviceListItem dli in connectedDevices) { // If you have multiple devices connected and want to select a particular // device by serial number, you could simply add a line like this: // if (dli.serialNumber != "39FF-7406-3054-3036-4923-0743"){ continue; } this.device = new Smc(dli); // Connect to the device. StatusLabel.Text = "Connected"; StatusLabel.ForeColor = SystemColors.ControlText; return; } throw new Exception("Could not find device. Make sure it is plugged in to USB " + "and check your Device Manager (Windows) or run lsusb (Linux)."); }
/// <summary> /// Connects to the device if it is found in the device list. /// </summary> public void TryToReconnect() { foreach (DeviceListItem d in Smc.getConnectedDevices()) { if (d.serialNumber == serialNumber) { motor = new Smc(d); return; } } throw new Exception("Cannot connect to the SMC Make sure it is plugged in to USB " + "and check your Device Manager (Windows)"); }
/// <summary> /// Connects to a Simple Motor Controller using native USB and returns the /// Smc object representing that connection. When you are done with the /// connection, you should close it using the Dispose() method so that other /// processes or functions can connect to the device later. The "using" /// statement can do this automatically for you. /// </summary> Smc connectToDevice() { // Get a list of all connected devices of this type. List <DeviceListItem> connectedDevices = Smc.getConnectedDevices(); foreach (DeviceListItem dli in connectedDevices) { // If you have multiple devices connected and want to select a particular // device by serial number, you could simply add a line like this: // if (dli.serialNumber != "39FF-7406-3054-3036-4923-0743"){ continue; } Smc device = new Smc(dli); // Connect to the device. return(device); // Return the device. } throw new Exception("Could not find device. Make sure it is plugged in to USB " + "and check your Device Manager (Windows) or run lsusb (Linux)."); }
/// <summary> /// Detects whether the device we are connected to is still plugged in /// to USB. If it is unplugged, we close the connection. /// </summary> void DetectDisconnect() { // Get a list of all connected devices of this type. List <DeviceListItem> connectedDevices = Smc.getConnectedDevices(); foreach (DeviceListItem dli in connectedDevices) { if (dli.serialNumber == device.getSerialNumber()) { // The device we are connected to is still plugged in. return; } } // The device we are connected to is not plugged in, so disconnect. Disconnect(); }
public static void listDevices() { List <DeviceListItem> list = Smc.getConnectedDevices(); if (list.Count == 1) { Console.WriteLine("1 " + Smc.name + " found:"); } else { Console.WriteLine(list.Count + " " + Smc.name + "s found:"); } foreach (DeviceListItem item in list) { Console.WriteLine(item.text); } }
public List <DeviceListItem> getConnectedDevices() { try { try { devices = Smc.getConnectedDevices(); } catch (NotImplementedException) { // use vendor and product instead } } catch (Exception e) { throw new Exception("There was an error getting the list of connected devices.", e); } return(devices); }
public static void listDevices() { List <DeviceListItem> list = Smc.getConnectedDevices(); // Note: This format is kind of annoying but we use it for compatibility with old // SmcCmd format. For future products, we should use a CSV format similar to // the one used by jrk2cmd. if (list.Count == 1) { Console.WriteLine("1 " + Smc.name + " found:"); } else { Console.WriteLine(list.Count + " " + Smc.namePlural + " found:"); } foreach (DeviceListItem item in list) { Console.WriteLine(item.text); } }
/// <summary> /// This funnction does all the real work. It will /// throw an exception if anything goes wrong. /// </summary> static void MainWithExceptions(string[] args) { // Parse all the command-line arguments, turning them in to a list of // actions to be taken. No actions on the device will occur until // all the arguments have been processed and validated. List <Action> actions = new List <Action>(); List <ActionOnDevice> actionsOnDevice = new List <ActionOnDevice>(); // If the user specifies a device serial number, it will be stored here. String specifiedSerialNumber = null; // Create a list object because it is easier to work with. List <String> argList = new List <String>(args); // Search for the -f option. This must be done ahead of time so that // it can be any place in the command line. forceOption = argList.Contains("-f"); argEnumerator = argList.GetEnumerator(); while (argEnumerator.MoveNext()) { // Read the next argument and decide what to do. switch (argEnumerator.Current) { case "-f": // Ignore, because we already searched for "-f". break; case "-l": case "--list": actions.Add(listDevices); break; case "-d": case "--device": if (specifiedSerialNumber != null) { throw new Exception("Only one -d/--device argument is alllowed."); } // Remove the leading # sign. It is not standard to put it there, // but if someone writes it, this program should still work. specifiedSerialNumber = nextArgument().TrimStart('#'); break; case "-s": case "--status": actionsOnDevice.Add(printStatus); break; case "--stop": actionsOnDevice.Add(stop); break; case "--resume": actionsOnDevice.Add(resume); break; case "--speed": actionsOnDevice.Add(laterSetSpeed()); break; case "--brake": actionsOnDevice.Add(laterSetBrake()); break; case "--restoredefaults": actionsOnDevice.Add(restoreDefaults); break; case "--configure": actionsOnDevice.Add(laterConfigure()); break; case "--getconf": actionsOnDevice.Add(laterGetConf()); break; case "--bootloader": actionsOnDevice.Add(startBootloader); break; case "--max-speed": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxSpeed)); break; case "--max-accel": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxAcceleration)); break; case "--max-decel": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxDeceleration)); break; case "--brake-dur": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.BrakeDuration)); break; case "--max-speed-forward": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxSpeed | SmcMotorLimit.ForwardOnly)); break; case "--max-accel-forward": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxAcceleration | SmcMotorLimit.ForwardOnly)); break; case "--max-decel-forward": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxDeceleration | SmcMotorLimit.ForwardOnly)); break; case "--brake-dur-forward": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.BrakeDuration | SmcMotorLimit.ForwardOnly)); break; case "--max-speed-reverse": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxSpeed | SmcMotorLimit.ReverseOnly)); break; case "--max-accel-reverse": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxAcceleration | SmcMotorLimit.ReverseOnly)); break; case "--max-decel-reverse": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.MaxDeceleration | SmcMotorLimit.ReverseOnly)); break; case "--brake-dur-reverse": actionsOnDevice.Add(laterSetMotorLimit(SmcMotorLimit.BrakeDuration | SmcMotorLimit.ReverseOnly)); break; default: throw new ArgumentException("Unrecognized argument \"" + argEnumerator.Current + "\"."); } } if (actions.Count == 0 && actionsOnDevice.Count == 0) { throw new ArgumentException("No actions specified."); } // Perform all actions that don't require being connected to a device. foreach (Action action in actions) { action(); } if (actionsOnDevice.Count == 0) { // There are no actions that require a device, so exit successfully. return; } // Find the right device to connect to. List <DeviceListItem> list = Smc.getConnectedDevices(); DeviceListItem item = null; if (specifiedSerialNumber == null) { // No serial number specified: connect to the first item in the list. if (list.Count > 0) { item = list[0]; } } else { // Find the device with the specified serial number. foreach (DeviceListItem checkItem in list) { if (checkItem.serialNumber == specifiedSerialNumber) { item = checkItem; break; } } } if (item == null && actionsOnDevice.Count == 1 && actionsOnDevice[0] == startBootloader) { // The correct device was not found, but all the user wanted to do was enter // bootloader mode so we should see if the device is connected in bootloader // mode and report success if that is true. List <DeviceListItem> bootloaders = Smc.getConnectedBootloaders(); if (specifiedSerialNumber == null) { if (bootloaders.Count > 0) { item = bootloaders[0]; } } else { // Find the device with the specified serial number. foreach (DeviceListItem checkItem in bootloaders) { if (checkItem.serialNumber.Replace("-", "") == specifiedSerialNumber.Replace("-", "")) { item = checkItem; break; } } } if (item == null) { if (specifiedSerialNumber == null) { throw new Exception("No " + Smc.namePlural + " (or bootloaders) found."); } else { throw new Exception("Could not find a device or bootloader with serial number " + specifiedSerialNumber + ".\n" + "To list devices, use the --list option."); } } Console.WriteLine("The device is already in bootloader mode."); return; } if (item == null) { if (specifiedSerialNumber == null) { throw new Exception("No " + Smc.namePlural + " found."); } else { throw new Exception("Could not find a device with serial number " + specifiedSerialNumber + ".\n" + "To list device, use the --list option."); } } // All the command-line arguments were good and a matching device was found, so // connect to it. Smc device = new Smc(item); // Perform all the previously computed actions on the device. foreach (ActionOnDevice action in actionsOnDevice) { action(device); } }