Skip to content

Commit

Permalink
Fix #6 - SimConnect.cfg in My Documents - conflict with other mods (#7)
Browse files Browse the repository at this point in the history
* Fixes #6, setting Local as default location for writing SimConnect.cfg
* Added all simulation variables and units as documented by the SimVarWatcher example in the MSFS SDK
* Verified that bundled SimConnect binaries in SDK 0.10.0 is the same as in this release.
  • Loading branch information
TimianHeber authored Feb 27, 2021
1 parent f743f93 commit 1465543
Show file tree
Hide file tree
Showing 17 changed files with 3,066 additions and 102 deletions.
65 changes: 55 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ A simple easy-to-use wrapper for the Flight Simulator 2020 SimConnect library. A
If, on the other hand, you just want to connect to Flight Simulator and read some information this may give you a quicker start.

FsConnect uses the _Microsoft.FlightSimulator.SimConnect_ .NET Framework library and the underlying native x64 _simconnect.dll_ library.
These files are distributed via the Flight Simulator 2020 SDK, currently version 0.6.1, but are included for easy use.
These files are distributed via the Flight Simulator 2020 SDK, currently version 0.10.0, but are included for easy use.

At the moment this project is intended as an easier to use wrapper than the current SimConnect for simple projects, creating a simpler C# programming model and reducing the need for repeated boiler plate code. Expect breaking changes.

Expand All @@ -12,7 +12,8 @@ At the moment this project is intended as an easier to use wrapper than the curr
* Supports registering and requesting simple simulation variables.
* Supports updating simulation variables.
* Does not require a Windows message pump.
* NuGet package handles deployment of native binaries, just add reference to package.
* NuGet package handles deployment of native binaries, just add reference to the package.
* Supports enums for all known simulation variables and units.

# Getting started
* Download the Flight Simulator SDK.
Expand All @@ -27,7 +28,37 @@ At the moment this project is intended as an easier to use wrapper than the curr
1) Create a .NET Framework Console project that target x64.
2) Add a reference to the CTrue.FsConnect package.
3) Set up Flight Simulator to receive remote TCP connections, see the SimConnect.xml file, and configure the example accordingly.
4) See example below:

Ensure that the SimConnect.xml file has an entry for the type of connection that you want to establish.
E.g. for a remote TCP IvP4 connection, the file should have an entry such as this:

```xml
<SimConnect.Comm>
<Descr>Static IP4 port</Descr>
<Protocol>IPv4</Protocol>
<Scope>remote</Scope>
<Port>33333</Port>
<MaxClients>64</MaxClients>
<MaxRecvSize>41088</MaxRecvSize>
</SimConnect.Comm>
```

For a local TCP IPv4 connection:

```xml
<SimConnect.Comm>
<Descr>Static IP4 port</Descr>
<Protocol>IPv4</Protocol>
<Scope>local</Scope>
<Port>500</Port>
<MaxClients>64</MaxClients>
<MaxRecvSize>41088</MaxRecvSize>
</SimConnect.Comm>
```

The SimConnect.xml can be found in the "%AppData%\Microsoft Flight Simulator" folder.

4) See example below, or the CTrue.FsConnect.ExampleConsole project in the GitHub repo:

```csharp
using System;
Expand All @@ -52,19 +83,31 @@ namespace FsConnectTest
public double Longitude;
public double Altitude;
public double Heading;
public double SpeedMpS;
public double SpeedKnots;
}

public class FsConnectTestConsole
{
public static void Main()
public static void Main(string[] args)
{
string hostName = "localhost";
uint port = 500;

// Also supports "somehostname 1234"
if(args.Length == 2)
{
hostName = args[0];
port = uint.Parse(args[1]);
}

FsConnect fsConnect = new FsConnect();

// Specify where the SimConnect.cfg should be written to
fsConnect.SimConnectFileLocation = SimConnectFileLocation.MyDocuments;
fsConnect.SimConnectFileLocation = SimConnectFileLocation.Local;

// Creates a SimConnect.cfg and connect to Flight Simulator using this configuration.
fsConnect.Connect("TestApp", "localhost", 500, SimConnectProtocol.Ipv4);
fsConnect.Connect("TestApp", hostName, port, SimConnectProtocol.Ipv4);

// Other alternatives, use existing SimConfig.cfg and specify config index:
// fsConnect.Connect(1);
Expand All @@ -79,10 +122,12 @@ namespace FsConnectTest
definition.Add(new SimProperty("Title", null, SIMCONNECT_DATATYPE.STRING256));
definition.Add(new SimProperty("Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty("Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));

// Can also use predefined enums for sim variables and units (incomplete)
definition.Add(new SimProperty(FsSimVar.PlaneAltitude, FsUnit.Feet, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.PlaneHeadingDegreesTrue, FsUnit.Degrees, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.MeterPerSecond, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.Knot, SIMCONNECT_DATATYPE.FLOAT64));

fsConnect.RegisterDataDefinition<PlaneInfoResponse>(Requests.PlaneInfo, definition);

Expand All @@ -96,7 +141,7 @@ namespace FsConnectTest
if (e.RequestId == (uint)Requests.PlaneInfo)
{
PlaneInfoResponse r = (PlaneInfoResponse)e.Data;
Console.WriteLine($"{r.Latitude:F4} {r.Longitude:F4} {r.Altitude:F1} {r.Heading}");
Console.WriteLine($"{r.Latitude:F4} {r.Longitude:F4} {r.Altitude:F1}ft {r.Heading:F1}deg {r.SpeedMpS:F0}m/s {r.SpeedKnots:F0}kt");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\CTrue.FsConnect\CTrue.FsConnect.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.FlightSimulator.SimConnect">
<HintPath>..\Dependencies\SimConnect\lib\net40\Microsoft.FlightSimulator.SimConnect.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<None Include="..\Dependencies\SimConnect\build\SimConnect.dll" Visible="false">
<Link>%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
85 changes: 85 additions & 0 deletions src/CTrue.FsConnect.ExampleConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using CTrue.FsConnect;
using Microsoft.FlightSimulator.SimConnect;

namespace FsConnectTest
{
public enum Requests
{
PlaneInfo = 0
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct PlaneInfoResponse
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public String Title;
public double Latitude;
public double Longitude;
public double Altitude;
public double Heading;
public double SpeedMpS;
public double SpeedKnots;
}

public class FsConnectTestConsole
{
public static void Main(string[] args)
{
string hostName = "localhost";
uint port = 500;

// Also supports "somehostname 1234"
if(args.Length == 2)
{
hostName = args[0];
port = uint.Parse(args[1]);
}

FsConnect fsConnect = new FsConnect();

// Specify where the SimConnect.cfg should be written to
fsConnect.SimConnectFileLocation = SimConnectFileLocation.Local;

// Creates a SimConnect.cfg and connect to Flight Simulator using this configuration.
fsConnect.Connect("TestApp", hostName, port, SimConnectProtocol.Ipv4);

// Other alternatives, use existing SimConfig.cfg and specify config index:
// fsConnect.Connect(1);
// or
// fsConnect.Connect();

fsConnect.FsDataReceived += HandleReceivedFsData;

List<SimProperty> definition = new List<SimProperty>();

// Consult the SDK for valid sim variable names, units and whether they can be written to.
definition.Add(new SimProperty("Title", null, SIMCONNECT_DATATYPE.STRING256));
definition.Add(new SimProperty("Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty("Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64));

// Can also use predefined enums for sim variables and units (incomplete)
definition.Add(new SimProperty(FsSimVar.PlaneAltitude, FsUnit.Feet, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.PlaneHeadingDegreesTrue, FsUnit.Degrees, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.MeterPerSecond, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.Knot, SIMCONNECT_DATATYPE.FLOAT64));

fsConnect.RegisterDataDefinition<PlaneInfoResponse>(Requests.PlaneInfo, definition);

fsConnect.RequestData(Requests.PlaneInfo);
Console.ReadKey();
fsConnect.Disconnect();
}

private static void HandleReceivedFsData(object sender, FsDataReceivedEventArgs e)
{
if (e.RequestId == (uint)Requests.PlaneInfo)
{
PlaneInfoResponse r = (PlaneInfoResponse)e.Data;
Console.WriteLine($"{r.Latitude:F4} {r.Longitude:F4} {r.Altitude:F1}ft {r.Heading:F1}deg {r.SpeedMpS:F0}m/s {r.SpeedKnots:F0}kt");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"CTrue.FsConnect.ExampleConsole": {
"commandName": "Project",
"commandLineArgs": "localhost 500"
}
}
}
1 change: 1 addition & 0 deletions src/CTrue.FsConnect.TestConsole/PlaneInfoResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public struct PlaneInfoResponse
public double AltitudeAboveGround;
public double Altitude;
public double Heading;
public double Speed;
};
}
5 changes: 3 additions & 2 deletions src/CTrue.FsConnect.TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ private static void InitializeDataDefinitions(FsConnect fsConnect)
definition.Add(new SimProperty(FsSimVar.PlaneLatitude, FsUnit.Radians, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.PlaneLongitude, FsUnit.Radians, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.PlaneAltitudeAboveGround, FsUnit.Feet, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.PlaneAltitude, FsUnit.Feet, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty("PLANE ALTITUDE", "Feet", SIMCONNECT_DATATYPE.FLOAT64)); // Example using known/new values
definition.Add(new SimProperty(FsSimVar.PlaneHeadingDegreesTrue, FsUnit.Radians, SIMCONNECT_DATATYPE.FLOAT64));
definition.Add(new SimProperty(FsSimVar.AirspeedTrue, FsUnit.Knot, SIMCONNECT_DATATYPE.FLOAT64));

fsConnect.RegisterDataDefinition<PlaneInfoResponse>(Definitions.PlaneInfo, definition);
}
Expand All @@ -186,7 +187,7 @@ private static void HandleReceivedFsData(object sender, FsDataReceivedEventArgs
{
_planeInfoResponse = (PlaneInfoResponse)e.Data;

Console.WriteLine($"Pos: ({FsUtils.Rad2Deg(_planeInfoResponse.Latitude):F4}, {FsUtils.Rad2Deg(_planeInfoResponse.Longitude):F4}), Alt: {_planeInfoResponse.Altitude:F0} ft, Hdg: {FsUtils.Rad2Deg(_planeInfoResponse.Heading):F1} deg");
Console.WriteLine($"Pos: ({FsUtils.Rad2Deg(_planeInfoResponse.Latitude):F4}, {FsUtils.Rad2Deg(_planeInfoResponse.Longitude):F4}), Alt: {_planeInfoResponse.Altitude:F0} ft, Hdg: {FsUtils.Rad2Deg(_planeInfoResponse.Heading):F1} deg, Speed: {_planeInfoResponse.Speed:F0} kt");
}
}
catch (Exception ex)
Expand Down
10 changes: 1 addition & 9 deletions src/CTrue.FsConnect.TestConsole/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
"profiles": {
"FsConnectTestConsole": {
"commandName": "Project",
"commandLineArgs": "-h 127.0.0.1 -p 500"
},
"FsConnectTestConsole1": {
"commandName": "Project",
"commandLineArgs": "-i 0"
},
"FsConnectTestConsole2": {
"commandName": "Project",
"commandLineArgs": ""
"commandLineArgs": "-h localhost -p 500"
}
}
}
14 changes: 14 additions & 0 deletions src/CTrue.FsConnect.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CTrue.FsConnect.ExampleConsole", "CTrue.FsConnect.ExampleConsole\CTrue.FsConnect.ExampleConsole.csproj", "{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -46,6 +48,18 @@ Global
{17BD0880-D516-472B-A615-92C8265CC8CC}.Release|x64.Build.0 = Release|Any CPU
{17BD0880-D516-472B-A615-92C8265CC8CC}.Release|x86.ActiveCfg = Release|Any CPU
{17BD0880-D516-472B-A615-92C8265CC8CC}.Release|x86.Build.0 = Release|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Debug|x64.ActiveCfg = Debug|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Debug|x64.Build.0 = Debug|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Debug|x86.ActiveCfg = Debug|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Debug|x86.Build.0 = Debug|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Release|Any CPU.Build.0 = Release|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Release|x64.ActiveCfg = Release|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Release|x64.Build.0 = Release|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Release|x86.ActiveCfg = Release|Any CPU
{EF83E27B-B413-4FEC-A49B-2F8E5B4CA213}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 7 additions & 6 deletions src/CTrue.FsConnect/CTrue.FsConnect.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
<Product>Flight Simulator Connect</Product>
<Copyright />
<Description>An easy to use wrapper for SimConnect, for connection to Flight Simulator 2020.
Contains SimConnect binaries, as distributed by the Flight Simulator 20202 SDK 0.6.1 release.</Description>
Contains SimConnect binaries, as distributed by the Flight Simulator 20202 SDK 0.10.0 release.</Description>
<Authors>C-True</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/c-true/FsConnect</RepositoryUrl>
<PackageTags>msfs flight-simulator simconnect</PackageTags>
<Version>1.0.3</Version>
<Version>1.1.1</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>Support for using predefined SimConnect.cfg or specifying location for where it should be written.
Verified that dependencies are the same for the SDK 0.6.1 release.</PackageReleaseNotes>
<AssemblyVersion>1.0.3.0</AssemblyVersion>
<FileVersion>1.0.3.0</FileVersion>
<PackageReleaseNotes>Default location for writing SimConnect.cfg file changed to Local, to avoid intefering with other SimConnect using applications/mods.
Verified that dependencies are the same for the SDK 0.10.0 release.
Support for all SimVars and units as enums, as documented by the SimVars example.</PackageReleaseNotes>
<AssemblyVersion>1.1.1.0</AssemblyVersion>
<FileVersion>1.1.1.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
2 changes: 1 addition & 1 deletion src/CTrue.FsConnect/FsConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private set
}

/// <inheritdoc />
public SimConnectFileLocation SimConnectFileLocation { get; set; } = SimConnectFileLocation.MyDocuments;
public SimConnectFileLocation SimConnectFileLocation { get; set; } = SimConnectFileLocation.Local;

/// <inheritdoc />
public event EventHandler ConnectionChanged;
Expand Down
Loading

0 comments on commit 1465543

Please sign in to comment.