树莓派3+开发Windows 10 IoT Core应用程序
上一篇分享介绍了Windows 10 物联网版(IoT Core)开发环境的搭建,这篇分享在此基础上探索这个平台上应用程序的开发。
Windows 10 IoT Core支持C#和C++等语言开发,这里以C#为例。 微软对于这个系统提供了大量的代码示例,接下来先测试一个简单的Hello World程序,后续再测试连接感测器。
Hello World !
首先通过HelloWorld程序走通编译部署的流程。代码样例:
https://github.com/ms-iot/samples
复制代码
打包下载(124 MB):
https://codeload.github.com/ms-iot/samples/zip/develop
复制代码
用Visual Studio 2015打开HelloWorld工程目录中的HelloWorld.sln,整个工程会在VS中打开。
稍微改下代码,以便和官方程序区分。
接下来开始编译部署工作。
如图,平台选择ARM,位置是远程计算机。
远程计算机已经自动检测到,当然也可以输入IP来连接。
远程连接的对话框在第一次设定后,以后可能不会自动弹出,如果要修改,可以通过项目属性中的调试选项打开。
按下工具栏上的开始调试按钮(就是有绿色箭头的"远程计算机"按钮),程序会自动完成编译生成,部署等工作。
如果之前安装过这个程序,会遇到这个弹窗:
选择"是"继续部署。
程序运行成功:
连接光强度感测器
使用的是BH1750数字环境光强度感测器。模块通过3.3V供电,使用I2C总线通信,连接到Raspberry Pi上的供电口和I2C界面,具体在物联网相关的帖子里已经提到(一起玩树莓派3+体验物联网云服务)。
断电连接好感测器,然后启动Raspberry Pi 3。
复制一份HelloWorld并修改为BH1750。
修改界面布局:
放置2个TextBox用于显示提示文字和感测器结果。
感测器界面代码来自参考资料[2],
注意使用了Windows.Devices.I2c,要在项目引用中添加"Windows IoT Extensions for the UWP"。
主程序如下:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using GY30LightSensor;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace HelloWorld
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class MainPage : Page
{
public GY30LightSensor.GY30LightSensor gy30 { get; set; }
public MainPage()
{
InitializeComponent();
gy30 = new GY30LightSensor.GY30LightSensor();
Loaded += OnLoaded;
}
private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
await gy30.InitLightSensorAsync();
gy30.Reading += (o, args) =>
{
var task = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
LightResult.Text = args.Lux + " lux";
});
};
}
}
}
复制代码
编译部署后,运行结果如下:
可以看到已经成功获取感测器数据。
参考
[1] https://developer.microsoft.com/ ... es/i2caccelerometer
[2] https://www.hackster.io/ediwang/ ... ndows-10-iot-148582
附件
BH1750.cs
复制代码
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;
namespace GY30LightSensor
{
public class GY30LightSensorEventArgs : EventArgs
{
public int? Lux { get; set; }
public GY30LightSensorEventArgs(int? lux)
{
Lux = lux;
}
}
public class GY30LightSensor
{
public int Bh1750Address => 0x23;
public I2cDevice I2CLightSensor { get; private set; }
private Timer PeriodicTimer { get; set; }
public int TimerIntervalMs { get; set; }
public event ReadingEventHandler Reading;
public delegate void ReadingEventHandler(object sender, GY30LightSensorEventArgs e);
private void OnReading(int lux)
{
Reading?.Invoke(lux, new GY30LightSensorEventArgs(lux));
}
public GY30LightSensor(int timerIntervalMs = 500)
{
TimerIntervalMs = timerIntervalMs;
}
public async Task InitLightSensorAsync()
{
string aqs = I2cDevice.GetDeviceSelector();
var dis = await DeviceInformation.FindAllAsync(aqs);
if (dis.Count == 0)
{
throw new FileNotFoundException("No I2C controllers were found on the system");
}
var settings = new I2cConnectionSettings(Bh1750Address)
{
BusSpeed = I2cBusSpeed.FastMode
};
I2CLightSensor = await I2cDevice.FromIdAsync(dis[0].Id, settings);
if (I2CLightSensor == null)
{
throw new UnauthorizedAccessException(string.Format("Slave address {0} on I2C Controller {1} is currently in use by " +
"another application. Please ensure that no other applications are using I2C.", settings.SlaveAddress, dis[0].Id));
}
try
{
I2CLightSensor.Write(new byte[] { 0x10 }); // 1 [lux] aufloesung
}
catch (Exception ex)
{
Debug.WriteLine("Failed to communicate with device: " + ex.Message);
throw;
}
PeriodicTimer = new Timer(this.TimerCallback, null, 0, TimerIntervalMs);
}
private void TimerCallback(object state)
{
var lux = ReadI2CLux();
OnReading(lux);
}
private int ReadI2CLux()
{
byte[] regAddrBuf = new byte[] { 0x23 };
byte[] readBuf = new byte[2];
I2CLightSensor.WriteRead(regAddrBuf, readBuf);
var valf = ((readBuf[0] << 8) | readBuf[1]) / 1.2;
return (int)valf;
}
}
}
复制代码
以上图文内容均是EEWORLD论坛网友:x1816 原创,在此感谢。
欢迎微博@EEWORLD
如果你也写过此类原创干货请关注微信公众号:EEWORLD(电子工程世界)回复"投稿",也可将你的原创发至:[email protected],一经入选,我们将帮你登上头条!
与更多行业内网友进行交流请登陆EEWORLD论坛。
相关文章
- Google Play 商店应用也不安全,许多 Android 应用程序还在用过时版本核心库
- 免费的 Auto Office L1 系统单机版,提高公司运作效率以达到降低成本
- 如何查看过往 Facebook 按赞互动、留言、被标注的动态活动记录?快速找出你要的内容
- 免费素材资源与线上工具整理,2020年12月号
- Claunch 快速启动免费小工具,可自订 Windows 常用的应用程序、文件、资料夹、网站等等
- 免费可商业使用的 Paper-co 高品质纸张素材库(400多款)
- Vector Club 免费向量图档,可商用并支援 AI 及 EPS 档
- SDelete-Gui 可完全删除重要档案,谁都无法恢复的免费工具
- 适合长辈观看的 Youtube 频道列表,手刀帮爸妈订阅起来
- Xbox 推出家长管理 app !! 控制细路打机时间