• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

P3D v3 Serial Communication

Messages
305
Country
us-california
Hi all,

As stated in a previous post, I am just starting out in SimConnect plugin development. With the help of Ron, I have been able to build working plugins. I am interested in serial communication with an external device, and so far I have successfully been able to establish communication from an external device (Arduino in my case) to P3D. I had the Arduino run a simple function, sending values between -1.0 and +1.0 to move the elevator in P3D.

This worked great, besides a glitch now and then with the elevator position.

Using the throttle control example from the SDK, I modified it to include a 6Hz routine:

Code:
        case EVENT_6HZ:
        {
            //Get data from the serial connection
            readResult = SerialP->ReadData(incomingData, dataLength);
            incomingData[readResult] = 0;
            float temp = atof(incomingData); //Perform conversion to type float for P3D
            ec.elevatorPosition = temp;
            //Set elevator position
            hr = SimConnect_SetDataOnSimObject(hSimConnect, DEFINITION_ELEVATOR, SIMCONNECT_OBJECT_ID_USER, 0, 0, sizeof(ec.elevatorPosition), &ec.elevatorPosition);

        }
        break;
The serial object, SerialP, is getting the incoming serial data and placing it in a char buffer of size 256. This must then be converted to float and assigned to the elevator position struct.

Now I want to do the opposite - I'm trying to send the elevator data to my Arduino using a WriteData() command (These functions are defined in a header file provided by Arduino). The data is sent every time the elevator position is requested (Once every sim frame).

Code:
        case REQUEST_ELEVATOR:
        {
            // Read and set the initial elevator position value
            structElevatorControl *pS = (structElevatorControl*)&pObjData->dwData;

            ec.elevatorPosition = pS->elevatorPosition;
            double outputData= ec.elevatorPosition;
            char outStr[9];//[sizeof(outputData)];
            memcpy(&outStr, &outputData, sizeof(outputData));
            outStr[9] = '\n';
            printf("\nREQUEST_USERID received, elevator = %2.1f", pS->elevatorPosition);
            SerialP->WriteData(outStr, sizeof(outStr));
            Sleep(15);
        }

The problem I'm trying to tackle is taking the double value of the elevator position, placing it into the char buffer and appending a null terminator '\n' to indicate to the Arduino the end of data.

Could anyone suggest a more elegant way of taking the elevator position, which is a double, and placing it into a character array with a null terminator?

As reference, this is the WriteData() function:
Code:
bool Serial::WriteData(char *buffer, unsigned int nbChar)
{
    DWORD bytesSend;

    //Try to write the buffer on the Serial port
    if (!WriteFile(this->hSerial, (void *)buffer, nbChar, &bytesSend, 0))
    {
        //In case it don't work get comm error and return false
        ClearCommError(this->hSerial, &this->errors, &this->status);

        return false;
    }
    else
        return true;
}

I appreciate any help!

Kobbe
 

JB3DG

Resource contributor
Messages
1,325
Country
southafrica
you can use 1 byte hex values on your buffer like 0x00 for the \0 (that is your null terminator. \n is new line I believe).
 
Top