/// <summary> /// Given a deviceID and client port definition, the host is connected to a particular serial device display. /// /// PRECONDITION: DiscoverDeviceIds() was successful /// /// </summary> /// <param name="deviceId"> /// A unique key that represents a physically connected serial device display. /// </param> /// <param name="portDef"> /// A <see cref="PortDef"/> baud rate that matches the related Workshop 4 project's baud rate. /// A mismatched baud rate will result in failed communication between the host and the display. /// </param> /// <returns>Returns a <see cref="Task"/></returns> public async Task Connect(string deviceId, PortDef portDef) { var serialDeviceDisplay = this.LookupDevice(deviceId); if (serialDeviceDisplay == null) { Debug.WriteLine($"Host.Instance.Connect was unable to connect to display using deviceId {deviceId}"); throw new NullReferenceException(); } await serialDeviceDisplay.Connect(deviceId, portDef).ConfigureAwait(false); }
/// <summary> /// Connects the serial device to the 4D Systems display per client specified /// DeviceInformation unique Id and the 4D Display's project PortDef, which specifies Baud rate. /// </summary> /// <param name="deviceInformationId"></param> /// <param name="portDef"></param> /// <returns></returns> public async Task Connect(string deviceInformationId, PortDef portDef) { try { //IMPORTANT: For accessing the serial port, you must add the DeviceCapability to the Package.appxmanifest file in your project. //This applies to Headless and Headed Apps contained in this project solution set. //https://ms-iot.github.io/content/en-US/win10/samples/SerialSample.htm //https://social.msdn.microsoft.com/Forums/en-US/b9633593-377e-4d6f-b3a9-838de0555371/serialdevicefromidasync-always-returns-null-unless-the-serial-adapter-is-plugged-in-after-boot?forum=WindowsIoT //If SerialDevice is null then follow steps below to resolve: //Connect to the Windows 10 IoT Core device through PowerShell //Run the command iotstartup remove headless ZWave //Reboot the device shutdown /r /t 0 this.SerialDevice = await SerialDevice.FromIdAsync(deviceInformationId); if (this.SerialDevice == null) { Debug.WriteLine("SerialDevice is null. Check DeviceCapability in app.manifest."); //will throw! } // Configure serial settings this.SerialDevice.WriteTimeout = TimeSpan.FromMilliseconds(1000); this.SerialDevice.ReadTimeout = TimeSpan.FromMilliseconds(1000); this.SerialDevice.BaudRate = (uint)portDef.BaudRate; this.SerialDevice.Parity = portDef.SerialParity; this.SerialDevice.StopBits = portDef.SerialStopBitCount; this.SerialDevice.DataBits = portDef.DataBits; } catch (Exception ex) { Debug.WriteLine("Connect threw: "); Debug.WriteLine(ex.StackTrace); throw; } }