2015年12月20日日曜日

プログラムの作成

 「MainPage.xaml」と「MainPage.xaml.cs」を次のように記述します。

<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Name="OnButton" Click="OnButton_Click" Grid.Row="0" IsEnabled="False" HorizontalAlignment="Stretch">On!</Button>
<Button Name="OffButton" Click="OffButton_Click" Grid.Row="1" IsEnabled="False" HorizontalAlignment="Stretch">Off!</Button>
</Grid>
</Page>
view raw MainPage.xaml hosted with ❤ by GitHub
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 Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;
// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 を参照してください
namespace App1
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
private bool useBluetooth = true;
BluetoothSerial bluetooth;
UsbSerial usb;
RemoteDevice arduino;
public MainPage()
{
this.InitializeComponent();
if (useBluetooth)
{
bluetooth = new BluetoothSerial("RNBT-E266");
arduino = new RemoteDevice(bluetooth);
bluetooth.ConnectionEstablished += OnConnectionEstablished;
bluetooth.begin(0, 0);
}
else
{
usb = new UsbSerial("VID_2341", "PID_0043");
arduino = new RemoteDevice(usb);
usb.ConnectionEstablished += OnConnectionEstablished;
usb.begin(57600, SerialConfig.SERIAL_8N1);
}
}
private void OnConnectionEstablished()
{
var action = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(() => {
OnButton.IsEnabled = true;
OffButton.IsEnabled = true;
}));
}
private void OnButton_Click(object sender, RoutedEventArgs e)
{
arduino.digitalWrite(5, PinState.HIGH);
}
private void OffButton_Click(object sender, RoutedEventArgs e)
{
arduino.digitalWrite(5, PinState.LOW);
}
}
}

 「bluetooth = new BluetoothSerial("RNBT-E266")」の「RNBT-E266」の箇所には、Bluetoothのデバイス名を入力します。
Bluetoothのデバイス名は、「Bluetoothデバイスの管理」の画面で確認できます。

 USB接続の場合は、「private bool useBluetooth = true;」を「private bool useBluetooth = false;」に変更します。






また、「usb = new UsbSerial("VID_2341", "PID_0043");」のVIDとPIDは、デバイスマネージャで確認します。


1.[スタート]ボタンを右クリックし、[デバイスマネージャー]を選択します。
2.「ポート(COMとLPT)」を展開し、Arudino デバイス(例:Arudino Uno(COM 3))を右クリックし、[プロパティ]を選択します。
3.「詳細」タブを選択し、[プロパティ]リストから、「ハードウェアID」を選択します。














 

 コードの入力が完了したら、[ローカルコンピュータ]をクリックして、アプリをビルドし、実行します。

アプリが起動し、無事接続されると、Bluetoothデバイスの緑色のLEDが点灯します。











 これで、アプリ1の[ON]ボタンを押すとLEDが点灯し、[OFF]ボタンを押すとLEDが消灯します。

0 件のコメント:

コメントを投稿