- Messages
- 305
- Country
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:
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).
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:
I appreciate any help!
Kobbe
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;
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