Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用运行在Windows 10 IoT core上的raspberrypi从RTC模块获取日期和时间,并在Windows 10 IoT仪表板上显示日期和时间_C#_Raspberry Pi3_Windows 10 Iot Core_Real Time Clock - Fatal编程技术网

C# 使用运行在Windows 10 IoT core上的raspberrypi从RTC模块获取日期和时间,并在Windows 10 IoT仪表板上显示日期和时间

C# 使用运行在Windows 10 IoT core上的raspberrypi从RTC模块获取日期和时间,并在Windows 10 IoT仪表板上显示日期和时间,c#,raspberry-pi3,windows-10-iot-core,real-time-clock,C#,Raspberry Pi3,Windows 10 Iot Core,Real Time Clock,谁能告诉我如何使用运行在windows 10 IoT core上的Raspberrypi从RTC模块DS3231读取日期和时间,并在IoT仪表板中显示日期和时间。 提前感谢。我们可以通过端口将DS3231模块连接到Raspberry Pi,以便在Windows IoT Core上与您的应用程序进行通信。然后请参考以下代码读取日期和时间 public async Task<DateTime> ReadDateTime() { I2cController i2c = awa

谁能告诉我如何使用运行在windows 10 IoT core上的Raspberrypi从RTC模块DS3231读取日期和时间,并在IoT仪表板中显示日期和时间。
提前感谢。

我们可以通过端口将DS3231模块连接到Raspberry Pi,以便在Windows IoT Core上与您的应用程序进行通信。然后请参考以下代码读取日期和时间

public async Task<DateTime> ReadDateTime()
{
       I2cController i2c = await I2cController.GetDefaultAsync();
       I2cConnectionSettings setting = new I2cConnectionSettings(0x68);
       setting.BusSpeed = I2cBusSpeed.StandardMode;
       var device = i2c.GetDevice(setting);
       byte[] writeBuf = { 0x00 };
       device.Write(writeBuf);
       byte[] buffer = new byte[7];
       device.Read(buffer);
       byte second = BcdToDec((byte)(buffer[0] & 0x7f));
       byte minute = BcdToDec(buffer[1]);
       byte hour = BcdToDec((byte)(buffer[2] & 0x3f));
       byte dayOfWeek = BcdToDec(buffer[3]);
       byte dayOfMonth = BcdToDec(buffer[4]);
       byte month = BcdToDec(buffer[5]);
       int year = 2000 + BcdToDec(buffer[6]);
       DateTime dt = new DateTime(year, month, dayOfMonth, hour, minute, second);
       device.Dispose();
       return dt;
}

 private byte DecToBcd(byte val)
 {
      return Convert.ToByte((val / 10 * 16) + (val % 10));
 }

 private byte BcdToDec(byte val)
 {
      return Convert.ToByte((val / 16 * 10) + (val % 16));
 }
public异步任务ReadDateTime()
{
I2cController i2c=等待I2cController.GetDefaultAsync();
I2cConnectionSettings设置=新的I2cConnectionSettings设置(0x68);
setting.BusSpeed=I2cBusSpeed.StandardMode;
var设备=i2c.GetDevice(设置);
字节[]writeBuf={0x00};
设备写入(writeBuf);
字节[]缓冲区=新字节[7];
设备读取(缓冲区);
字节秒=BcdToDec((字节)(缓冲区[0]&0x7f));
字节分钟=BcdToDec(缓冲区[1]);
字节小时=BcdToDec((字节)(缓冲区[2]&0x3f));
字节dayOfWeek=BcdToDec(缓冲区[3]);
字节dayOfMonth=BcdToDec(缓冲区[4]);
字节月=BcdToDec(缓冲区[5]);
int year=2000+BcdToDec(缓冲区[6]);
DateTime dt=新的日期时间(年、月、月、时、分、秒);
device.Dispose();
返回dt;
}
专用字节DecToBcd(字节val)
{
返回Convert.ToByte((val/10*16)+(val%10));
}
专用字节BcdToDec(字节val)
{
返回Convert.ToByte((val/16*10)+(val%16));
}

你能建议一下你试过什么吗?所以有人可以帮你。