Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14
  1. #11
    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

    Mycockpit.org Re: SimConnect GetPlaneHeading

    I have now successfuly got a window working with FSX to output the heading in degrees. The exe can be downloaded here. The next step is to get the data to the serial port. I soon will be calling for beta testers to try the extension out on their own gyrocompass gauge.

  2. #12
    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

    MechDave,
    Do you have your gryo compass circuit working? I am almost finished with the mechanical fabrication. I am building a 1960 version of a gyro compass. I have been breadboarding 3315 rotary encoders learning how to decode the output pulses. I am about ready to start experimenting with the LW293 driver and the ste pping motor. I really want to develop a circuit board in 3.24" format to attach to the back of the mechanical stack.

    William, Tripacer Builder



  3. #13
    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

    If you use a stepper motor, you can graft Mike's electronics and firmware onto it and then run it using my translation code from FSX it should all work. You don't even need to use a 0.9 degree step motor either, whatever step motor you get just change the number of steps in the firmware adn all should be good

  4. #14
    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

    Mycockpit.org Re: SimConnect GetPlaneHeading

    I have got all the data out to the visible window to show the number of steps along with the basic serial port stuff going. Here is the code to make the window and serial port open up. I haven't got the data to work for the serial port yet. There are several notification windows in the code to tell me what is going on... -->

    Code:
    #include 
    
    #include "SimConnect.h"
    
    #include   //Needed for sprintf()remove for production code
    
    
    
    const char g_szClassName[] = "myWindowClass";
    
    HANDLE  hSerial,hSimConnect = NULL;
    
    HWND hwnd = NULL;
    
    
    
    double degrees=0; //Remove for production
    
    
    
    struct CMD  //ADT for Commands. No frame transfer function added yet
    
    {
    
    	char* ID;
    
    	int Byte5;
    
    	int MotorPos;
    
    }QueryName = {"ZZZ99",8}, Reset = {"ZZZ99",0}, SetMotorPos = {"ZZZ99",1,0};
    
    
    
    
    
    struct Struct1
    
    {
    
        double  radians;  //More members of Struct1 to come
    
    };
    
    
    
    static enum EVENT_ID{
    
        EVENT_SIM_START,
    
    };
    
    
    
    static enum DATA_DEFINE_ID {
    
        DEFINITION_1,
    
    };
    
    
    
    static enum DATA_REQUEST_ID {
    
        REQUEST_1,
    
    };
    
    
    
    
    
    void ReadQuery (HANDLE hSerial)
    
    {
    
    	char szBuff[18] = {0};
    
    	DWORD dwBytesRead = {17};
    
    
    
    	MessageBox(NULL, "Thread ReadQuery works", "Success!",
    
    				MB_ICONINFORMATION | MB_OK);
    
    	if(!ReadFile(hSerial,szBuff,18,&dwBytesRead,NULL))
    
    	{
    
    		//Error occured
    
    		MessageBox(NULL, "Failed to read from instrument", "Error!",
    
    				MB_ICONEXCLAMATION | MB_OK);
    
    	}
    
    }
    
    void WriteQuery(HANDLE hSerial)
    
    {
    
    	char szBuff[7] = {"ZZZ998"};
    
    	DWORD dwBytesWrite = {0};
    
    
    
    	MessageBox(NULL, "Thread WriteQuery works", "Success!",
    
    				MB_ICONINFORMATION | MB_OK);
    
    	if(!WriteFile(hSerial,szBuff,7,&dwBytesWrite,NULL))
    
    	{
    
    		//Error occured
    
    		MessageBox(NULL, "Failed to write to serial port", "Error!",
    
    				MB_ICONEXCLAMATION | MB_OK);
    
    	}
    
    }
    
    void QueryGaugeName(HANDLE hSerial)
    
    {
    
    	//Write to serial port QueryName.ID and QueryName.Byte5
    
    	//Recieve from serial port 17 bytes of data from gauge.
    
    	//Function runs as a thread.
    
    	HANDLE hThreadWrite = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WriteQuery,hSerial,0,NULL);
    
    	if(hThreadWrite == NULL)
    
    	{
    
    		MessageBox(NULL, "Thread for reading instrument type failed", "Error!",
    
            MB_ICONEXCLAMATION | MB_OK);
    
    	}
    
    	HANDLE hThreadRead = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReadQuery,hSerial,0,NULL);
    
    	if(hThreadWrite == NULL)
    
    	{
    
    		MessageBox(NULL, "Thread for reading instrument type failed", "Error!",
    
            MB_ICONEXCLAMATION | MB_OK);
    
    	}
    
    }
    
    void SetupSerialPort(DCB &dcbSerialParams, COMMTIMEOUTS &timeouts)
    
    {
    
    	//Function call to allow access to serial port. Opens serial port for non overlapped read/write
    
    		hSerial = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
    
    
    
    		if(hSerial == INVALID_HANDLE_VALUE)
    
    		{
    
    			if(GetLastError() == ERROR_FILE_NOT_FOUND)
    
    			{
    
    				MessageBox(NULL, "Serial Port does not exist", "Error!",
    
    				MB_ICONEXCLAMATION | MB_OK);
    
    				DestroyWindow(hwnd);
    
    			}
    
    			else //Occurence of unknown error
    
    			{
    
    				MessageBox(NULL, "Unknown Error - FAILED", "Error!",
    
    				MB_ICONEXCLAMATION | MB_OK);
    
    				DestroyWindow(hwnd);
    
    			}
    
    		}
    
    		else //Proceed to setting up serial port
    
    		{
    
    			MessageBox(NULL, "Serial Port does exist", "Success!",
    
    				MB_ICONINFORMATION | MB_OK);
    
    
    
    			dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    
    			if(!GetCommState(hSerial, &dcbSerialParams))
    
    			{
    
    				//error getting state
    
    				MessageBox(NULL, "Error getting state of port", "Error!",
    
    					MB_ICONEXCLAMATION | MB_OK);
    
    			}
    
    
    
    			dcbSerialParams.BaudRate = CBR_19200;
    
    			dcbSerialParams.ByteSize = 8;
    
    			dcbSerialParams.StopBits = ONESTOPBIT;
    
    			dcbSerialParams.Parity = NOPARITY;
    
    
    
    			if(!SetCommState(hSerial, &dcbSerialParams))
    
    			{
    
    				//error setting serial port state
    
    				MessageBox(NULL, "Error setting port", "Error!",
    
    					MB_ICONEXCLAMATION | MB_OK);
    
    			}
    
    			else
    
    			{
    
    				MessageBox(NULL, "Success setting port", "Success!",
    
    					MB_ICONEXCLAMATION | MB_OK);
    
    			}
    
    
    
    			timeouts.ReadIntervalTimeout = 50;
    
    			timeouts.ReadTotalTimeoutConstant = 50;
    
    			timeouts.ReadTotalTimeoutMultiplier = 10;
    
    			timeouts.WriteTotalTimeoutConstant = 50;
    
    			timeouts.WriteTotalTimeoutMultiplier = 10;
    
    
    
    			if(!SetCommTimeouts(hSerial, &timeouts))
    
    			{
    
    				//timeout set error occurred
    
    				MessageBox(NULL, "Timeout set error", "Error!",
    
    					MB_ICONEXCLAMATION | MB_OK);
    
    			}
    
    			else
    
    			{
    
    				MessageBox(NULL, "Success setting Timeouts", "Success!",
    
    					MB_ICONEXCLAMATION | MB_OK);
    
    			}
    
    		}
    
    } //End of SetupSerialPort()
    
    
    
    void CALLBACK MyDispatchProcRD(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
    
    {
    
        HRESULT hr;
    
    	static double conversion = 57.2957780; //Conversion for radians --> degrees 
    
    										   //courtesy of Google :) Remove for production
    
    	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, 
    
    					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);
    
    					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;
    
    					SetMotorPos.MotorPos = (int)(pS->radians / 0.0078539);
    
    					//degrees = pS->radians * conversion; //Used to provide a user interface
    
                        break; 
    
    				}
    
    			default:
    
    				{
    
    				break;
    
    				}
    
    			}
    
    			break;
    
    		}
    
    	case SIMCONNECT_RECV_ID_QUIT:
    
    		{
    
    			DestroyWindow(hwnd);
    
    			CloseHandle(hSerial);
    
    			break;
    
    		}
    
    	default:
    
    		{
    
    			break;
    
    		}
    
    	}
    
    } //End of MyDispatchProcRD
    
    
    
    // Step 4: the Window Procedure
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    
    {
    
        switch(msg)
    
        {
    
            case WM_CLOSE:
    
    			{
    
    				DestroyWindow(hwnd);
    
    				CloseHandle(hSerial);
    
    				return 0;
    
    			}
    
            break;
    
            case WM_DESTROY:
    
    			{
    
    				PostQuitMessage(0);
    
    				CloseHandle(hSerial);
    
    				return 0;
    
    			}
    
            break;
    
    		case WM_USER+1:
    
    		{
    
    				SimConnect_CallDispatch(hSimConnect,MyDispatchProcRD,NULL);
    
    				//Place call to serial function to update the instrument here
    
    				InvalidateRect(hwnd,NULL,TRUE);
    
    				return 0;
    
    		}
    
    		case WM_PAINT:  //Only needed for testing the program... Remove after setting HWND_MESSAGE in final code.
    
    			{
    
    			PAINTSTRUCT   Ps;
    
    			HDC          hDC;
    
    			TCHAR szBuffer[40];
    
    			TCHAR szBuffer2[40];
    
    			int iLength;
    
    			hDC = BeginPaint(hwnd, &Ps);
    
    			//Convert the degrees into a string for use with TextOut()
    
    			sprintf_s (szBuffer2,"%d",SetMotorPos.MotorPos );
    
    			iLength = wsprintf(szBuffer,TEXT("Heading is %s steps"),szBuffer2);
    
    			TextOut(hDC,0,0,szBuffer,iLength);
    
    			EndPaint(hwnd, &Ps);
    
    			return 0;
    
    			}
    
    		default:
    
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    
        }
    
        return 0;
    
    }  //End of WndProc
    
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    
        LPSTR lpCmdLine, int nCmdShow)
    
    {
    
        WNDCLASSEX wc;
    
        MSG Msg;
    
    	HRESULT hr;
    
    	DCB dcbSerialParams = {0};
    
    	COMMTIMEOUTS timeouts ={0};
    
    	HANDLE hThread1 = NULL;
    
    	DWORD ThreadID;
    
    	//Step 1: Registering the Window Class
    
        wc.cbSize        = sizeof(WNDCLASSEX);
    
        wc.style         = 0;
    
        wc.lpfnWndProc   = WndProc;
    
        wc.cbClsExtra    = 0;
    
        wc.cbWndExtra    = 0;
    
        wc.hInstance     = hInstance;
    
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    
        wc.lpszMenuName  = NULL;
    
        wc.lpszClassName = g_szClassName;
    
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
    
    
        if(!RegisterClassEx(&wc))
    
        {
    
            MessageBox(NULL, "Window Registration Failed!", "Error!",
    
                MB_ICONEXCLAMATION | MB_OK);
    
            return 0;
    
        }
    
    		
    
    	// Step 2: Creating the Window
    
    	hwnd = CreateWindowEx(
    
    			WS_EX_CLIENTEDGE,
    
    			g_szClassName,
    
    			"Heading Window",
    
    			WS_OVERLAPPEDWINDOW,
    
    			CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
    
    			/*HWND_MESSAGE*/NULL, NULL, hInstance, NULL);
    
    
    
    	if(hwnd == NULL)
    
    		{
    
    			MessageBox(NULL, "Window Creation Failed!", "Error!",
    
    				MB_ICONEXCLAMATION | MB_OK);
    
    			return 0;
    
    		}
    
    	//Test and open a connection to the SimConnect server	
    
    	if (SUCCEEDED(
    
    			SimConnect_Open(&hSimConnect, "Request Heading", 
    
    			hwnd, WM_USER+1, 0,SIMCONNECT_OPEN_CONFIGINDEX_LOCAL)))
    
    	{
    
    		hr = SimConnect_AddToDataDefinition(
    
    				hSimConnect, DEFINITION_1, "HEADING INDICATOR", 
    
    				"Radians",SIMCONNECT_DATATYPE_FLOAT64,0.0078539f);
    
    
    
    		//Request an event when simulation starts
    
    		hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START,"SimStart");
    
    
    
    		//Now that everything is up and connected we need to set up the serial port!
    
    		SetupSerialPort(dcbSerialParams,timeouts);
    
    
    
    		//Now that the serial port is set up we can start the read thread for reading instrument type.
    
    		hThread1 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)QueryGaugeName,hSerial,0,&ThreadID);
    
    		if(hThread1 == NULL)
    
    		{
    
    			MessageBox(NULL, "Thread for reading instrument type failed", "Error!",
    
                MB_ICONEXCLAMATION | MB_OK);
    
    		}
    
    
    
    		
    
    		//Now I am happy we can show the window (remove for production)
    
    		ShowWindow(hwnd, nCmdShow);
    
    		UpdateWindow(hwnd);
    
    		while(GetMessage(&Msg, NULL, 0, 0) > 0)
    
    		{
    
    			DispatchMessage(&Msg);
    
    		}
    
        return Msg.wParam;
    
    	}
    
    	else //Write an error message window to screen if Simconnect fails.
    
    	{
    
    		MessageBox(NULL, "This program requires Flight Simulator X to be running", "Error!",
    
                MB_ICONEXCLAMATION | MB_OK);
    
    		DestroyWindow(hwnd);
    
    		return 0;
    
    	}
    
    }
    I have the project files in a zip archive here

    Cheers,
    Dave

Page 2 of 2 FirstFirst 12

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