Skip to content

Understanding the code  #120

@FarmerJon1066

Description

@FarmerJon1066

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();
    }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions