Hello everyone
I wrote a very simple Windows Form program that can display flight parameters on a second computer connected to the same wired network.
I also wrote a mode that sends data to this windows form via KSP.
KSP Plug-in
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
namespace KSP_Connect
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class VeriAktarimi : MonoBehaviour
{
private const int port = 1234;
private const string serverIP = "192.168.0.108"; // Receiver IP
public void Update()
{
Double Altitude = Math.Round(FlightGlobals.ActiveVessel.altitude,2);
Double RadarAltitude = Math.Round(FlightGlobals.ActiveVessel.radarAltitude,2);
Double SpeedVector = Math.Round(FlightGlobals.ActiveVessel.speed,2);
Double SurfaceSpeed = Math.Round(FlightGlobals.ActiveVessel.srfSpeed, 2);
Double VerticalSpeed = Math.Round(FlightGlobals.ActiveVessel.verticalSpeed, 2);
Double OrbitalSpeed = Math.Round(FlightGlobals.ActiveVessel.obt_speed);
using (TcpClient client = new TcpClient(serverIP, port))
using (NetworkStream stream = client.GetStream())
{
string data = Altitude + ":" + RadarAltitude + ":" + SpeedVector + ":" + SurfaceSpeed + ":" + VerticalSpeed
+":"+OrbitalSpeed;
byte[] buffer = Encoding.ASCII.GetBytes(data);
stream.Write(buffer, 0, buffer.Length);
}
}
}
}
Codes of the receiver windows form and the visual of receiver:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Seri_Iletisim_Alici
{
public partial class Form1 : Form
{
private const int port = 1234;
private TcpListener listener;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartListening()
{
try
{
while (true)
{
TcpClient client = listener.AcceptTcpClient();
HandleClient(client);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
private void HandleClient(TcpClient client)
{
try
{
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
string[] parts = data.Split(':');
UpdateLabels(parts[0], parts[1],parts[2],parts[3],parts[4],parts[5]);
client.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}
private void UpdateLabels(string value1, string value2,string value3,string value4,string value5, string value6)
{
if (label1.InvokeRequired || label2.InvokeRequired )
{
Invoke(new MethodInvoker(() => UpdateLabels(value1, value2, value3, value4, value5, value6)));
}
else
{
lblAltitude.Text = value1;
lblRadarAltitude.Text = value2;
lblSpeed.Text = value3;
lblSurfaceSpeed.Text = value4;
lblVerticalSpeed.Text = value5;
lblOrbitalSpeed.Text = value6;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
listener = new TcpListener(IPAddress.Any, port);
listener.Start();
MessageBox.Show("Server started successfully!");
Task.Run(() => StartListening());
}
catch (Exception ex)
{
MessageBox.Show("Error starting server: " + ex.Message);
}
}
}
}
The codes and program-plug-in are working normally.
https://drive.google.com/file/d/1NE-NeZNrhFsHLqPmy5MMYZuAl63oL9Xr/view?usp=sharing
However, I cannot access other parameters of the flight. For example, Throttle Rate, apoapsis and periapsis etc.
How can I access these parameters?