コード例 #1
0
        private void Receive()
        {
            try
            {
                while (_isWorking)
                {
                    if (_serialPort.IsOpen)
                    {
                        var data = _serialPort.ReadLine();

                        if (OnDataReceivedEvent != null)
                        {
                            OnDataReceivedEvent.Invoke(data);
                        }
                    }
                    else
                    {
                        _isWorking = false;
                    }
                }

                _serialPort.Close();
            }
            catch (Exception exception)
            {
                if (OnPortErrorEvent != null)
                {
                    OnPortErrorEvent.Invoke(exception);
                }
            }
            finally
            {
                _serialPort.Close();
            }
        }
コード例 #2
0
        public void Start()
        {
            try
            {
                _serialPort.Open();
            }
            catch (Exception exception)
            {
                if (OnPortErrorEvent != null)
                {
                    OnPortErrorEvent.Invoke(exception);
                }
            }

            if (OnPortOpenEvent != null)
            {
                OnPortOpenEvent.Invoke();
            }

            _serialPortReaderThread.Start();
        }