9639|2

232

帖子

5

TA的资源

一粒金砂(高级)

楼主
 

一起玩树莓派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程序走通编译部署的流程。代码样例:
  1. https://github.com/ms-iot/samples
复制代码
打包下载(124 MB):
  1. 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”。
主程序如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.Foundation;
  7. using Windows.Foundation.Collections;
  8. using Windows.UI.Xaml;
  9. using Windows.UI.Xaml.Controls;
  10. using Windows.UI.Xaml.Controls.Primitives;
  11. using Windows.UI.Xaml.Data;
  12. using Windows.UI.Xaml.Input;
  13. using Windows.UI.Xaml.Media;
  14. using Windows.UI.Xaml.Navigation;
  15. using GY30LightSensor;

  16. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

  17. namespace HelloWorld
  18. {
  19.     /// <summary>
  20.     /// An empty page that can be used on its own or navigated to within a Frame.
  21.     /// </summary>
  22.    
  23.     public sealed partial class MainPage : Page
  24.     {
  25.         public GY30LightSensor.GY30LightSensor gy30 { get; set; }
  26.         public MainPage()
  27.         {
  28.             InitializeComponent();
  29.             gy30 = new GY30LightSensor.GY30LightSensor();
  30.             Loaded += OnLoaded;
  31.         }
  32.         private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
  33.         {
  34.             await gy30.InitLightSensorAsync();
  35.             gy30.Reading += (o, args) =>
  36.             {
  37.                 var task = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  38.                 {
  39.                     LightResult.Text = args.Lux + " lux";
  40.                 });
  41.             };
  42.         }
  43.     }
  44. }
复制代码

编译部署后,运行结果如下:

可以看到已经成功获取传感器数据。
参考
[1] https://developer.microsoft.com/ ... es/i2caccelerometer
[2] https://www.hackster.io/ediwang/ ... ndows-10-iot-148582


附件
  1. BH1750.cs
复制代码
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Windows.Devices.Enumeration;
  7. using Windows.Devices.I2c;

  8. namespace GY30LightSensor
  9. {
  10.     public class GY30LightSensorEventArgs : EventArgs
  11.     {
  12.         public int? Lux { get; set; }

  13.         public GY30LightSensorEventArgs(int? lux)
  14.         {
  15.             Lux = lux;
  16.         }
  17.     }

  18.     public class GY30LightSensor
  19.     {
  20.         public int Bh1750Address => 0x23;

  21.         public I2cDevice I2CLightSensor { get; private set; }

  22.         private Timer PeriodicTimer { get; set; }

  23.         public int TimerIntervalMs { get; set; }

  24.         public event ReadingEventHandler Reading;

  25.         public delegate void ReadingEventHandler(object sender, GY30LightSensorEventArgs e);

  26.         private void OnReading(int lux)
  27.         {
  28.             Reading?.Invoke(lux, new GY30LightSensorEventArgs(lux));
  29.         }

  30.         public GY30LightSensor(int timerIntervalMs = 500)
  31.         {
  32.             TimerIntervalMs = timerIntervalMs;
  33.         }

  34.         public async Task InitLightSensorAsync()
  35.         {
  36.             string aqs = I2cDevice.GetDeviceSelector();
  37.             /* Get a selector string that will return all I2C controllers on the system */
  38.             var dis = await DeviceInformation.FindAllAsync(aqs);
  39.             /* Find the I2C bus controller device with our selector string           */
  40.             if (dis.Count == 0)
  41.             {
  42.                 throw new FileNotFoundException("No I2C controllers were found on the system");
  43.             }

  44.             var settings = new I2cConnectionSettings(Bh1750Address)
  45.             {
  46.                 BusSpeed = I2cBusSpeed.FastMode
  47.             };

  48.             I2CLightSensor = await I2cDevice.FromIdAsync(dis[0].Id, settings);
  49.             /* Create an I2cDevice with our selected bus controller and I2C settings */
  50.             if (I2CLightSensor == null)
  51.             {
  52.                 throw new UnauthorizedAccessException(string.Format("Slave address {0} on I2C Controller {1} is currently in use by " +
  53.                                  "another application. Please ensure that no other applications are using I2C.", settings.SlaveAddress, dis[0].Id));
  54.             }

  55.             /* Write the register settings */
  56.             try
  57.             {
  58.                 I2CLightSensor.Write(new byte[] { 0x10 }); // 1 [lux] aufloesung
  59.             }
  60.             /* If the write fails display the error and stop running */
  61.             catch (Exception ex)
  62.             {
  63.                 Debug.WriteLine("Failed to communicate with device: " + ex.Message);
  64.                 throw;
  65.             }

  66.             PeriodicTimer = new Timer(this.TimerCallback, null, 0, TimerIntervalMs);
  67.         }

  68.         private void TimerCallback(object state)
  69.         {
  70.             var lux = ReadI2CLux();
  71.             OnReading(lux);
  72.         }

  73.         private int ReadI2CLux()
  74.         {
  75.             byte[] regAddrBuf = new byte[] { 0x23 };
  76.             byte[] readBuf = new byte[2];
  77.             I2CLightSensor.WriteRead(regAddrBuf, readBuf);

  78.             
  79.             var valf = ((readBuf[0] << 8) | readBuf[1]) / 1.2;

  80.             return (int)valf;
  81.         }
  82.     }
  83. }

复制代码



最新回复

干货满满  详情 回复 发表于 2018-3-25 16:52
点赞 关注(1)

回复
举报

2万

帖子

74

TA的资源

管理员

沙发
 
必须支持 :)
加EE小助手好友,
入技术交流群
EE服务号
精彩活动e手掌握
EE订阅号
热门资讯e网打尽
聚焦汽车电子软硬件开发
认真关注技术本身
 
个人签名

加油!在电子行业默默贡献自己的力量!:)

 

回复

1

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
干货满满
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表