Hello,
I'm trying to set up a "gaming dashboard" for playing Project Cars on my PC. Due to Shared Memory I'm able to get some ingame-data which I want to send to my new Arduino Uno Rev3, and with the tone.h library I can visualize the data on my real dashboard (Ford Mondeo Mk1 btw...).
I know that I can only send one tone at a time, so I will get another Uno or Mega because i think the Mega can send two tones?
But let's get to my real problem:
I've already written some code to see how the transfer is working. This is my code for the PC...
#include <conio.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <windows.h>
#include "SerialPort.h"
#include "SerialPort.cpp"
#include "SharedMemory.h"
#define MAP_OBJECT_NAME "$pcars$"
using namespace std;
//String for getting the output from arduino
char output[MAX_DATA_LENGTH-1];
char *port_name = "\\\\.\\COM4";
//String for incoming data
char incomingData[MAX_DATA_LENGTH];
int kmh, rpm, gear, oiltemp, antilock, fuel;
int main() {
//Initializing the arduino
SerialPort arduino(port_name);
if (!arduino.isConnected()) {
printf("Check port name.\n");
}
//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("Running...");
while (arduino.isConnected())
{
kmh = (int)((sharedData->mSpeed)*3.6);
rpm = (int)sharedData->mRpm;
gear = sharedData->mGear;
oiltemp = (int)sharedData->mOilTempCelsius;
antilock = (int)sharedData->mAntiLockActive;
fuel = (int)((sharedData->mFuelLevel)*100);
sprintf(output, "%i,%i,%i,%i,%i,%i\n", kmh, rpm, gear, oiltemp, antilock, fuel);
printf("%s\n", output); //Debug
arduino.writeSerialPort(output, MAX_DATA_LENGTH);
Sleep(20);
arduino.readSerialPort(incomingData, MAX_DATA_LENGTH);
puts(incomingData); //Debug
Sleep(20);
}
//------------------------------------------------------------------------------
// Cleanup
UnmapViewOfFile(sharedData);
CloseHandle(fileHandle);
return 0;
}
...and this for the Arduino,
String input;
char output[255];
int kmh, rpm, gear, oiltemp, antilock, fuel;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
input = Serial.readStringUntil('\n');
sscanf(input.c_str(), "%i,%i,%i,%i,%i,%i", &kmh, &rpm, &gear, &oiltemp, &antilock, &fuel);
delay(20);
//Debug
sprintf(output, "%i,%i,%i,%i,%i,%i", kmh, rpm, gear, oiltemp, antilock, fuel);
Serial.println(output);
delay(20);
}
}
The codes are compiling and running well, but the data on the arduino somehow doesn't seem to update.
To explain it in detail: Let's assume I pause the game and start the two programs, the output would be:
1139,0,1,50,0,25
1139,0,1,50,0,25
So far so good, all the recieved data is matching.
But when I go back ingame and start to drive, the output is the following:
5349,49,1,51,0,25
1139,0,1,50,0,25
The data on the arduino is somehow frozen according to the data recieved at the start of the program.
I even tried to just transmit one dataset - such as RPM, i also changed the delay times to other values... None of these is fixing my problem.
You can read some "tutorial" for it here.
Feel free to contact me for more information. Can you tell me why the **** this is happening?
Thanks in advance!