簡體   English   中英

即使使用if語句檢查后,字符串也不包含字符

[英]String doesn't contain character even after checking with if-statement

我正在嘗試將傳感器數據讀取到Unity中。 為此,我通過在json中發送數據在ESP32上使用TCP Server。 我現在試圖將接收到的數據解析為可序列化的對象。 目前,我正在從服務器讀取數據,直到收到最后的“}”括號為止,這是對作為有效起始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 ();
        }
    }
}

你能發現我的錯誤嗎? 我只是看不到代碼失敗的地方。

您的錯誤是您沒有檢查讀取返回的字節數,而是在字符串內容中添加了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被轉換為字符串結尾字節,這就是為什么您看不到'}'char的原因,因為之前有字符串結尾字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM