示例#1
0
        public async Task Send(byte[] buffer, CancellationToken token)
        {
            if (serialPort == null)
            {
                return;
            }
            var dataWriter = new DataWriter(serialPort.OutputStream);

            try
            {
                if (buffer.Length != 0)
                {
                    LocalLog.Instance.Info("send ->" + Common.BytesToString(buffer) + "<- end");
                    dataWriter.WriteBytes(buffer);

                    var   storeAsyncTask = dataWriter.StoreAsync().AsTask(token);
                    await storeAsyncTask;
                }
            }
            catch (Exception ex)
            {
                Status = SerialPortStatus.Initialled;
                serialPort?.Dispose();
                serialPort = null;

                throw new CustomException(ex.Message + "Send", this.GetType().FullName, ExceptionPriority.Importance);
            }
            finally
            {
                dataWriter.DetachBuffer();
                dataWriter.DetachStream();
                dataWriter.Dispose();
            }
        }
示例#2
0
 /// <summary>
 /// CloseDevice:
 /// - Disposes SerialDevice object
 /// - Clears the enumerated device Id list
 /// </summary>
 private void CloseDevice()
 {
     CancelReadTask();
     _serialPort?.Dispose();
     _serialPort = null;
     _listOfDevices.Clear();
 }
示例#3
0
        private void DeInitialiseSerialDevice()
        {
            lock (_serialDeviceLock)
            {
                if (Enabled)
                {
                    Enabled = false;

                    _readCancellationTokenSource?.Cancel();

                    _listenTask?.Wait();
                    _listenTask = null;

                    _readCancellationTokenSource?.Dispose();
                    _readCancellationTokenSource = null;

                    _dataReader?.DetachStream();
                    _dataReader?.Dispose();
                    _dataReader = null;

                    _dataWriter?.DetachStream();
                    _dataWriter?.Dispose();
                    _dataWriter = null;

                    _serialDevice?.Dispose();
                    _serialDevice = null;
                }
            }
        }
        protected override async Task <bool> OnClosingAsync()
        {
            try
            {
                _tokenSource.Cancel();

                if (_dataReaderObject != null)
                {
                    _dataReaderObject.DetachStream();
                    _dataReaderObject.Dispose();
                }

                // .NET is having an issue with Disposing Serial Device when the connection is cut (cannot even dispose streams) - deadlocks
                // but when synchronization with new ThreadPoolScheduler is requested, disposing is complete
                await Task.Run(() =>
                {
                    _device?.Dispose();
                }).TimeoutAfter(TimeSpan.FromSeconds(1), new ThreadPoolScheduler());

                return(true);
            }
            catch (Exception ex)
            {
                _loggingChannel.LogMessage($"{ex.Message}", LoggingLevel.Error);
                return(false);
            }
        }
示例#5
0
        async Task<bool> onewireReset(string deviceId)
        {
            try
            {
                if (serialPort != null)
                    serialPort.Dispose();

                serialPort = await SerialDevice.FromIdAsync(deviceId);

                // Configure serial settings
                serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
                serialPort.BaudRate = 9600;
                serialPort.Parity = SerialParity.None;
                serialPort.StopBits = SerialStopBitCount.One;
                serialPort.DataBits = 8;
                serialPort.Handshake = SerialHandshake.None;

                dataWriteObject = new DataWriter(serialPort.OutputStream);
                dataWriteObject.WriteByte(0xF0);
                await dataWriteObject.StoreAsync();

                dataReaderObject = new DataReader(serialPort.InputStream);
                await dataReaderObject.LoadAsync(1);
                byte resp = dataReaderObject.ReadByte();
                if (resp == 0xFF)
                {
                    System.Diagnostics.Debug.WriteLine("Nothing connected to UART");
                    return false;
                }
                else if (resp == 0xF0)
                {
                    System.Diagnostics.Debug.WriteLine("No 1-wire devices are present");
                    return false;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("Response: " + resp);
                    serialPort.Dispose();
                    serialPort = await SerialDevice.FromIdAsync(deviceId);

                    // Configure serial settings
                    serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                    serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
                    serialPort.BaudRate = 115200;
                    serialPort.Parity = SerialParity.None;
                    serialPort.StopBits = SerialStopBitCount.One;
                    serialPort.DataBits = 8;
                    serialPort.Handshake = SerialHandshake.None;
                    dataWriteObject = new DataWriter(serialPort.OutputStream);
                    dataReaderObject = new DataReader(serialPort.InputStream);
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception: " + ex.Message);
                return false;
            }
        }
示例#6
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         _serialPort?.Dispose();
         _serialPort = null;
     }
 }
