Пример #1
0
        private async void port_DataReceived(object sender,
                                             SerialDataReceivedEventArgs e)
        {
            var data = new List <string>(_port.Serial.ReadLine().Split(','));

            // Show all the incoming data in the port's buffer
            data.ForEach(i => Console.Write("{0}\t", i));
            // Value1, Units1, Value2, Units2, ...
            var timeStamp = DateTime.Now;

            using (var scope = _serviceScopeFactory.CreateScope())
            {
                for (var i = 0; i < Sequence.Count; i++)
                {
                    if (data.Count < 2 * (i + 1))
                    {
                        break;                           // In case not all serial data was read
                    }
                    var canConvert = int.TryParse(data[2 * i], out var val);
                    if (!canConvert)
                    {
                        Console.WriteLine("Serial data error: value is not a number");
                        break;
                    }
                    var reading = new ReadingValue(val, data[2 * i + 1], timeStamp);
                    var repo    = scope.ServiceProvider.GetRequiredService <IReadingRepository>();
                    _ = await repo.Add(Sequence[i], reading);
                }
            }
        }
Пример #2
0
 public Reading(Guid id, ReadingValue value)
 {
     Id    = id;
     Value = value;
 }