Porrey.Uwp.IoT.Devices.Ds1307.GetAsync C# (CSharp) Method

GetAsync() public method

Get the current Date and Time from the DS1307.
public GetAsync ( ) : Task
return Task
		public Task<DateTimeOffset> GetAsync()
		{
			DateTimeOffset returnValue = DateTime.MinValue;

			byte[] readBuffer = new byte[7];
			byte[] writeBuffer = new byte[] { RTC_ADDRESS };

			this.WriteReadAsync(writeBuffer, readBuffer);

			returnValue = new DateTime
			(
				Bcd.ToInt(readBuffer[6]) + 2000,            // Year
				Bcd.ToInt(readBuffer[5]),                   // Month
				Bcd.ToInt(readBuffer[4]),                   // Day
				Bcd.ToInt((byte)(readBuffer[2] & 0x3f)),    // Hours over 24 hours (bit 6 is 24/12 hour format; 1 = 12, 0 = 24)
				Bcd.ToInt(readBuffer[1]),                   // Minutes
				Bcd.ToInt((byte)(readBuffer[0] & 0x7f))     // Seconds (bit 7 is the clock halt bit; 0 = enabled, 1 = halted)
			);

			return Task<DateTimeOffset>.FromResult(returnValue);
		}

Usage Example

Beispiel #1
0
		private async Task TestClock()
		{
			// ***
			// *** Clock
			// ***
			Ds1307 dtc = new Ds1307();
			await dtc.InitializeAsync();

			// ***
			// *** Get the date and time from the clock
			// ***
			DateTimeOffset dt = await dtc.GetAsync();

			// ***
			// *** Create an NTP client and get the date and time
			// ***
			NtpClient ntp = new NtpClient();
			DateTimeOffset? ndt = await ntp.GetAsync("0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org");

			// ***
			// *** Update the clock if we have a result from the servers
			// ***
			if (ndt.HasValue)
			{
				await dtc.SetAsync(ndt.Value);
			}
		}
All Usage Examples Of Porrey.Uwp.IoT.Devices.Ds1307::GetAsync