예제 #1
0
        public async void Connect()
        {
            try
            {
                string availableSerialDevices = SerialDevice.GetDeviceSelector();
                var    asyncDevices           = await DeviceInformation.FindAllAsync(availableSerialDevices);

                DeviceInformation entry = asyncDevices[0];

                uart = await SerialDevice.FromIdAsync(entry.Id);

                uart.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                uart.ReadTimeout  = TimeSpan.FromMilliseconds(1000);
                uart.BaudRate     = 57600; // Need to parametise
                uart.Parity       = SerialParity.None;
                uart.StopBits     = SerialStopBitCount.One;
                uart.DataBits     = 8;
                uart.Handshake    = SerialHandshake.None;
                portOpen          = true;
                portStatus        = UARTstatus.Open;
            }
            catch (Exception ex)
            {
                portMessage = ex.Message;
            }
        }
예제 #2
0
 private void CloseDevice()
 {
     if (uart != null)
     {
         uart.Dispose();
     }
     uart       = null;
     portStatus = UARTstatus.Closed;
 }
예제 #3
0
        private async Task ReadAsync(CancellationToken token)
        {
            Task <UInt32> loadAsyncTask;

            uint ReadBufferLength = 1024;

            // If task cancellation was requested, comply
            token.ThrowIfCancellationRequested();

            // Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
            dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;

            // Create a task object to wait for data on the serialPort.InputStream
            loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(token);

            // Launch the task and wait
            UInt32 bytesRead = await loadAsyncTask;

            if (bytesRead > 0)
            {
                message    = dataReaderObject.ReadString(bytesRead);
                portStatus = UARTstatus.Read;
            }
        }