@BitsplashIO ok thank you so much.
And i would like to know how is it possible to modify variables to enter variables from a tcp connexion? thanks
@BitsplashIO ok thank you so much.
And i would like to know how is it possible to modify variables to enter variables from a tcp connexion? thanks
Hi there. You can write a c# program that populates data to the chart from a tcp connection. However there is no out of the box way to do that , since this is really program depended and different for each use case.
you can also change category setting from code , if thatâs what you mean. Have a look at the following page : Configuring categories from script â BitSplash IO
Ok thanks.
I just want to âtransferâ datas from a sensor with a tcp connexion. Would be possible
in case, if i âmodifyâ the streaming script like this could work?:
using System.Net.Sockets;
using System.Text;
using UnityEngine;
public class StreamingGraph : MonoBehaviour
{
public GraphChart Graph;
public int TotalPoints = 5;
float lastTime = 0f;
float lastX = 0f;
private TcpListener listener;
private bool isListening = true;
void Start()
{
if (Graph == null) // Vérification si le Graph est défini
return;
float x = 0f;
// Initialisation du listener TCP
listener = new TcpListener(IPAddress.Any, 12345); // Change le port selon tes besoins
listener.Start();
StartListening();
Graph.DataSource.ClearCategory("Player 1"); // Efface la catégorie "Player 1"
Graph.DataSource.ClearCategory("Player 2"); // Efface la catégorie "Player 2"
for (int i = 0; i < TotalPoints; i++) // Ajoute des points aléatoires au graphique
{
Graph.DataSource.AddPointToCategoryRealtime("Player 1", x, Random.value * 20f + 10f);
Graph.DataSource.AddPointToCategoryRealtime("Player 2", x, Random.value * 10f);
x += Random.value * 3f;
lastX = x;
}
}
void Update()
{
float time = Time.time;
if (lastTime + 2f < time)
{
lastTime = time;
lastX += Random.value * 3f;
Graph.DataSource.AddPointToCategoryRealtime("Player 1", lastX, Random.value * 20f + 10f, 1f);
Graph.DataSource.AddPointToCategoryRealtime("Player 2", lastX, Random.value * 10f, 1f);
}
}
void StartListening()
{
// Lancer un nouveau thread pour écouter les données TCP
System.Threading.Thread listenerThread = new System.Threading.Thread(ListenForData);
listenerThread.Start();
}
void ListenForData()
{
while (isListening)
{
using (TcpClient client = listener.AcceptTcpClient())
{
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
string data = Encoding.UTF8.GetString(buffer, 0, bytesRead);
ProcessReceivedData(data);
}
}
}
}
void ProcessReceivedData(string data)
{
// Traiter les données reçues
float value;
if (float.TryParse(data, out value))
{
// Mettre à jour le graphique avec les données reçues
lastX += Random.value * 3f; // Pour garder l'axe des X incrémental
Graph.DataSource.AddPointToCategoryRealtime("Player 1", lastX, value, 1f);
// On pourrait aussi ajouter un deuxiÚme joueur ou d'autres catégories
}
}
private void OnDestroy()
{
// Nettoyer le listener lors de la destruction
isListening = false;
listener.Stop();
}
}
Hi there.
Yes it will work with any data provided. make sure to know how your sensor communicates via tcp and your it.
Ok great got it
Instead i started with your tutorial to understand the streaming action. It shows a error : GraphData doesnât contain any âAddPointToCategoryRealtimeâ definition. Any things to add inside GraphData.cs?
thank you again!
This method is available in the regular version, if you are using the lite edition try upgrading to the regular. or alternativly use AddPointToCategor evry second or so.