首页 > 解决方案 > Windows 远程 Arduino StringMessageReceived 事件

问题描述

好吧,我厌倦了谷歌搜索。所以我正在开发一个使用Windows Remote Arduino的项目。我才刚刚开始,所以我处于“一起破解阶段”。arduino 正在运行标准firmata,我正在调用一个 sysex 函数,该函数从某些传感器返回一些数据。

我订阅了 StringMessageReceived 事件,但我发现该事件并不总是触发。每次发送 sysex 命令时,我都“应该”接收数据。我确实在 git hub 站点上发现了一个用于远程接线的已关闭问题,该问题说要在 Firmata 对象上调用 flush(),我正在这样做,但我没有得到 1 对 1 的响应。

这是我得到的示例输出。

触发设备就绪事件

计时器滴答!

计时器滴答!

{\"temp1\":21.50,\"pH\":0.000}

计时器滴答!

{\"temp1\":21.50,\"pH\":0.000}

计时器滴答!

计时器滴答!

计时器滴答!

{\"temp1\":21.50,\"pH\":0.000}

计时器滴答!

计时器滴答!

计时器滴答!

计时器滴答!

{\"temp1\":21.50,\"pH\":0.000}

计时器滴答!

计时器滴答!

计时器滴答!

计时器滴答!

{\"temp1\":21.50,\"pH\":0.000}

{\"temp1\":21.50,\"pH\":0.000}

计时器滴答!

计时器滴答!

计时器滴答!

{\"temp1\":21.50,\"pH\":0.000}

计时器滴答!

{\"temp1\":21.50,\"pH\":0.000}

计时器滴答!

计时器滴答!

计时器滴答!

{\"temp1\":21.50,\"pH\":0.000}"

我知道 arduino 正在获取我的命令,因为我可以看到我的一个传感器上的 LED 正在处理命令。

这是我的 UWP 代码。

public sealed partial class MainPage : Page
{
    IStream connection;
    RemoteDevice arduino;
    UwpFirmata firmata;
    DispatcherTimer timer;

    public MainPage()
    {
        this.InitializeComponent();
    }

    private void btnConnect_Click(object sender, RoutedEventArgs e)
    {
        connection = new UsbSerial("VID_0403", "PID_6001");
        firmata = new UwpFirmata();
        arduino = new RemoteDevice(firmata);
        timer = new DispatcherTimer();
        firmata.begin(connection);

        connection.begin(57600, SerialConfig.SERIAL_8N1);
        arduino.DeviceReady += Setup;
        arduino.DeviceConnectionFailed += ConnectFailed;
        arduino.StringMessageReceived += StringRecieved;

        timer.Interval = new TimeSpan(0, 0, 5);
        timer.Tick += timerTick;
        timer.Start();
    }

    private void timerTick(object sender, object e)
    {
        Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, ()
        =>
        {
            lblPhData.Text += "Timer Tick!\n";
        }
        );
        byte PH_QUERY = 0x44;
        firmata.sendSysex(PH_QUERY, new byte[] { }.AsBuffer());
        firmata.flush();
    }

    private void StringRecieved(string message)
    {
        Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, ()
        =>
        {
            lblPhData.Text += message + "\n";
        }
        );
    }

    public void ConnectFailed(String message)
    {
        Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, ()
             =>
         {
             lblPhData.Text += message;
         }
        );
    }
    public void Setup()
    {
        Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, ()
             =>
         {
             lblPhData.Text += "Device Ready Event Fired\n";
         }
        );            
    }

    private void btnGetData_Click(object sender, RoutedEventArgs e)
    {
        byte PH_QUERY = 0x44;
        firmata.sendSysex(PH_QUERY, new byte[] { }.AsBuffer());
        firmata.flush();
    }
}

我必须错过一些非常简单的东西。

标签: c#uwparduinowindows-10-iot-corefirmata

解决方案


推荐阅读