I'm trying to read engine RPM in a Cassna 152. After trying a multitude of ways to read a SimVar, I ended with Lockheed's sample and tweaked it (like changing the read interval to Frames).
{ I isolated my SimConnect code to separate .h file cause I use a USB code generator for my PIC and C++ code, and it rewrites over the C++ program. That way I have very little modifications to re-do each time I change my USB code. }
I trigger the CallBack from a Button control for the time being, it will be moved into a Timer later.
private: System::Void MainForm_Load(System::Object^ sender, System::EventArgs^ e) { OpenSimConnect(); } private: System::Void bUpdateIndicators_Click(System::Object^ sender, System::EventArgs^ e) { SimConnect_CallDispatch(hSimConnect, MyDispatchProcPDR, NULL); //this->tbEngine1_RPM->Text = intEngine1_RPM; } private: System::Void bClose_Click(System::Object^ sender, System::EventArgs^ e) { CloseSimConnect(); Application::Exit(); }
The SimConnect code:
//Copyright (c) Lockheed Martin Corporation. All rights reserved. //------------------------------------------------------------------------------ // // SimConnect Tagged Data Request Sample // // Description: // After a flight has loaded, request the vertical speed and pitot // heat switch setting of the user aircraft, but only when the data // has changed //------------------------------------------------------------------------------ #pragma once #include <windows.h> #include "SimConnect.h" #include <tchar.h> #include <stdio.h> #include <strsafe.h> int quit = 0; HANDLE hSimConnect = NULL; // A basic structure for a single item of returned data struct StructOneDatum { int id; float value; }; // maxReturnedItems is 3 in this case, as the sample only requests RPM, // vertical speed and pitot heat switch data #define maxReturnedItems 3 // A structure that can be used to receive Tagged data struct StructDatum { StructOneDatum datum[maxReturnedItems]; }; static enum EVENT_PDR { EVENT_SIM_START, }; static enum DATA_DEFINE_ID { DEFINITION_PDR, }; static enum DATA_REQUEST_ID { REQUEST_PDR, }; static enum DATA_NAMES { DATA_ENGINE1_RPM, DATA_VERTICAL_SPEED, DATA_PITOT_HEAT, }; void OpenSimConnect() { HRESULT hr; if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Open", NULL, 0, 0, 0))) { hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_PDR, "PROP RPM:1", "rpm", SIMCONNECT_DATATYPE_FLOAT32, 0, DATA_ENGINE1_RPM); hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_PDR, "Vertical Speed", "Feet per second", SIMCONNECT_DATATYPE_FLOAT32, 0, DATA_VERTICAL_SPEED); hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_PDR, "Pitot Heat", "Bool", SIMCONNECT_DATATYPE_FLOAT32, 0, DATA_PITOT_HEAT); hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_PDR, DEFINITION_PDR, SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD_VISUAL_FRAME, 0, 0); } } void CloseSimConnect() { HRESULT hr; hr = SimConnect_Close(hSimConnect); } void CALLBACK MyDispatchProcPDR(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext) { HRESULT hr; float fltEngine1_RPM; switch (pData->dwID) { case SIMCONNECT_RECV_ID_SIMOBJECT_DATA: { SIMCONNECT_RECV_SIMOBJECT_DATA* pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData; switch (pObjData->dwRequestID) { case REQUEST_PDR: { int count = 0;; StructDatum* pS = (StructDatum*)&pObjData->dwData; // There can be a minimum of 1 and a maximum of maxReturnedItems // in the StructDatum structure. The actual number returned will // be held in the dwDefineCount parameter. while (count < (int)pObjData->dwDefineCount) { switch (pS->datum[count].id) { case DATA_ENGINE1_RPM: // printf("\nEngine 1 RPM = %f", pS->datum[count].value); fltEngine1_RPM = pS->datum[count].value; break; case DATA_VERTICAL_SPEED: // printf("\nVertical speed = %f", pS->datum[count].value); break; case DATA_PITOT_HEAT: // printf("\nPitot heat = %f", pS->datum[count].value); break; default: // printf("\nUnknown datum ID: %d", pS->datum[count].id); break; } ++count; } break; } default: break; } break; } } }
I added Engine1 RPM for my application. I get weird data when I add a Breakpoint at where "I think" Engine 1 RPM is read.
I'm a main-frame programmer by trade, so please disregard my ignorance when it comes to coding C++ and using structures and variables properly in C++ (I only started C++ this year on my own).
My goal is to get that Engine 1 RPM into a variable that I can handle, like displaying on-screen, but the main goal is to move to USB variables for download to a PIC mcu.
Thanks for any help!
Robert