Exemple #1
0
 private async Task SendReadEvent()
 {
     if (_serialPort != null && Mode == SensorMode.Active)
     {
         ReadingComplete?.Invoke(this, new ReadingArgs(ReadingTime));
     }
 }
 public void Listen(IUniverse uni)
 {
     ProcessBeforeFrame(uni);
     uni.FrameFinished += (sender, e) =>
     {
         T res = ProcessAfterFrame(uni);
         ReadingComplete?.Invoke(this, new FrameReading <T>(res, e.FrameLength));
     };
 }
Exemple #3
0
        private async Task Listen()
        {
            byte[] ReceiveData;
            UInt32 bytesRead;

            try
            {
                if (_serialPort != null)
                {
                    while (true)
                    {
                        if (_readCancellationTokenSource.Token.IsCancellationRequested || (_serialPort == null))
                        {
                            break;
                        }

                        bytesRead = await _dataReader.LoadAsync(1).AsTask(_readCancellationTokenSource.Token);

                        if (bytesRead == 1)
                        {
                            byte StartData = _dataReader.ReadByte();

                            if (!_readCancellationTokenSource.Token.IsCancellationRequested)
                            {
                                //looking for a 0x42, 0x4D as a data start marker
                                if (StartData == 0x42)
                                {
                                    bytesRead = await _dataReader.LoadAsync(1).AsTask(_readCancellationTokenSource.Token);

                                    if (bytesRead == 1)
                                    {
                                        if (!_readCancellationTokenSource.Token.IsCancellationRequested)
                                        {
                                            byte StartData2 = _dataReader.ReadByte();

                                            if (StartData2 == 0x4D)
                                            {
                                                //next 2 bytes contain the framelength
                                                bytesRead = await _dataReader.LoadAsync(2).AsTask(_readCancellationTokenSource.Token);

                                                if (bytesRead == 2)
                                                {
                                                    if (!_readCancellationTokenSource.Token.IsCancellationRequested)
                                                    {
                                                        byte[] FrameHeader = new byte[2];
                                                        _dataReader.ReadBytes(FrameHeader);

                                                        uint framelength = PMSConvert(FrameHeader[0], FrameHeader[1]);

                                                        bytesRead = await _dataReader.LoadAsync(framelength).AsTask(_readCancellationTokenSource.Token);

                                                        if (!_readCancellationTokenSource.Token.IsCancellationRequested)
                                                        {
                                                            if (bytesRead == framelength)
                                                            {
                                                                ReceiveData = new byte[framelength];
                                                                _dataReader.ReadBytes(ReceiveData);

                                                                //do checksum test remembering to add in the previous bytes
                                                                uint checksum = (uint)(0x42 + 0x4D + FrameHeader[0] + FrameHeader[1]);

                                                                for (int i = 0; i < ReceiveData.Length - 2; i++)
                                                                {
                                                                    checksum += ReceiveData[i];
                                                                }

                                                                if (!_readCancellationTokenSource.Token.IsCancellationRequested)
                                                                {
                                                                    if (ReceiveData.Length == 28) //receiving expected data
                                                                    {
                                                                        uint checksumcheck = PMSConvert(ReceiveData[framelength - 2], ReceiveData[framelength - 1]);

                                                                        if (checksum == checksumcheck)
                                                                        {
                                                                            ReadingTime = DateTime.Now;

                                                                            PM1_0Concentration_CF1  = PMSConvert(ReceiveData[0], ReceiveData[1]);
                                                                            PM2_5Concentration_CF1  = PMSConvert(ReceiveData[2], ReceiveData[3]);
                                                                            PM10_0Concentration_CF1 = PMSConvert(ReceiveData[4], ReceiveData[5]);

                                                                            PM1_0Concentration_amb  = PMSConvert(ReceiveData[6], ReceiveData[7]);
                                                                            PM2_5Concentration_amb  = PMSConvert(ReceiveData[8], ReceiveData[9]);
                                                                            PM10_0Concentration_amb = PMSConvert(ReceiveData[10], ReceiveData[11]);

                                                                            PM0_3Count = PMSConvert(ReceiveData[12], ReceiveData[13]);
                                                                            PM0_5Count = PMSConvert(ReceiveData[14], ReceiveData[15]);
                                                                            PM1_0Count = PMSConvert(ReceiveData[16], ReceiveData[17]);

                                                                            PM2_5Count  = PMSConvert(ReceiveData[18], ReceiveData[19]);
                                                                            PM5_0Count  = PMSConvert(ReceiveData[20], ReceiveData[21]);
                                                                            PM10_0Count = PMSConvert(ReceiveData[22], ReceiveData[23]);

                                                                            ProductVersion = ReceiveData[24];
                                                                            StatusCodes    = ReceiveData[25];

                                                                            if (Mode == SensorMode.Passive || ReadingInterval == 0)
                                                                            {
                                                                                //raise the event that there is a new reading available
                                                                                ReadingComplete?.Invoke(this, new ReadingArgs(ReadingTime));
                                                                            }
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        Debug.Write("Some other kind of response: ");
                                                                        foreach (var item in ReceiveData)
                                                                        {
                                                                            Debug.WriteLine(" {0} ", item);
                                                                        }
                                                                        Debug.WriteLine("");
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                Debug.WriteLine("Alignment issue 2 read non header character {0}", StartData2);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    Debug.WriteLine("Alignment issue read non header character {0}", StartData);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (_readCancellationTokenSource != null)
                {
                    _readCancellationTokenSource.Cancel();
                }

                System.Diagnostics.Debug.WriteLine("UART ReadAsync Exception: {0}", ex.Message);
            }
        }