Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1
    10+ Posting Member
    Join Date
    Jul 2009
    Location
    Australia
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Thumbs up SimConnect GetPlaneHeading

    Here is my source code for the SimConnect GetPlaneHeading program for MS FSX.
    When launched with FSX it displays the heading in degrees of the plane on the console. This is the first step in getting data for input into the gyro compass to make it work.

    Code:
    //------------------------------------------------------------------------------
    //
    //  GetPlaneHeading V1.02 Beta
    //  
    //	Description:
    //				Open a connection to the server and get the users aircraft
    //				heading and write it to the console.
    //
    //	Written by Mechdave using FSX SDK code sample "RequestData" as a code base
    //
    //------------------------------------------------------------------------------
    
    #include 
    #include 
    #include 
    #include 
    
    #include "SimConnect.h"
    
    int     quit = 0;
    HANDLE  hSimConnect = NULL;
    
    struct Struct1
    {
        double  radians;
    };
    
    static enum EVENT_ID{
        EVENT_SIM_START,
    };
    
    static enum DATA_DEFINE_ID {
        DEFINITION_1,
    };
    
    static enum DATA_REQUEST_ID {
        REQUEST_1,
    };
    
    
    void CALLBACK MyDispatchProcRD(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
    {
        HRESULT hr;
    	static double conversion = 57.2957795; //Conversion for radians --> degrees courtesy of Google :)
    	double degrees=0;
        
        switch(pData->dwID)
        {
            case SIMCONNECT_RECV_ID_EVENT:
            {
                SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
                switch(evt->uEventID)
                {
                    case EVENT_SIM_START:
    					// Now the sim is running, request information on the user aircraft
                        hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1, DEFINITION_1, 0, SIMCONNECT_SIMOBJECT_TYPE_USER);
                        
    					break;
    
                    default:
                       break;
                }
                break;
            }
    
            case SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE:
            {
                SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE*)pData;
                switch(pObjData->dwRequestID)
                {
                    case REQUEST_1:
                    {
    					//Run below again to update the data.
    					hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1, DEFINITION_1, 0, SIMCONNECT_SIMOBJECT_TYPE_AIRCRAFT);
                        DWORD ObjectID = pObjData->dwObjectID;
                        Struct1 *pS = (Struct1*)&pObjData->dwData;
                        if (SUCCEEDED(sizeof(pS->radians))) // security check
                        {
                            degrees = pS->radians * conversion; //Convert the radians to degrees so a compare can be done with plane
    						printf("\nHeading in degrees = %f",degrees );
                        } 
                        break;
                    }
    
                    default:
                       break;
                }
                break;
            }
    
    
            case SIMCONNECT_RECV_ID_QUIT:
            {
                quit = 1;
                break;
            }
    
            default:
                printf("\nReceived:%d",pData->dwID);
                break;
        }
    }
    
    void GetHeading()
    {
        HRESULT hr;
    
        if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Data", NULL, 0, 0, 0)))
        {
            printf("\nConnected to Flight Simulator!");  
    		// Set up the data definition, but do not yet do anything with it
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "HEADING INDICATOR", "radians");
    		// Request an event when the simulation starts
            hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart");
    		while( quit == 0 )
            {
                SimConnect_CallDispatch(hSimConnect, MyDispatchProcRD, NULL);
                //Sleep(1); //To introduce a delay for a lower resolution.
            } 
    
            hr = SimConnect_Close(hSimConnect);
    	}
    }
    
    int __cdecl _tmain(int argc, _TCHAR* argv[])
    {
        GetHeading();
    
        return 0;
    }

  2. Thanks autocadplease thanked for this post
  3. #2
    10+ Posting Member
    Join Date
    Jul 2009
    Location
    Australia
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Post Re: SimConnect GetPlaneHeading for Gyro Compass instrument

    I found that the 1.02 Beta code had some major sim slow down problems to the extent where the sim stopped responding after a few minutes. Here is a much more elegant solution to the problem. The sim now only sends data when the turn angle exceeds roughly 1 step of the stepper motor, (.45 degrees), so we don't waste processing power in the interface software evaluating and ignoring values which don't matter. This may end up in a slightly jerky resolution with the stepper motor yet . If that is the case I shall have to reduce the angle before the sim sends more data to the gauges (console at the moment).

    Here is the code, (almost complete... sans some comments)
    Code:
    //------------------------------------------------------------------------------
    
    //
    
    //  GetPlaneHeading V1.03 Beta
    
    //  
    
    //	Description:
    
    //				Open a connection to the server and get the users aircraft
    
    //				heading and write it to the console. Now data is only recieved
    
    //				if aircraft turns more than .45 degrees. This is designed so 
    
    //				unneccessary cycles are not wasted on sending position data that will
    
    //				be discarded by the gyro compass driver software.
    
    //
    
    //	Written by Mechdave using FSX SDK Sample RequestData as a code base
    
    //
    
    //------------------------------------------------------------------------------
    
    
    
    #include 
    
    #include 
    
    #include 
    
    #include 
    
    
    
    #include "SimConnect.h"
    
    
    
    int quit = 0;
    
    HANDLE  hSimConnect = NULL;
    
    
    
    struct Struct1
    
    {
    
        double  radians;
    
    };
    
    
    
    static enum EVENT_ID{
    
        EVENT_SIM_START,
    
    };
    
    
    
    static enum DATA_DEFINE_ID {
    
        DEFINITION_1,
    
    };
    
    
    
    static enum DATA_REQUEST_ID {
    
        REQUEST_1,
    
    };
    
    
    
    
    
    void CALLBACK MyDispatchProcRD(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
    
    {
    
        HRESULT hr;
    
    	static double conversion = 57.295778; //Conversion for radians --> degrees courtesy of Google :)
    
    	double degrees=0;
    
    	switch (pData->dwID)
    
    	{
    
    	case SIMCONNECT_RECV_ID_EVENT:
    
    		{
    
    			SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*) pData;
    
    			switch (evt->uEventID)
    
    			{
    
    			case EVENT_SIM_START:
    
    				{
    
    					//Now the Sim is running, request information on the user aircraft and get it to send data when plane exceeds turn
    					//angle (last) argument in function SimConnect_AddToDataDefinition()
    
    					hr = SimConnect_RequestDataOnSimObject(hSimConnect, REQUEST_1, DEFINITION_1, SIMCONNECT_OBJECT_ID_USER,SIMCONNECT_PERIOD_VISUAL_FRAME,SIMCONNECT_DATA_REQUEST_FLAG_CHANGED);
    
    					//printf("\nSim successfully started"); //Debug output
    
    					break;
    
    				}
    
    			default:
    
    			{
    
    				//printf("Error1"); //Debug output
    
    				break;
    
    				}
    
    			}
    
    			break;
    
    		}
    
    	case SIMCONNECT_RECV_ID_SIMOBJECT_DATA:
    
    		{
    
    			SIMCONNECT_RECV_SIMOBJECT_DATA *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;
    
    			switch(pObjData->dwRequestID)
    
    			{
    
    			case REQUEST_1:
    
    				{
    
    					Struct1 *pS = (Struct1*)&pObjData->dwData;
    
    					degrees = pS->radians * conversion; //Used to provide a user interface
    
    					printf("\nDegrees =%f", degrees ); //to show program is actually doing something :) Eventually the code to
    					//send the data to the gyro compass will live here
    
                        break; 
    
    				}
    
    			default:
    
    				{
    
    				//printf("Error"); //Debug output
    
    				break;
    
    				}
    
    			}
    
    			break;
    
    		}
    
    	case SIMCONNECT_RECV_ID_QUIT:
    
    		{
    
    			quit = 1;
    
    			break;
    
    		}
    
    	default:
    
    		{
    
    			//printf("\nRecieved: %d",pData->dwID); //Debug output
    
    			break;
    
    		}
    
    	}
    
    }
    
    
    
    void GetHeading()
    
    {
    
        HRESULT hr;
    
    
    
        if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Heading", NULL, 0, 0,0)))
    
        {
    
            //printf("\nConnected to Flight Simulator!");  //Debug output
    
    		// Set up the data definition, but do not yet do anything with it. The last argument is the angle the plane must turn in radians
    		//before the sim sends another angle position
    
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "HEADING INDICATOR", "Radians",SIMCONNECT_DATATYPE_FLOAT64,0.0078539);
    
    		//Request an event when simulation starts
    
    		hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START,"SimStart");
    
    		while( quit == 0 )
    
    		{
    
    			SimConnect_CallDispatch(hSimConnect,MyDispatchProcRD,NULL);
    
    			Sleep (1); //Sleep for 1 mSec
    
    		}
    
    		hr = SimConnect_Close(hSimConnect);
    
    	}
    
    }
    
    
    
    int __cdecl _tmain(int argc, _TCHAR* argv[])
    
    {
    
        GetHeading();
    
        return 0;
    
    }

  4. #3
    300+ Forum Addict
    Join Date
    Jan 2007
    Posts
    496
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: SimConnect GetPlaneHeading for Gyro Compass instrument

    Quote Originally Posted by MechDave View Post
    Here is the code, (almost complete... sans some comments)


    while( quit == 0 )

    {

    SimConnect_CallDispatch(hSimConnect,MyDispatchProcRD,NULL);

    Sleep (1); //Sleep for 1 mSec

    }
    You don't need to keep calling "CallDispatch" in a tight loop -- it is better (more efficient) to supply SimConnect with a Window handle and a User Message number (i.e. WM_USER+1 etc.), and it will then send you that message when there's something for you. THAT's when you call "CallDispatch".

    Check out the hWnd and UserEventWin32 parameters to the SimConnect Open call.

    Regards

    Pete

  5. #4
    10+ Posting Member
    Join Date
    Jul 2009
    Location
    Australia
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Unhappy Re: SimConnect GetPlaneHeading

    Pete,
    Having a little trouble handling the windows message, how should I catch the message so I can make it work, could you please explain a little more in depth?
    [EDIT]
    Had a crack at it, is this sort of right? Where am I going wrong, it all works but I have a funny feeling there are more than just 1 message windows on the desktop!
    Code:
    void GetHeading()
    {
        HRESULT hr;
    	HWND wo = 0;
    	DWORD HC_VEH = 110011;
    
        if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Heading", wo, HC_VEH, 0,0)))
        {
            //printf("\nConnected to Flight Simulator!");  //Debug output
    		// Set up the data definition, but do not yet do anything with it. The last argument is the angle the plane must turn in radians
    		//before the sim sends another angle position
    		hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "HEADING INDICATOR", "Radians",SIMCONNECT_DATATYPE_FLOAT64,0.0078539);
    		//Request an event when simulation starts
    		hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START,"SimStart");
    		while( quit == 0 )
    		{
    			if(FindWindowEx(NULL,NULL,NULL,NULL)!= NULL)
    			{
    				SimConnect_CallDispatch(hSimConnect,MyDispatchProcRD,NULL);
    				//Sleep (1); //Sleep for 1 mSec
    			}
    		}
    		hr = SimConnect_Close(hSimConnect);
    	}
    }
    Cheers,
    Dave

  6. #5
    300+ Forum Addict
    Join Date
    Jan 2007
    Posts
    496
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: SimConnect GetPlaneHeading

    Quote Originally Posted by MechDave View Post
    Having a little trouble handling the windows message, how should I catch the message so I can make it work, could you please explain a little more in depth?
    Sorry, what part needs explanation?

    If it is a Windows program, it must have a Windows Procedure, a "wndproc". That's where all windows messages are processed. It simply isn't possible to call a program a Windows program unless it processes Windows messages.

    Windows messages are used to draw the window, to get mouse and key events, etc etc. Just about everything Windows does with a windows program is done via its wndproc.

    In the WndProc, having switched on the message number, you have a "case WM_USER+1:" (say, depending what value you gave to the SimConnect_Open call), and there you do your CallDispatch. That's it.

    Pete

  7. #6
    300+ Forum Addict
    Join Date
    Jan 2007
    Posts
    496
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: SimConnect GetPlaneHeading

    Quote Originally Posted by MechDave View Post
    Had a crack at it, is this sort of right?
    No, totally different to what I said.

    { HWND wo = 0;

    A window handle of zero is not right. You need the handle to YOUR window.

    DWORD HC_VEH = 110011;

    Why not use "WM_USER + 1"?

    if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Heading", wo, HC_VEH, 0,0)))

    You are providing SimConnect with a zero Window handle. How can it call that Window? That makes no sense. You must provide it with a valid Window handle.

    while( quit == 0 )
    {
    if(FindWindowEx(NULL,NULL,NULL,NULL)!= NULL)
    {
    SimConnect_CallDispatch(hSimConnect,MyDispatchProcRD,NULL);
    //Sleep (1); //Sleep for 1 mSec
    }
    }


    You are still Calling your dispatcher in the same loop. Obviously you must because you failed to provide the Window handle for SimConnect to send you your message!

    What is the FindWindowEx for? What is it you think that is doing?

    Regards

    Pete

  8. #7
    10+ Posting Member
    Join Date
    Jul 2009
    Location
    Australia
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: SimConnect GetPlaneHeading

    Pete,
    Am I correct in thinking that to obtain a valid window handle (HWND), I first need to create a message only window?

    Do I need to use CreateWindow(), or is there an simpler function to call, or can I just use a current window handle?

    I have searched the internet with what I can think of, do you know of a good reference/tutorial for these message only windows?

    Please excuse my ignorance of Win32 programming as I have come from UNIX based programming, and I didn't do much of that, just basic console stuff.

  9. #8
    300+ Forum Addict
    Join Date
    Jan 2007
    Posts
    496
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: SimConnect GetPlaneHeading

    Quote Originally Posted by MechDave View Post
    Am I correct in thinking that to obtain a valid window handle (HWND), I first need to create a message only window?
    That would do. Any window would do, but if you really don't want or need anything on screen, then a message-only window would do.

    Do I need to use CreateWindow(), or is there an simpler function to call, or can I just use a current window handle?
    CreateWindow is simple enough. But if you already have a window, as you are implying here, why not use that? As long as you are processing messages!

    I have searched the internet with what I can think of, do you know of a good reference/tutorial for these message only windows?
    They are the same as any other, but don't receive Paint, Mouse or Keyboard messages because they have no user interface.

    Please excuse my ignorance of Win32 programming as I have come from UNIX based programming, and I didn't do much of that, just basic console stuff.
    Sorry, I didn't realise that when I was advising on efficient use of SimConnect. I find it quite difficult to imagine how one could write an efficient console program -- they are mostly used for short tasks, ones that come and go quickly. For anything persistent in Windows you normally design something which is message-driven, not something which is assumed to retain control of a processor al the time and which therefore has to deliberately relinquish it on occasion to get any cooperation from other processes.

    Maybe, if you don't really want to venture into Windows programming you are better off staying with the methods you were using before I interfered. However, if you do want to try, then almost any elementary book on Windows will start you off with a "Hello World" type of application, which makes a trivial Window to display that message. There realy are only a few parts:

    1. Register a "class" for your Window, usually using your application name. This Registration call provides the name of the windows message processing procedure (WndProc).

    2. Create the window using, usually, CreateWindow. It doesn't need to be a visible window -- a Message window certainly would not be visible.

    3. Loop getting messages and dispatching them (no sleep needed -- that's taken care of by GetMessage not returning till there is a message.). The loop is exited on a Quit message from Windows.

    4. The WndProc, processing any messages you want, on a Switch statement with Cases for each, ending with a call to the default window message processing for all those messages you aren't interested in.

    Regards

    Pete

  10. #9
    10+ Posting Member
    Join Date
    Jul 2009
    Location
    Australia
    Posts
    19
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Thumbs up Re: SimConnect GetPlaneHeading

    Thanks Pete,
    Im off to digest a book on Win32 API programming
    Cheers for the pointers on SimConnect, without them I would have been well stuck. I shall report back when I have managed to get an "invisible" window going and managed to feed it to SimConnect

    I think it is going to be a sizeable jump for me from procedural to event based programming, considering the last two days have been nearly a vertical learning curve for me... Just the way I like it.

    Cheers,
    Dave...

  11. #10
    25+ Posting Member
    Join Date
    Nov 2007
    Location
    Mobile, AL
    Posts
    48
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: SimConnect GetPlaneHeading

    Thanks to you both, Dave and Pete. I am just about to start, I have been building hardware for about a year and finished my cockpit upholstery (see attachment). Now I moving to instruments based on Mike Powells designs. I am anxiously awaiting the next book. My first batch of PIC chip hardware is inbound and a stepping motor instrument (directional gyro compass) is next for me.

    Dave, please keep posting as you solve you problems. We probably need a tutorial section somewhere.

    William, Tripacer Builder
    Attached Images Attached Images

Page 1 of 2 12 LastLast

Similar Threads

  1. FSX SDK Simconnect troubles
    By knuffelbaer1971 in forum General Microsoft Flight Simulator 10 (FSX)
    Replies: 1
    Last Post: 02-27-2010, 12:10 PM
  2. simconnect ?
    By Mr. Midnight in forum General Builder Questions All Aircraft Types
    Replies: 1
    Last Post: 11-01-2009, 12:04 AM
  3. C# programming and Simconnect
    By kranky in forum I/O Interfacing Hardware and Software
    Replies: 2
    Last Post: 05-14-2009, 06:51 PM

Tags for this Thread