|
一起玩树莓派3+开发Windows 10 IoT Core应用程序
[复制链接]
本帖最后由 x1816 于 2016-11-18 09:06 编辑
上一篇分享介绍了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
- {
- /// <summary>
- /// An empty page that can be used on its own or navigated to within a Frame.
- /// </summary>
-
- 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
附件
|
|