c# - 我如何将 MQTT 从字符串解析为在 c#/unity 中浮动?
问题描述
我试图为我的统一项目开发一个 mqtt 字符串,使用 m2mqttunity 订阅和接收来自发布者的消息。我已成功订阅并收到消息和日志。问题是,我似乎无法找到将有效负载字符串消息解析为浮点数的方法,因此我可以将浮点数用作我的统一演示的温度输入。我得到的错误是:
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00057] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00013] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Int32.Parse (System.String s) [0x00007] in <437ba245d8404784b9fbab9b439ac908>:0
at mqttTest.client_MqttMsgPublishReceived (System.Object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e) [0x00021] in C:\Users\Tila\Unity3d_MQTT-master\Assets\MQTT\scripts\test\mqttTest.cs:38
at uPLibrary.Networking.M2Mqtt.MqttClient.OnMqttMsgPublishReceived (uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublish publish) [0x0000f] in C:\Users\Tila\Unity3d_MQTT-master\Assets\MQTT\scripts\MqttClient.cs:764
at uPLibrary.Networking.M2Mqtt.MqttClient.ReceiveEventThread () [0x000f8] in C:\Users\Tila\Unity3d_MQTT-master\Assets\MQTT\scripts\MqttClient.cs:1487
at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00014] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <437ba245d8404784b9fbab9b439ac908>:0
at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <437ba245d8404784b9fbab9b439ac908>:0 occurred
GIT 是
using UnityEngine;
using System.Collections;
using System.Net;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using uPLibrary.Networking.M2Mqtt.Utility;
using uPLibrary.Networking.M2Mqtt.Exceptions;
using System;
public class mqttTest : MonoBehaviour {
private MqttClient client;
public int tempDemo;
// Use this for initialization
void Start () {
// create client instance
client = new MqttClient(IPAddress.Parse("127.0.0.1"),1883, false , null );
// register to message received
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
string clientId = Guid.NewGuid().ToString();
client.Connect(clientId);
// subscribe to the topic "/home/temperature" with QoS 2
client.Subscribe(new string[] { "hello/world" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
}
void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
Debug.Log("Received: " + System.Text.Encoding.UTF8.GetString(e.Message) );
tempDemo = int.Parse(System.Text.Encoding.UTF8.GetString(e.Message));
}
void OnGUI(){
if ( GUI.Button (new Rect (20,40,80,20), "Level 1")) {
Debug.Log("sending...");
client.Publish("hello/world", System.Text.Encoding.UTF8.GetBytes("bro"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
Debug.Log("sent");
}
}
// Update is called once per frame
void Update () {
}
}
解决方案
您的 client_MqttMsgPublishReceived 方法有tempDemo = int.Parse(...)
但是您建议传入的数据是浮点数。int.Parse
不会解析浮点数和双精度数,因为您需要float.Parse或double.Parse。您始终可以将非整数数字转换(截断)为 int。
可能的修复:
tempDemo = (int)double.Parse(System.Text.Encoding.UTF8.GetString(e.Message));
推荐阅读
- java - 如何使用Java查找给定列表中具有重复项的最大数字的索引
- mysql - 加入 3 个表并使用 SUM 计算属于一个线程的论坛帖子的评分
- svg - SVG Sprite 资源加载性能
- javascript - 当数据表中的内容发生更改时,React Draggable 冻结
- python - VSCode 没有运行 PyLint(pylint 在终端中运行良好)
- flutter - Navigator.pop 带参数
- ruby-on-rails - 我看不到错误值,但看不到整个 url
- node.js - 我应该如何使用端点 API 查询 Mongo DB?
- html - 为什么我的下拉内容在 Safari 上不起作用?
- javascript - 是否可以对跨域 iframe 进行沙箱处理,然后在单击沙箱时将其删除?