Ich entwickle einen Spiel-Simulator für Project Cars, ich habe den Code, der die Daten aus dem Shared Memory des PC nimmt, aber jetzt muss ich es über die serielle senden, Es muss an ein Arduino gesendet werden. Ich habe gesucht, wie es geht, aber ich habe nicht gefunden, wie man einen Int (das ist die Daten, die die API aus dem Spiel nimmt) an eine serielle Schnittstelle sendet.Wie sende ich einen Int über eine serielle Schnittstelle in C++?
Hier ist mein Code
// Used for memory-mapped functionality
#include <windows.h>
#include "sharedmemory.h"
// Used for this example
#include <stdio.h>
#include <conio.h>
//Serial
#include <iostream>
#include "SerialClass.h"
using namespace std;
// Name of the pCars memory mapped file
#define MAP_OBJECT_NAME "$pcars$"
int main()
{
SetConsoleTitle("API F1 Steering Wheel for Project Cars");
Serial* Puerto = new Serial("COM4");
// Open the memory-mapped file
HANDLE fileHandle = OpenFileMapping(PAGE_READONLY, FALSE, MAP_OBJECT_NAME);
if (fileHandle == NULL)
{
printf("Could not open file mapping object (%d).\n", GetLastError());
return 1;
}
// Get the data structure
const SharedMemory* sharedData = (SharedMemory*)MapViewOfFile(fileHandle, PAGE_READONLY, 0, 0, sizeof(SharedMemory));
if (sharedData == NULL)
{
printf("Could not map view of file (%d).\n", GetLastError());
CloseHandle(fileHandle);
return 1;
}
// Ensure we're sync'd to the correct data version
if (sharedData->mVersion != SHARED_MEMORY_VERSION)
{
printf("Data version mismatch\n");
return 1;
}
//------------------------------------------------------------------------------
// TEST DISPLAY CODE
//------------------------------------------------------------------------------
printf("ESC TO EXIT\n\n", sharedData->mUnfilteredSteering);
while (true)
{
const bool isValidParticipantIndex = sharedData->mViewedParticipantIndex != -1 && sharedData->mViewedParticipantIndex < sharedData->mNumParticipants && sharedData->mViewedParticipantIndex < STORED_PARTICIPANTS_MAX;
if (isValidParticipantIndex)
{
const ParticipantInfo& viewedParticipantInfo = sharedData->mParticipantInfo[sharedData->mViewedParticipantIndex];
printf("mParticipantName: (%s)\n", viewedParticipantInfo.mName);
printf("lap Distance = %f \n", viewedParticipantInfo.mCurrentLapDistance);
printf("mWorldPosition: (%f,%f,%f)\n", viewedParticipantInfo.mWorldPosition[0], viewedParticipantInfo.mWorldPosition[1], viewedParticipantInfo.mWorldPosition[2]);
}
printf("mGameState: (%d)\n", sharedData->mGameState);
printf("mSessionState: (%d)\n", sharedData->mSessionState);
printf("mRaceState: (%d)\n", sharedData->mRaceState);
printf("mGear: (%d)\n", sharedData-> mGear);
printf("mRpm: (%d)\n", sharedData->mRpm);
printf("mSpeed: (%d)\n", sharedData->mSpeed);
system("cls");
while (Puerto->IsConnected())
{
Puerto->WriteData(sharedData->mGear, sizeof(sharedData->mGear)); // Envía al puerto el texto "Luz_ON".
break;
}
if (_kbhit() && _getch() == 27) // check for escape
{
break;
}
}
//------------------------------------------------------------------------------
// Cleanup
UnmapViewOfFile(sharedData);
CloseHandle(fileHandle);
return 0;
}
wo in diesem Code ist es nicht? – TinyTheBrontosaurus
Bytes werden normalerweise auch über die Leitung gesendet. Ein int kann aus 2 oder 4 Bytes bestehen, also muss der Code den int in einen Byte-Puffer umwandeln und dann den übergeben. – TinyTheBrontosaurus
Wenn Sie können, und Sie können wahrscheinlich hier, bevorzugen 'Serial Puerto (" COM4 ");' 'Serial * Puerto = neue serielle (" COM4 ");'. Weniger Speicherverwaltungsfehler. – user4581301