public void OnMatlabResponseReceived(MatlabResponse res) { //Debug.Log($"Answer: {(res != null ? res.response : new MatlabResponse().response)}"); float value = res != null ? res.response : prevValue; text.text = value.ToString(); prevValue = value; }
IEnumerator _Connect() { try { socket = new TcpClient(); socket.Connect(host, port); connected = socket.Connected; connectionEstablished.Invoke(); Debug.Log("Connected!"); stream = socket.GetStream(); } catch { TryReconnect(reconnectTime); } while (connected) { connected = socket.Connected; if (connected == false) { connectionFinished.Invoke(); } try { if (isWriting) { deltaRpm = drivetrain.rpm - oldRpm; oldRpm = drivetrain.rpm; VehicleData obj = new VehicleData(drivetrain.rpm, drivetrain.gear - 1, drivetrain.velo * kMhRatio, deltaRpm); string wrapped = JsonUtility.ToJson(obj); byte[] bytes = Encoding.UTF8.GetBytes(wrapped); stream.Write(bytes, 0, bytes.Length); stream.Flush(); isWriting = false; } else { if (stream.CanRead) { byte[] bytes = new byte[512]; int nuberOfBytesRead = 0; string message = ""; while (stream.DataAvailable) { nuberOfBytesRead = stream.Read(bytes, 0, bytes.Length); message += Encoding.UTF8.GetString(bytes, 0, nuberOfBytesRead); } MatlabResponse response = JsonUtility.FromJson <MatlabResponse>(message); matlabResponseReceived.Invoke(response); } isWriting = true; } } catch (Exception e) { Debug.LogException(e); } yield return(new WaitForEndOfFrame()); } }