在Windows Phone 8.1上从Arduino蓝牙读取数据

时间:2016-05-10 11:47:55

标签: windows-phone-8 bluetooth arduino

我正在开发一个代码,允许我使用HC-05蓝牙模块在我的arduino mega和我的Windows 8.1手机之间发送少量数据。

从手机向arduino发送数据非常简单,但是我很难读取从串口发送的arduino上的数据。

出于测试目的,我的代码很简单,发送ascii charaters' a' &安培; ' B'打开LED&关闭,这可能不会更好,但我无法弄清楚如何正确读取我发送回手机的数据。

我将一个任意的ascii字符发送回手机,但我不能在我的生活中找出从我设置的蓝牙流中读取这些数据的正确方法。

我已经尝试了将近两天,但我尝试的所有内容最终都冻结了我的手机而没有例外?很多在线帖子都把我发送到诺基亚开发网站,该网站现在处于非活动状态。

我尝试过使用' datareader'和#streamreader'这样做的课程,但它总是冻结,有谁知道如何使这项工作?为什么我的流媒体播放器会一直冻结我的手机?

我试图注释我的代码appropriatley(见下文)。问题出现在“嘀嗒”中。代码底部的事件处理程序。

(仅供参考:所有功能都已添加到清单文件中,因此不应该出现问题)。

谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Bluetooth_SL.Resources;
using Windows.Networking;
using Windows.Networking.Proximity;
using Windows.Networking.Sockets;
using Windows.Devices.Bluetooth;
using System.IO;
using Microsoft.Xna.Framework;
using System.Windows.Threading;
using Windows.Storage.Streams; // <-- for the datareader class


namespace Bluetooth_SL // silverlight, does this matter?
{
    public partial class MainPage : PhoneApplicationPage
    {
        DispatcherTimer Timer = new DispatcherTimer();
        StreamSocket socket = new StreamSocket();        
        StreamWriter writer;        
        StreamReader reader;    


        public MainPage()
        {
            InitializeComponent();
            Timer.Interval = TimeSpan.FromMilliseconds(1000); // dispatcher timer used to check for incoming data from arduino
            Timer.Tick += Timer_Tick; // event handler for dispatcher timer
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e) // frees up memory
        {
            socket.Dispose();
        }

        private void Connect_But_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ConnectToBluetooth();
        }

        private async void ConnectToBluetooth() // sets up the connection // this works fine
        {

            // Configure PeerFinder to search for all paired devices.
            PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
            var pairedDevices = await PeerFinder.FindAllPeersAsync();

            if (pairedDevices.Count == 0)
            {
                Debug_Block.Text = "No paired devices were found.";
            }
            else
            {
                Debug_Block.Text = "Found";
                PeerInformation selectedDevice = pairedDevices[0]; // pick the first paired device

                try // 'try' used in the case the socket has already been connected
                {
                    await socket.ConnectAsync(selectedDevice.HostName, "1");

                    writer = new StreamWriter(socket.OutputStream.AsStreamForWrite());
                    writer.AutoFlush = true;

                    reader = new StreamReader(socket.InputStream.AsStreamForRead());
                    Debug_Block.Text = "Connected";
                }
                catch (Exception x)
                {
                    Debug_Block.Text = x.ToString();
                }
            }
        }        

        private void SendButton_Tap(object sender, System.Windows.Input.GestureEventArgs e) // this works perfectly
        {
            try { writer.WriteLine("a"); } // attempts to write the ascii 'a' to the arduino which turns on the on-board LED
            catch { Debug_Block.Text = "Failed to write"; }
        }

        private void SendButton_Off_Tap(object sender, System.Windows.Input.GestureEventArgs e) // this works perfectly
        {
            try { writer.WriteLine("b"); } // attempts to write the ascii 'b' to the arduino which turns off the on-board LED
            catch { Debug_Block.Text = "Failed to write"; }
        }

        private void ReadButtonToggle_Tap(object sender, System.Windows.Input.GestureEventArgs e) // toggles the timer on and off
        {
            if(Timer.IsEnabled == true)
            {
                Timer.Stop();
                Debug_Block.Text = "Timer Stopped";                
            }
            else
            {
                Timer.Start();
                Debug_Block.Text = "Timer Started";
            }
        }

        void Timer_Tick(object sender, EventArgs e) // THIS IS THE PROBLEM
        {
            Debug_Block.Text = "Tick";
            Debug_Block.Text = reader.ReadLine(); // <-- ALWAYS FREEZES HERE
            Timer.Stop(); // This line is temporary for debugging
        }
    }
}

0 个答案:

没有答案