Exemple #1
0
 private void ResourcePackage_ArrivedAtDestination(ResourcePackage resourcePackage)
 {
     PackageArrived?.Invoke(this, resourcePackage);
     AllPackageList.Remove(resourcePackage);
 }
Exemple #2
0
        private void RxEnterPoint()
        {
            lock (m_LockPortRx)
            {
                while (true)
                {
                    Monitor.Wait(m_LockPortRx);
                    try
                    {
                        m_Port = new SerialPort(
                            m_TheProfile.Name,
                            m_TheProfile.BaudRate,
                            m_TheProfile.Parity,
                            m_TheProfile.DataBits,
                            m_TheProfile.StopBits);
                        m_Port.Open();
                        OpenPort?.Invoke(null);
                        lock (m_LockPortTx)
                            Monitor.PulseAll(m_LockPortTx);
                    }
                    catch (Exception e)
                    {
                        OpenPort?.Invoke(e);
                        continue;
                    }

                    var stream = m_Port.BaseStream;

                    m_Buffer.Clear();

                    var buffer = new byte[PackageLength];
                    while (true)
                    {
                        try
                        {
                            var task =
                                stream.ReadAsync(
                                    buffer,
                                    0,
                                    PackageLength,
                                    m_CancelSource.Token);
                            task.Wait(m_CancelSource.Token);
                            var lng = task.Result;

                            m_Buffer.AddRange(buffer.Take(lng));
                            while (m_Buffer.Count >= PackageLength)
                            {
                                var isValid = PackageArrived?.Invoke(m_Buffer);
                                if (!isValid.HasValue ||
                                    isValid.Value)
                                {
                                    m_Buffer.RemoveRange(0, PackageLength);
                                }
                                else
                                {
                                    m_Buffer.RemoveRange(0, m_Buffer.Count - PackageLength);
                                }
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            break;
                        }
                    }