示例#7
0
        /// <summary>
        /// CloseDevice:
        /// - Disposes SerialDevice object
        /// - Clears the enumerated device Id list
        /// </summary>
        private void CloseDevice()
        {
            if (serialPort != null)
            {
                serialPort.Dispose();
            }
            serialPort = null;

            listOfDevices.Clear();
        }
        private void CloseSerial()
        {
            if (serialPort != null)
            {
                serialPort.Dispose();
            }

            serialPort        = null;
            serialInitialized = false;
        }
 /// <summary>
 /// Closes the device.
 /// </summary>
 private void CloseDevice()
 {
     if (SelectedSerialDevice != null)
     {
         SelectedSerialDevice.Dispose();
         SelectedSerialDevice  = null;
         this.TxText.IsEnabled = false;
         this.RxText.IsEnabled = false;
     }
 }
示例#10
0
        public void Close()
        {
            CancelToken();
            if (serialPort != null)
            {
                serialPort.Dispose();
            }

            IsEnabled = false;
        }
 /// <summary>
 /// Dispose all the serial communication objects associated with this device. We may or may not try to
 /// reconnect after this.
 /// </summary>
 public virtual void DisposeSerialObjeccts()
 {
     Log($"Disposing serial I/O objects of {DeviceLabel}");
     _dataWriter?.Dispose();
     _dataWriter = null;
     _dataReader?.Dispose();
     _dataReader = null;
     _serialDevice?.Dispose();
     _serialDevice = null;
 }
示例#12
0
        /// <summary>
        /// CloseDevice:
        /// - Disposes SerialDevice object
        /// - Clears the enumerated device Id list
        /// </summary>
        public void CloseDevice()
        {
            if (serialport != null)
            {
                serialport.Dispose();
            }
            serialport = null;

            listOfDevices.Clear();
        }
示例#13
0
        /// <summary>
        /// CloseDevice:
        /// - Disposes SerialDevice object
        /// - Clears the enumerated device Id list
        /// </summary>
        private void CloseDevice()
        {
            if (serialPort != null)
            {
                serialPort.Dispose();
            }
            serialPort = null;

            _Mode = Mode.Disconnected;
        }
示例#14
0
 public void Close()
 {
     if (device != null)
     {
         device.Dispose();
         device = null;
         info   = null;
     }
     Source?.Dispose();
     Source = null;
 }
示例#15
0
 //释放资源
 private void Close()
 {
     if (null != _dataReader)
     {
         _dataReader.DetachStream();
     }
     if (null != _derialPort)
     {
         _derialPort.Dispose();
     }
 }
示例#16
0
 protected virtual void DetruirePortSerie()
 {
     lock (_portSerieLock)
     {
         if (_portSerie != null)
         {
             _portSerie.Dispose();
         }
         _portSerie = null;
     }
 }
示例#17
0
        /// <summary>
        /// Closes the connected serial device and restores the UI to the
        /// "device disconnected" state.
        /// </summary>
        private void DeviceClose()
        {
            if (serialPort != null)
            {
                serialPort.Dispose();
            }

            serialPort = null;
            serialDeviceOutputTextBox.Text = "";
            deviceList.Clear();
        }
示例#18
0
 /// <summary>
 /// CloseDevice:
 /// - Disposes SerialDevice object
 /// - Clears the enumerated device Id list
 /// </summary>
 private void CloseDevice()
 {
     if (serialPort != null)
     {
         serialPort.Dispose();
     }
     serialPort             = null;
     comPortInput.IsEnabled = true;
     rcvdText.Text          = "";
     listOfDevices.Clear();
 }
示例#19
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (serialPort != null)
         {
             serialPort.Dispose();
             serialPort = null;
         }
     }
 }
示例#20
0
        // todo: WTF!!! understand why and fix - case PartialRead exception!
        // todo: looks like _serialDevice.InputStream.ReadAsync is not cancelled by .AsTask(cancellationToken) - so following read on the same SerialDevice could cause unexpected effects
        // todo: ConfigAwait(false)?
        public async void Dispose()
        {
            await ThreadHelper.RunInNewThreadAsync(() =>
            {
                _serialDevice.Dispose();
                return(Task.CompletedTask);
            });

            // todo: do we need this?
            _serialDevice = null;
        }
示例#21
0
 /// <summary>
 /// Close the serial port device关闭串口设备
 /// </summary>
 private void CloseDevice()
 {
     if (serialPort != null)
     {
         serialPort.Dispose();
     }
     serialPort = null;
     listOfDevice.Clear();
     AllPortName.Clear();
     serialPortDeviceDic.Clear();
 }
示例#22
0
 /// <summary>
 /// 关闭串口资源
 /// </summary>
 public void Close()
 {
     try
     {
         SerialDevice.Close();
         SerialDevice.Dispose();
     }
     catch (Exception ex)
     {
         ExceptionProcess(ex);
     }
 }
示例#23
0
        public void Close()
        {
            Debug.WriteLine("Closing Lidar...");

            CancelToken();
            if (serialPort != null)
            {
                serialPort.Dispose();
            }

            IsEnabled = false;
        }
