public override void DoWork(Serial thisSensor) { PMSensordata thisReading = new PMSensordata { Pm1_atm = (DateTime.Now - DateTime.UnixEpoch).TotalSeconds % 86400 / 86400 * 50, Pm25_atm = (DateTime.Now - DateTime.UnixEpoch).TotalSeconds % 86400 / 86400 * 100, Pm10_atm = (DateTime.Now - DateTime.UnixEpoch).TotalSeconds % 86400 / 86400 * 200 }; Sup.LogTraceInfoMessage($"Serial data read: {thisReading.Pm1_atm:F1}; {thisReading.Pm25_atm:F1}; {thisReading.Pm10_atm:F1};"); thisSensor.ObservationList.Add(thisReading); }
public override void DoWork(Serial thisSensor) { PMSensordata thisReading = new PMSensordata(); // Read the input buffer, throw away all other data in the buffer and read again // Do this as long as there is no 32 characters in the buffer (the minimum) // As it fills appr every second the 5 second read I implement should be enough try { int Count; if (thisSerial.BytesToRead > 0) { do { lock ( buffer ) { Count = thisSerial.Read(buffer, 0, 32); thisSerial.DiscardInBuffer(); } // Below is for debugging, takes performance Sup.LogTraceInfoMessage($"Trying {Count} chars: {buffer[ 0 ]:x2}/{buffer[ 1 ]:x2}"); } while (Count < 32 || (buffer[0] != 0x42 || buffer[1] != 0x4d)); // Below is for debugging, takes performance // string _hex = Sup.ByteArrayToHexString(buffer); Sup.LogTraceInfoMessage($"Nr of Bytes {Count}: {Sup.ByteArrayToHexString( buffer )}"); thisReading.Pm1_atm = buffer[10] * 255 + buffer[11]; thisReading.Pm25_atm = buffer[12] * 255 + buffer[13]; thisReading.Pm10_atm = buffer[14] * 255 + buffer[15]; Sup.LogTraceInfoMessage($"Serial data read: {thisReading.Pm1_atm:F1}; {thisReading.Pm25_atm:F1}; {thisReading.Pm10_atm:F1};"); thisSensor.ObservationList.Add(thisReading); } } catch (Exception e) when(e is ArgumentOutOfRangeException || e is ArgumentException || e is TimeoutException || e is InvalidOperationException || e is ArgumentNullException || e is IOException) { Sup.LogTraceWarningMessage($"DoPMS1003: Exception on Serial Read => {e.Message}"); // Continue reading } }