为什么我的程序崩溃与一个无效的json原语错误?

e5nszbig  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(102)

我正在做一个飞行控制器程序在c#和这接收数据函数,该程序将运行约2分钟前,程序崩溃,并返回错误无效JSON原语。我已经尝试谷歌搜索,并没有找到任何关于我的问题,因为我看不到任何问题的程序。函数反序列化JSON是在这里,这也是导致错误并返回无效JSON原语消息的行。

void ReceiveData() //This function is used to listen for messages from the flight simulator
{
    while (true)
    {
        NetworkStream stream = client.GetStream(); //sets the newtwork stream to the client's stream
        byte[] buffer = new byte[256]; //Defines the max amount of bytes that can be sent
        int bytesRead = stream.Read(buffer, 0, buffer.Length);
        if (bytesRead > 0)
        {
            string jsonreceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); //Converts the recieved data into ASCII for the json variable
            JavaScriptSerializer Deserializer = new JavaScriptSerializer();
            TelemetryUpdate telemetry = Deserializer.Deserialize<TelemetryUpdate>(jsonreceived);
            this.Invoke(new Action(() => { TelemetryReceivedLabel.Text = jsonreceived; })) ;
            Updatelabels(telemetry); //runs the update labels function with the telemetry data as an arguement
            lock (@"c:\temp\BlackBox.txt")
            {
            File.AppendAllText(@"c:\temp\BlackBox.txt", "Data Received" + jsonreceived + DateTime.Now.ToString("h:mm:ss tt") + Environment.NewLine); //this appends the json data to the blackbox file
            }
        }
    }
}

该程序工作完全正常,并传递信息到变量如何我想要它太像这样,我已经记录了JSON数据t检查,如果变量名匹配等,他们都适合正确。

void Updatelabels(TelemetryUpdate telemetry) //this function updates all the labels with received data
{
    if (this.InvokeRequired) //invokes the telemetryupdatedelegate
    {
        this.Invoke(new TelemetryUpdateDelegate(Updatelabels), telemetry);
        return;
    }
    
    altitudelabel.Text = string.Format("{0:0.00}", telemetry.Altitude);
    speedlabel.Text = string.Format("{0:0.00}", telemetry.Speed);
    pitchlabel.Text = string.Format("{0:0.00}", telemetry.Pitch);
    verticalspeedlabel.Text = string.Format("{0:0.00}", telemetry.VerticalSpeed);
    throttlelabel.Text = string.Format("{0:0.00}", telemetry.Throttle);
    elevatorspeedlabel.Text = string.Format("{0:0.00}", telemetry.ElevatorPitch);

    WarningUpdateEvent?.Invoke(telemetry); //invokes the warning update event
    ReceivedDataGrid.Rows.Insert(0,telemetry.Altitude, telemetry.Speed, telemetry.Pitch, telemetry.VerticalSpeed, telemetry.Throttle, telemetry.ElevatorPitch, telemetry.WarningCode); //populates the datagrid with the received data
    ReceivedDataGrid.AutoResizeColumns(); //resizes the columns to fit properly
}
jdgnovmf

jdgnovmf1#

此问题似乎源于与datagrid相关的内存泄漏。

相关问题