c# - 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# - .NET 自定义依赖项(无 nuget)
- video - 如何检测其他用户在 sinch 视频通话中断开连接
- flask - Flask-Babel 转换 Flask-WTF SelectField
- c++ - 如果不使用 vector::erase(),如何编写自定义 Vector 方法来删除元素?
- python - 如何处理字典理解中的异常
- reactjs - Reactjs 没有重新渲染从 WebSockets 收到的更新
- wolfram-mathematica - 如何计算 n 阶的行列式,其中 n 是带有数学或枫树或最大值的变量?
- python - 强制python使用'而不是'作为字符串
- amazon-web-services - 从 rds 迁移到 dynamoDB
- r - R Studio在添加par时不绘图