Esempio n. 1
0
        /// <summary>
        /// Initialize a new SerialPort object with a specific baud value
        /// </summary>
        /// <param name="baud">Baud value</param>
        public async void InitSerialPort(int baud)
        {
            _SerialPortReady = false;
            string aqs = SerialDevice.GetDeviceSelector();
            DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs);

            if (dis.Count == 0)
            {
                return;
            }

            try
            {
                _SerialPort = await SerialDevice.FromIdAsync(GuessDeviceId(dis));

                if (_SerialPort == null)
                {
                    return;
                }

                _SerialPort.WriteTimeout = TimeSpan.FromMilliseconds(100);
                _SerialPort.ReadTimeout  = TimeSpan.FromMilliseconds(100);
                _SerialPort.BaudRate     = (uint)baud;
                _SerialPort.Parity       = SerialParity.None;
                _SerialPort.StopBits     = SerialStopBitCount.One;
                _SerialPort.DataBits     = 8;
                _SerialPort.Handshake    = SerialHandshake.None;

                // Create cancellation token object to close I/O operations when closing the device
                _ReadCancellationTokenSource = new CancellationTokenSource();

                _SerialPortReady = true;
                SerialDeviceReady?.Invoke(this, new EventArgs());
                Listen();
            }
            catch (Exception e)
            {
                LogHelper.LogError(e.Message);
            }
        }
 private void Event_SerialDeviceReady(object sender, EventArgs e)
 {
     SerialDeviceReady?.Invoke(this, new EventArgs());
 }