Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding the code #120

Open
FarmerJon1066 opened this issue May 11, 2024 · 2 comments
Open

Understanding the code #120

FarmerJon1066 opened this issue May 11, 2024 · 2 comments

Comments

@FarmerJon1066
Copy link

I want to make sure I am heading down the right path for getting the data from the memory mapped file. This is the code I have so far but it always returns crap data of (Current truck speed: 1E-45 m/s (3E-45 mph) Truck position: X=1E-45, Y=5.7172556E-31, Z=0)

using System;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using System.Threading;

namespace ATS_API
{
class Program
{
static void Main()
{
var telemetryHandler = new TelemetryHandler("Local\SCSTelemetry");
telemetryHandler.Start();
}
}

// Define the TelemetryData structure according to the SCS SDK plugin documentation
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct TelemetryData
{
    
    public float speed; // Truck speed in m/s
    public FVector position; // Truck position (assuming FVector is the correct type)
    
}


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct FVector
{
    public float X;
    public float Y;
    public float Z;
}

public class TelemetryHandler
{
    private MemoryMappedFile memoryMappedFile;
    private MemoryMappedViewAccessor accessor;

    public TelemetryHandler(string sharedMemoryName)
    {
        memoryMappedFile = MemoryMappedFile.OpenExisting(sharedMemoryName);
        accessor = memoryMappedFile.CreateViewAccessor();
    }

    public void Start()
    {
        Console.WriteLine("Telemetry data is ready.");
        while (true)
        {
            Console.Clear(); // Clear the console window
            var telemetryData = ReadTelemetryData();
            Console.WriteLine($"Current truck speed: {telemetryData.speed} m/s ({telemetryData.speed * 2.23694f} mph)");
            Console.WriteLine($"Truck position: X={telemetryData.position.X}, Y={telemetryData.position.Y}, Z={telemetryData.position.Z}");
            Thread.Sleep(1000); // Wait for 1 second before the next update
        }
    }

    private TelemetryData ReadTelemetryData()
    {
        int dataSize = Marshal.SizeOf(typeof(TelemetryData));
        byte[] rawData = new byte[dataSize];

        // Ensure the accessor has enough capacity to read the data
        if (accessor.Capacity >= dataSize)
        {
            accessor.ReadArray(0, rawData, 0, rawData.Length);
            GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
            TelemetryData data = (TelemetryData)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(TelemetryData));
            handle.Free();
            return data;
        }
        else
        {
            Console.WriteLine("Shared memory does not contain enough data.");
            return new TelemetryData(); // Return a default structure
        }
    }

    ~TelemetryHandler()
    {
        accessor?.Dispose();
        memoryMappedFile?.Dispose();
    }
}

}

@RenCloud
Copy link
Owner

Hey, if you didn't modify the shared memory mapped created by the plugin, the offset for the speed is somewhat over 700 and the truck position even has a much higher offset in the memory. If I read your code correctly, you take the first bytes from it. But there is other data located. The data starts with sdkActive, empty space, paused, empty space, time ... and so on.

In scs-telemetry/inc/scs-telemetry-common.hpp offsets are given to specific areas of the memory. Or the unwrapping side scs-client/C#/SCSSdkClient/SCSSdkConvert.cs.

To direct fetch specific values, you would need to create/calculate the offset to the specific value first. It is not that difficult, but currently there is no available table for that.

@FarmerJon1066
Copy link
Author

FarmerJon1066 commented May 12, 2024

Thanks for the fast reply so something like this then? The data seems to make much better sense now, its just finding the right offset. Current truck speed: 3 m/s (6.7108197 mph) Truck cabin position: X=0, Y=0.9946883, Z=-1.8208612

using System;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using System.Threading;

namespace ATS_API
{
class Program
{
static void Main()
{
var telemetryHandler = new TelemetryHandler("Local\SCSTelemetry");
telemetryHandler.Start();
}
}

// Define the TelemetryData structure with explicit layout to set specific field offsets
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Pack = 1)]
public struct TelemetryData
{
    // Define the speed field at offset 700
    [FieldOffset(700)]
    public float speed; // Truck speed in m/s

    // Define the cabin position fields at offset 1640
    [FieldOffset(1640)]
    public FVector cabinPosition; // Truck cabin position
}


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct FVector
{
    public float X;
    public float Y;
    public float Z;
}

public class TelemetryHandler
{
    private MemoryMappedFile memoryMappedFile;
    private MemoryMappedViewAccessor accessor;

    public TelemetryHandler(string sharedMemoryName)
    {
        memoryMappedFile = MemoryMappedFile.OpenExisting(sharedMemoryName);
        accessor = memoryMappedFile.CreateViewAccessor();
    }

    public void Start()
    {
        Console.WriteLine("Telemetry data is ready.");
        while (true)
        {
            Console.Clear(); // Clear the console window
            var telemetryData = ReadTelemetryData();
            Console.WriteLine($"Current truck speed: {telemetryData.speed} m/s ({telemetryData.speed * 2.23694f} mph)");
            Console.WriteLine($"Truck cabin position: X={telemetryData.cabinPosition.X}, Y={telemetryData.cabinPosition.Y}, Z={telemetryData.cabinPosition.Z}");
            Thread.Sleep(1000); // Wait for 1 second before the next update
        }
    }

    private TelemetryData ReadTelemetryData()
    {
        int dataSize = Marshal.SizeOf(typeof(TelemetryData));
        byte[] rawData = new byte[dataSize];

        // Ensure the accessor has enough capacity to read the data
        if (accessor.Capacity >= dataSize)
        {
            accessor.ReadArray(0, rawData, 0, rawData.Length);
            GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
            TelemetryData data = (TelemetryData)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(TelemetryData));
            handle.Free();
            return data;
        }
        else
        {
            Console.WriteLine("Shared memory does not contain enough data.");
            return new TelemetryData(); // Return a default structure
        }
    }

    ~TelemetryHandler()
    {
        accessor?.Dispose();
        memoryMappedFile?.Dispose();
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants