CAPTAIN SIM FORUM
767 Captain II (P3D4) >> 767 Captain II - General  >> SDK
https://www.captainsim.org/forum/csf.pl?num=1587503503

Message started by Skipy on Apr 21st, 2020 at 9:11pm

Title: Re: SimConnect C++ and CS767 LVARS...
Post by Dave Snyder WI on Mar 19th, 2021 at 10:33pm

Dave Snyder WI wrote on Mar 19th, 2021 at 3:18am:
Does anyone have any experience writing a C++ or C# application that uses SimConnect.  Specifically, I am writing an application that needs to R/W the CS767 LVARS for my home cockpit.  See other post recently here by me - I am subscribing to SIOC events (for my OpenCockpits hardware) and need to sync button presses etc in my hardware to the SIM.  Additionally, I will need to read values from CS767 LVARS to update my various LEDs etc.  Any advice would be greatly appreciated.


Found a great video on YouTube () that I think gets me very close...

I'll paste in the code that looks to at least get the built-in variables for generic aircraft, just not quite sure how to change the request for LVARS - I wonder if it is simple as putting in the klid number instead of the #define of the variable?

#include <windows.h>
#include <iostream>
#include "SimConnect.h"

int quit = 0;
HANDLE hSimConnect = NULL;

static enum DATA_DEFINE_ID {
     DEFINITION_1,
};

static enum DATA_REQUEST_ID {
     REQUEST_1,
     REQUEST_2,
};

struct SimResponse {
     double altitude;
     int32_t heading;
     int32_t speed;
     int32_t vertical_speed;
};

void CALLBACK MyDispatchProc1(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext) {
     switch (pData->dwID)
     {

     case SIMCONNECT_RECV_ID_SIMOBJECT_DATA:
     {
           SIMCONNECT_RECV_SIMOBJECT_DATA* pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;

           switch (pObjData->dwRequestID)
           {
           case REQUEST_1:

                 SimResponse* pS = (SimResponse*)&pObjData->dwData;

                 std::cout
                       
                       << "\rAltitude: " << pS->altitude
                       << " - Heading: " << pS->heading
                       << " - Speed (knots): " << pS->speed
                       << " - Vertical Speed: " << pS->vertical_speed
                       
                       << std::flush;

                 break;
           }
           break;
     }

     case SIMCONNECT_RECV_ID_QUIT:
     {
           quit = 1;
           break;
     }

     default:
           break;
     }
}

bool initSimEvents() {
     HRESULT hr;

     if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Client Event Demo", NULL, 0, NULL, 0))) {
           std::cout << "\nConnected To Microsoft Flight Simulator 2020!\n";

           // DATA
           hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Indicated Altitude", "feet");
           hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "HEADING INDICATOR", "degrees", SIMCONNECT_DATATYPE_INT32);
           hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Airspeed Indicated", "knots", SIMCONNECT_DATATYPE_INT32);
           hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "VERTICAL SPEED", "Feet per second", SIMCONNECT_DATATYPE_INT32);

           // EVERY SECOND REQUEST DATA FOR DEFINITION 1 ON THE CURRENT USER AIRCRAFT (SIMCONNECT_OBJECT_ID_USER)
           hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_1, DEFINITION_1, SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_SECOND);

           // Process incoming SimConnect Server messages
           while (quit == 0) {
                 // Continuously call SimConnect_CallDispatch until quit - MyDispatchProc1 will handle simulation events
                 SimConnect_CallDispatch(hSimConnect, MyDispatchProc1, NULL);
                 Sleep(1);
           }

           hr = SimConnect_Close(hSimConnect);
           return true;
     }
     else {
           std::cout << "\nFailed to Connect!!!!\n";
           return false;
     }
}

int main() {
     initSimEvents();
     return 0;
}


For example, in the code above, I could try replacing "HEADING INDICATOR" with the numeric klid?

Anyone know?  I'll code this up this weekend and find out...


CAPTAIN SIM FORUM » Powered by YaBB 2.6.0!
YaBB Forum Software © 2000-2024. All Rights Reserved.