即使在使用if语句

时间:2018-05-15 14:32:06

标签: c# string unity3d

我正在尝试将传感器数据读入Unity。为此我通过在json中发送数据在ESP32上使用TCP服务器。 我现在正在尝试将接收的数据解析为可序列化的对象。 目前我正在从服务器读取数据,直到我收到最后的“}”括号作为有效json作为起点的非常基本的检查。

现在这是我无法找到错误的地方。我在类中启动一个Thread,它在后台运行,并不断读取服务器以获取新值。 但不知何故,我无法成功连接应该检查“}”字符的字符串。

到目前为止我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net.Sockets;
using System.Threading;

[Serializable]
public class SensorData
{
    public int capacity;
}

public class MugSensorRead : MonoBehaviour 
{
    TcpClient client = new TcpClient();
    const string IP = "192.168.137.50";
    const int PORT = 5000;

    public SensorData sensorData;
    public int capacity;

    bool _threadRunning;
    Thread _thread;

    // Use this for initialization
    void Start () 
    {
        client.Connect (IP, PORT);  
        _thread = new Thread (updateSensorData);
        _thread.Start ();
    }

    // Update is called once per frame
    void Update () 
    {
        capacity = sensorData.capacity;
    }

    void updateSensorData()
    {
        _threadRunning = true;

        while (_threadRunning) 
        {
            NetworkStream stream = client.GetStream ();
            string jsonMsg = "";
            bool validJson = false;

            while (!validJson) 
            {
                byte[] inStream = new byte[1024];
                stream.Read (inStream, 0, 1024);
                string jsonData = System.Text.Encoding.ASCII.GetString (inStream);
                jsonMsg = string.Concat (jsonMsg, jsonData);
                if (jsonMsg.Contains("}"))
                {
                    validJson = true;
                    //This part here is executed, but when I print(jsonMsg), it just prints the character "{" which gets transmitted in the first segment
                }
            }
            sensorData = JsonUtility.FromJson<SensorData> (jsonMsg);
        }
        _threadRunning = false;
    }

    void OnDisable()
    {
        if (_threadRunning) 
        {
            _threadRunning = false;
            _thread.Join ();
        }
    }
}

你能发现我的错误吗?我只是无法看到我的代码失败的地方。

1 个答案:

答案 0 :(得分:3)

您的错误是您没有检查读取返回的字节数,并且您将0添加为字符串内容的一部分。

这应该被替换:

    while (!validJson) 
    {
        byte[] inStream = new byte[1024];
        stream.Read (inStream, 0, 1024);
        string jsonData = System.Text.Encoding.ASCII.GetString (inStream);
        jsonMsg = string.Concat (jsonMsg, jsonData);
        if (jsonMsg.Contains("}"))
        {
            validJson = true;
            //This part here is executed, but when I print(jsonMsg), it just prints the character "{" which gets transmitted in the first segment
        }
    }

这将是正确的代码:

    while (!validJson) 
    {
        byte[] inStream = new byte[1024];
        int bytesRead = stream.Read (inStream, 0, 1024);
        string jsonData = System.Text.Encoding.ASCII.GetString (inStream, 0, bytesRead);
        jsonMsg = string.Concat (jsonMsg, jsonData);
        if (jsonMsg.Contains("}"))
        {
            validJson = true;
            //This part here is executed, but when I print(jsonMsg), it just prints the character "{" which gets transmitted in the first segment
        }
    }
数组中的

0转换为字符串结束字节,这就是为什么你没有看到'}'字符,因为之前有字符串结束字符。