public static void Main() { _appVersion = typeof(Program).Assembly.GetName().Version; DisplayTitlePage(); ClearStatus(); BrainPad.Display.DrawFillRect(65, 100, 10, 10, BrainPad.Color.Palette.Black); ICommunicationChannel channel = null; _firmata = new FirmataService("BrainPad", "b335f01176044984941833c9ce00d3ae", _appVersion.Major, _appVersion.Minor); _board = new BrainPadBoard(_firmata); try { var vsp = new Cdc(); Controller.ActiveDevice = vsp; channel = new UsbCommunicationChannel(vsp); DisplayStatus("Using USB. Reset to deploy"); } catch { var port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One); channel = new SerialCommunicationChannel(port); DisplayStatus("Using secondary serial port"); } _firmata.Open(_board, channel); while (true) { _board.Process(); } }
private void ReadThread() { byte[] oneByte = new byte[1]; while (true) { try { int count = _stream.Read(oneByte); if (count == 1) { this.DataReceived(this, oneByte); } else if (count == -1) { throw new Exception("need to restart the port"); } } catch { _port.Deactivate(); Thread.Sleep(300); _port = new Cdc(); Controller.ActiveDevice = _port; _stream = _port.Stream; } } }
static void Main() { // visit link for demo: https://ghi-electronics.github.io/TinyCLR-WebUSB/serialwebusb gpio = GpioController.GetDefault(); var usbclientController = UsbClientController.GetDefault(); var usbClientSetting = new UsbClientSetting { ProductName = "TinyCLR Serial WebUsb", InterfaceName = "TinyCLR Serial WebUsb", MaxPower = RawDevice.MAX_POWER, SerialNumber = "12344321", Mode = UsbClientMode.Cdc, }; webUsb = new Cdc(usbclientController, usbClientSetting); webUsb.DeviceStateChanged += (a, b) => Debug.WriteLine("Connection changed."); webUsb.DataReceived += (a, count) => Debug.WriteLine("Data received:" + count); webUsb.Enable(); while (webUsb.DeviceState != DeviceState.Configured) { ; } Debug.WriteLine("UsbClient Connected"); while (true) { try { var command = ReadString(); HandleCommand(command); SendString("Success"); } catch (Exception ex) { SendString(ex.Message); } } }
public static bool Init() { Cdc vsp = new Cdc(); //activedevice is null Controller.ActiveDevice = vsp; // Send "Hello world!" to PC every second. (Append a new line too) //byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Hello world!\r\n"); while (true) { // Check if connected to PC if (Controller.State != UsbController.PortState.Running) { Debug.Print("Waiting to connect to PC..."); } else break; //else //{ // vsp.Stream.Write(bytes, 0, bytes.Length); //} Thread.Sleep(500); } // See if the hardware supports USB UsbController[] controllers = UsbController.GetControllers(); // Bail out if USB is not supported on this hardware! if (0 == controllers.Length) { Debug.Print("USB is not supported on this hardware!"); return false; } foreach (UsbController controller in controllers) { if (controller.Status != UsbController.PortState.Stopped) { controller.Stop(); Thread.Sleep(1000); } } // Find a free USB controller UsbController usbController = null; foreach (UsbController controller in controllers) { if (controller.Status == UsbController.PortState.Stopped) { usbController = controller; break; } } // If no free USB controller if (null == usbController) { Debug.Print("All available USB controllers already in use. Set the device to use Ethernet or Serial debugging."); return false; } try { // Configure the USB controller if (ConfigureUSBController(usbController)) { usbStream = usbController.CreateUsbStream(WRITE_EP, READ_EP); usbStream.ReadTimeout = 60000; usbStream.WriteTimeout = 60000; } else { throw new Exception(); } } catch (Exception) { Debug.Print("USB stream could not be created, error " + usbController.ConfigurationError.ToString()); return false; } return true; }
private void ReadThread() { byte[] oneByte = new byte[1]; while (true) { try { int count = _stream.Read(oneByte); if (count == 1) this.DataReceived(this, oneByte); else if (count == -1) throw new Exception("need to restart the port"); } catch { _port.Deactivate(); Thread.Sleep(300); _port = new Cdc(); Controller.ActiveDevice = _port; _stream = _port.Stream; } } }
public UsbCommunicationChannel(Cdc port) { _port = port; _stream = _port.Stream; new Thread(ReadThread).Start(); }
public static void Main() { _appVersion = typeof(Program).Assembly.GetName().Version; DisplayTitlePage(); ClearStatus(); BrainPad.Display.DrawFillRect(65, 100, 10, 10, BrainPad.Color.Palette.Black); ICommunicationChannel channel = null; // 437 : _firmata = new FirmataService("BrainPad", "b335f01176044984941833c9ce00d3ae", _appVersion.Major, _appVersion.Minor); // 438 : _firmata = new FirmataService("BrainPad", "07e62d24272240d2b4876b199524079c", _appVersion.Major, _appVersion.Minor); _board = new BrainPadBoard(_firmata); if (GHI.Processor.DebugInterface.Type == DebugInterface.InterfaceType.Usb) { GHI.Processor.DebugInterface.Type = DebugInterface.InterfaceType.Serial; GHI.Processor.DebugInterface.ComPort = "COM1"; GHI.Processor.DebugInterface.UseInTinyBooter = true; GHI.Processor.DebugInterface.Save(); PowerState.RebootDevice(false); } try { var vsp = new Cdc(); Controller.ActiveDevice = vsp; channel = new UsbCommunicationChannel(vsp); DisplayStatus("Using USB. Reset to deploy"); } catch { var port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One); channel = new SerialCommunicationChannel(port); DisplayStatus("Using secondary serial port"); } _firmata.Open(_board, channel); while (true) { try { _board.Process(); } catch (GHI.Usb.OperationTimedOutException) { } catch (Exception ex) { BrainPad.Display.Clear(); BrainPad.Display.DrawString(0, 0, "Exception", BrainPad.Color.Palette.Red); var msg = ex.Message; int iy = 10; while (msg.Length > 0) { var line = msg; if (line.Length > 27) { line = msg.Substring(0, 27); } BrainPad.Display.DrawString(0, iy, line, BrainPad.Color.Palette.Red); iy += 10; if (msg.Length > 27) { msg = msg.Substring(27); } else { msg = ""; } } } } }
private void FillCdcs(ref IList<Cdc> contents, System.IO.DirectoryInfo directoryInfo) { var dirs = directoryInfo.GetDirectories(); foreach (var dir in dirs) { var cdc = new Cdc(dir); contents.Add(cdc); } var files = directoryInfo.GetFiles(); foreach (var file in files) { var fileCdc = new Cdc(file); contents.Add(fileCdc); } }
public static bool Init() { Cdc vsp = new Cdc(); //activedevice is null Controller.ActiveDevice = vsp; // Send "Hello world!" to PC every second. (Append a new line too) //byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Hello world!\r\n"); while (true) { // Check if connected to PC if (Controller.State != UsbController.PortState.Running) { Debug.Print("Waiting to connect to PC..."); } else { break; } //else //{ // vsp.Stream.Write(bytes, 0, bytes.Length); //} Thread.Sleep(500); } // See if the hardware supports USB UsbController[] controllers = UsbController.GetControllers(); // Bail out if USB is not supported on this hardware! if (0 == controllers.Length) { Debug.Print("USB is not supported on this hardware!"); return(false); } foreach (UsbController controller in controllers) { if (controller.Status != UsbController.PortState.Stopped) { controller.Stop(); Thread.Sleep(1000); } } // Find a free USB controller UsbController usbController = null; foreach (UsbController controller in controllers) { if (controller.Status == UsbController.PortState.Stopped) { usbController = controller; break; } } // If no free USB controller if (null == usbController) { Debug.Print("All available USB controllers already in use. Set the device to use Ethernet or Serial debugging."); return(false); } try { // Configure the USB controller if (ConfigureUSBController(usbController)) { usbStream = usbController.CreateUsbStream(WRITE_EP, READ_EP); usbStream.ReadTimeout = 60000; usbStream.WriteTimeout = 60000; } else { throw new Exception(); } } catch (Exception) { Debug.Print("USB stream could not be created, error " + usbController.ConfigurationError.ToString()); return(false); } return(true); }