示例#24
0
 private void CloseSerialPortDevice()
 {
     try
     {
         _serialDevice?.Dispose();
         _serialDevice = null;
     }
     catch (Exception ex)
     {
         throw new Exception($"{ex.Message} -->[WinIotPakcgeManager_CloseingDevice]", ex.InnerException);
     }
 }
示例#25
0
        private void CloseDevice()
        {
            if (serialPort != null)
            {
                serialPort.Dispose();
            }
            serialPort = null;

            connectButton.IsEnabled = true;
            outputTextBox.Text      = "";
            deviceList.Clear();
        }
示例#26
0
 public void Dispose()
 {
     lock (this)
     {
         if (_serialDevice != null)
         {
             _serialDevice.Dispose();
             _serialDevice = null;
         }
     }
     throw new NotImplementedException();
 }
示例#27
0
 public void Dispose()
 {
     Cancel();
     Task.Delay(TimeSpan.FromSeconds(CancelTimeoutSeconds));
     _dataReader?.DetachBuffer();
     _dataReader?.DetachStream();
     _dataReader?.Dispose();
     _dataWriter?.DetachBuffer();
     _dataWriter?.DetachStream();
     _dataWriter?.Dispose();
     _snocModule?.Dispose();
     _readCancellationTokenSource.Dispose();
 }
示例#28
0
        private static void CloseConnection()
               
        {
            if (serialPort != null)
            {
                            {
                    serialPort.Dispose();
                           
                }
            }

            serialPort = null;
        }
示例#29
0
        /// <summary>
        /// CloseDevice:
        /// - Disposes SerialDevice object
        /// - Clears the enumerated device Id list
        /// </summary>
        private void CloseDevice()
        {
            if (serialPort != null)
            {
                serialPort.Dispose();
            }
            serialPort = null;

            comPortInput.IsEnabled   = true;
            sendTextButton.IsEnabled = false;

            listOfDevices.Clear();
        }
        /// <summary>
        /// Closes the device, stops the device watcher, stops listening for app events, and resets object state to before a device
        /// was ever connected.
        ///
        /// When the SerialDevice is closing, it will cancel all IO operations that are still pending (not complete).
        /// The close will not wait for any IO completion callbacks to be called, so the close call may complete before any of
        /// the IO completion callbacks are called.
        /// The pending IO operations will still call their respective completion callbacks with either a task
        /// canceled error or the operation completed.
        /// </summary>
        private void CloseCurrentlyConnectedDevice()
        {
            if (device != null)
            {
                // Notify callback that we're about to close the device
                deviceCloseCallback?.Invoke(this, deviceInformation);

                Debug.WriteLine($"Closing device {deviceInformation?.Id}");

                // This closes the handle to the device
                device.Dispose();
            }
        }
示例#31
0
        /// <summary>
        /// CloseDevice:
        /// - Disposes SerialDevice object
        /// - Clears the enumerated device Id list
        /// </summary>
        private void CloseDevice()
        {
            if (serialPort != null)
            {
                serialPort.Dispose();
            }
            serialPort = null;

            Bt_ConnectDevice.IsEnabled = true;
            Bt_SendText.IsEnabled      = false;
            TxBx_RxText.Text           = "";
            listOfDevices.Clear();
        }
        private async Task<bool> InitHardwarePrivate()
        {
            try
            {
                string aqs = SerialDevice.GetDeviceSelector();
                DeviceInformationCollection devices_info = await DeviceInformation.FindAllAsync(aqs);

                _serialDevice = await SerialDevice.FromIdAsync(devices_info[0].Id);
                if (_serialDevice != null)
                {

                    // Configure serial settings
                    _serialDevice.WriteTimeout = TimeSpan.FromMilliseconds(1000);
                    _serialDevice.ReadTimeout = TimeSpan.FromMilliseconds(1000);
                    _serialDevice.BaudRate = 57600;
                    _serialDevice.Parity = SerialParity.None;
                    _serialDevice.StopBits = SerialStopBitCount.One;
                    _serialDevice.DataBits = 8;

                    _dw = new DataWriter(_serialDevice.OutputStream);
                    _dr = new DataReader(_serialDevice.InputStream);
                    _dr.InputStreamOptions = InputStreamOptions.None;

                    int version = await GetBoardVersion();
                    int error;
                    await StopAll();
                    error = await GetErrorStatus();
                    if (error == 0)
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            await SetOperationMode(i);
                            if ((error = await GetErrorStatus()) != 0)
                                break;
                            await SetMotorPWM(i);
                            if ((error = await GetErrorStatus()) != 0)
                                break;
                            await SetMotorPower(i, 0);
                            if ((error = await GetErrorStatus()) != 0)
                                break;
                            await SetMotorDirection(i, 0);
                            if ((error = await GetErrorStatus()) != 0)
                                break;
                        }
                    }
                    if (error != 0)
                    {
                        _serialDevice.Dispose();
                        _serialDevice = null;
                    }
                }
                SendEvent();
                return (_serialDevice != null);
            }
            catch
            {
                _serialDevice = null;
                SendEvent();
                return false;
            }
        }